Skip to content

Commit

Permalink
Add Flutter plugin and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Simon committed Oct 10, 2024
1 parent 84e98bf commit 9ed7852
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 1 deletion.
61 changes: 61 additions & 0 deletions src/cfgnet/plugins/concept/flutter_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# This file is part of the CfgNet module.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
import os
from cfgnet.plugins.file_type.yaml_plugin import YAMLPlugin
from cfgnet.config_types.config_types import ConfigType


class FlutterPlugin(YAMLPlugin):
def __init__(self):
super().__init__("flutter")

def is_responsible(self, abs_file_path) -> bool:
file_name = os.path.basename(abs_file_path)
return file_name == "pubspec.yaml"

# pylint: disable=too-many-return-statements
def get_config_type(self, option_name: str) -> ConfigType:
"""
Find config type based on option name.
:param option_name: name of option
:return: config type
"""
if option_name.endswith(("name", "description", "ref")):
return ConfigType.NAME

if option_name.endswith(
("version", "dependencies", "dev-dependencies", "environment")
):
return ConfigType.VERSION_NUMBER

if option_name.endswith(("exectuables", "false_secrets", "path")):
return ConfigType.PATH

if option_name.endswith(
(
"homepage",
"issue_tracker",
"documentation",
"repository",
"hosted",
"url",
"git",
"funding",
)
):
return ConfigType.URL

return ConfigType.UNKNOWN
2 changes: 2 additions & 0 deletions src/cfgnet/plugins/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
from cfgnet.plugins.concept.cargo_plugin import CargoPlugin
from cfgnet.plugins.concept.github_actions_plugin import GitHubActionPlugin
from cfgnet.plugins.concept.gradle_plugin import GradlePlugin
from cfgnet.plugins.concept.flutter_plugin import FlutterPlugin


class PluginManager:
Expand Down Expand Up @@ -91,6 +92,7 @@ class PluginManager:
CargoPlugin(),
GitHubActionPlugin(),
GradlePlugin(),
FlutterPlugin(),
]

file_type_plugins: List[Plugin] = [
Expand Down
57 changes: 57 additions & 0 deletions tests/cfgnet/plugins/concept/test_flutter_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This file is part of the CfgNet module.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.

import os

import pytest

from cfgnet.plugins.concept.flutter_plugin import FlutterPlugin
from cfgnet.config_types.config_types import ConfigType
from tests.utility.id_creator import make_id


@pytest.fixture(name="get_plugin")
def get_plugin_():
plugin = FlutterPlugin()
return plugin


def test_is_responsible(get_plugin):
plugin = get_plugin

assert plugin.is_responsible("tests/files/pubspec.yaml")
assert not plugin.is_responsible("tests/files/pubspec.yml")


def test_config_types(get_plugin):
plugin = get_plugin
file = os.path.abspath("tests/files/pubspec.yaml")
artifact = plugin.parse_file(file, "pubspec.yaml")
nodes = artifact.get_nodes()

for node in nodes:
print(node, node.config_type)

version_node = next(filter(lambda x: x.id == make_id("pubspec.yaml", "version", "1.0.0"), nodes))
name_node = next(filter(lambda x: x.id == make_id("pubspec.yaml", "name", "my_flutter_app"), nodes))
url_node = next(filter(lambda x: x.id == make_id("pubspec.yaml", "homepage", "https://example.com"), nodes))
boolean_node = next(filter(lambda x: x.id == make_id("pubspec.yaml", "flutter", "uses-material-design", "true"), nodes))
dep_node = next(filter(lambda x: x.id == make_id("pubspec.yaml", "dev_dependencies", "test", ">=1.15.0 <2.0.0"), nodes))

assert version_node.config_type == ConfigType.VERSION_NUMBER
assert boolean_node.config_type == ConfigType.BOOLEAN
assert dep_node.config_type == ConfigType.VERSION_NUMBER
assert name_node.config_type == ConfigType.NAME
assert url_node.config_type == ConfigType.URL
4 changes: 3 additions & 1 deletion tests/cfgnet/plugins/test_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
def test_get_all_plugins():
all_plugins = PluginManager.get_plugins()

assert len(all_plugins) == 30
assert len(all_plugins) == 31


def test_get_responsible_plugin():
Expand Down Expand Up @@ -55,6 +55,7 @@ def test_get_responsible_plugin():
cargo_plugin = PluginManager.get_responsible_plugin(plugins, "path/to/Cargo.toml")
github_action_plugin = PluginManager.get_responsible_plugin(plugins, ".github/workflows/test.yml")
gradle_plugin = PluginManager.get_responsible_plugin(plugins, "path/to/gradle.properties")
flutter_plugin = PluginManager.get_responsible_plugin(plugins, "path/to/pubspec.yaml")

assert docker_plugin.concept_name == "docker"
assert maven_plugin.concept_name == "maven"
Expand Down Expand Up @@ -86,3 +87,4 @@ def test_get_responsible_plugin():
assert cargo_plugin.concept_name == "cargo"
assert github_action_plugin.concept_name == "github-action"
assert gradle_plugin.concept_name == "gradle"
assert flutter_plugin.concept_name == "flutter"
18 changes: 18 additions & 0 deletions tests/files/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: my_flutter_app
description: A new Flutter project.
version: 1.0.0
homepage: https://example.com

environment:
sdk: '^3.2.0'

dependencies:
efts: ^2.0.4
transmogrify: ^0.4.0

dev_dependencies:
test: '>=1.15.0 <2.0.0'

flutter:
uses-material-design: true

0 comments on commit 9ed7852

Please sign in to comment.