Skip to content

Commit

Permalink
Add Angular plugin and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simisimon committed Sep 29, 2024
1 parent a5dcd83 commit 4ab829a
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/cfgnet/config_types/config_type_inferer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ConfigTypeInferer:
)
regex_time_value = re.compile(r"[\d]+ ?(s|min|h|d|ms)*")
regex_filepath_option = re.compile(
r"path|dir|directory|folder|destination"
r"path|dir|directory|folder|destination|root"
)
# regex_filepath_value = re.compile(r"\/?([^\/]+\/)+[^\/]*")
regex_filepath_value = re.compile(r"^([~.\w\d]*\/[.\w\d]+)+(\.[\w\d]+)*$")
Expand Down
62 changes: 62 additions & 0 deletions src/cfgnet/plugins/concept/angular_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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.json_plugin import JsonPlugin
from cfgnet.config_types.config_types import ConfigType


class AngularPlugin(JsonPlugin):
def __init__(self):
super().__init__("angular")
self.excluded_keys = [
"keywords",
"description",
"author",
"contributors",
]

def is_responsible(self, abs_file_path: str) -> bool:
file_name = os.path.basename(abs_file_path)
return file_name == "angular.json"

# 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 in ("version"):
return ConfigType.VERSION_NUMBER
if option_name in (
"main",
"sourceRoot",
"replace",
"with",
"root",
"$schema",
):
return ConfigType.PATH
if option_name in ("scripts", "bin"):
return ConfigType.COMMAND
if option_name in (
"name",
"bundledDependencies",
"newProjectRoot",
"projectType",
):
return ConfigType.NAME
return ConfigType.UNKNOWN
13 changes: 2 additions & 11 deletions src/cfgnet/plugins/file_type/json_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,13 @@ def _parse_json_object(
parent.add_child(virtual_option)

if isinstance(parent, OptionNode):
name = (
f"{parent.name}:{item}"
if parent.config_type == ConfigType.VERSION_NUMBER
else item
)

name = item
value = ValueNode(name=name)
virtual_option.add_child(value)

else:
if not isinstance(parent, ArtifactNode):
name = (
f"{parent.name}:{json_object}"
if parent.config_type == ConfigType.VERSION_NUMBER
else json_object
)
name = json_object
value = ValueNode(name=name)

parent.add_child(value)
Expand Down
2 changes: 2 additions & 0 deletions src/cfgnet/plugins/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from cfgnet.plugins.concept.yarn_plugin import YarnPlugin
from cfgnet.plugins.concept.elastisearch_plugin import ElasticsearchPlugin
from cfgnet.plugins.concept.kafka_plugin import KafkaPlugin
from cfgnet.plugins.concept.angular_plugin import AngularPlugin


class PluginManager:
Expand Down Expand Up @@ -79,6 +80,7 @@ class PluginManager:
YarnPlugin(),
ElasticsearchPlugin(),
KafkaPlugin(),
AngularPlugin(),
]

file_type_plugins: List[Plugin] = [
Expand Down
81 changes: 81 additions & 0 deletions tests/cfgnet/plugins/concept/test_angular_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 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.angular_plugin import AngularPlugin
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 = AngularPlugin()
return plugin


def test_is_responsible(get_plugin):
plugin = get_plugin

angular_file = plugin.is_responsible("tests/files/angular.json")
no_angular_file = plugin.is_responsible("tests/files/test.json")

assert angular_file
assert not no_angular_file


def test_parse_angular_file(get_plugin):
plugin = get_plugin
file = os.path.abspath("tests/files/angular.json")

artifact = plugin.parse_file(file, "angular.json")
nodes = artifact.get_nodes()
ids = {node.id for node in nodes}

assert artifact is not None
assert len(nodes) == 13

assert make_id("angular.json", "file", "angular.json") in ids
assert make_id("angular.json", "$schema", "./node_modules/@angular/cli/lib/config/schema.json") in ids
assert make_id("angular.json", "version", "1") in ids
assert make_id("angular.json", "projects", "my-app", "architect", "configurations", "production", "fileReplacements", "replace", "src/environments/environment.ts") in ids
assert make_id("angular.json", "projects", "my-app", "architect", "configurations", "production", "fileReplacements", "with", "src/environments/environment.prod.ts") in ids
assert make_id("angular.json", "projects", "my-app", "test", "builder", "@angular-devkit/build-angular:karma") in ids
assert make_id("angular.json", "projects", "my-app", "projectType", "application") in ids
assert make_id("angular.json", "projects", "my-app", "architect", "configurations", "production", "buildOptimizer", "True") in ids
assert make_id("angular.json", "projects", "my-app", "architect", "configurations", "production", "fileReplacements", "with", "src/environments/environment.prod.ts") in ids
assert make_id("angular.json", "projects", "my-app", "test", "options", "main", "src/test.ts") in ids
assert make_id("angular.json", "projects", "my-app", "sourceRoot", "src") in ids
assert make_id("angular.json", "projects", "my-app", "root", "") in ids
assert make_id("angular.json", "newProjectRoot", "projects") in ids


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

version_node = next(filter(lambda x: x.id == make_id("angular.json", "version", "1"), nodes))
path_node = next(filter(lambda x: x.id == make_id("angular.json", "projects", "my-app", "sourceRoot", "src"), nodes))
path2_node = next(filter(lambda x: x.id == make_id("angular.json", "projects", "my-app", "architect", "configurations", "production", "fileReplacements", "with", "src/environments/environment.prod.ts"), nodes))
boolean_node = next(filter(lambda x: x.id == make_id("angular.json", "projects", "my-app", "architect", "configurations", "production", "buildOptimizer", "True"), nodes))

assert version_node.config_type == ConfigType.VERSION_NUMBER
assert path_node.config_type == ConfigType.PATH
assert path2_node.config_type == ConfigType.PATH
assert boolean_node.config_type == ConfigType.BOOLEAN
2 changes: 1 addition & 1 deletion tests/cfgnet/plugins/concept/test_cypress_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_parse_cypress_file(get_plugin):
assert make_id("cypress.json", "viewportWidth", "1064") in ids
assert make_id("cypress.json", "reporter", "junit") in ids
assert make_id("cypress.json", "specPattern", "*.test.js") in ids
assert make_id("cypress.json", "nodeVersion", "nodeVersion:system") in ids
assert make_id("cypress.json", "nodeVersion", "system") in ids


def test_config_types(get_plugin):
Expand Down
8 changes: 4 additions & 4 deletions tests/cfgnet/plugins/concept/test_nodejs_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ def test_parse_package_json_file(get_plugin):

assert make_id("package.json", "file", "package.json") in ids
assert make_id("package.json", "name", "node-js-sample") in ids
assert make_id("package.json", "version", "version:0.2.0") in ids
assert make_id("package.json", "version", "0.2.0") in ids
assert make_id("package.json", "main", "index.js") in ids
assert make_id("package.json", "license", "ISC") in ids

assert make_id("package.json", "scripts", "start", "node index.js") in ids
assert (
make_id("package.json", "dependencies", "express", "express:^4.13.3")
make_id("package.json", "dependencies", "express", "^4.13.3")
in ids
)
assert (
make_id("package.json", "devDependencies", "nodemon", "nodemon:^1.1.1")
make_id("package.json", "devDependencies", "nodemon", "^1.1.1")
in ids
)
assert (
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_config_types(get_plugin):
filter(
lambda x: x.id
== make_id(
"package.json", "dependencies", "express", "express:^4.13.3"
"package.json", "dependencies", "express", "^4.13.3"
),
nodes,
)
Expand Down
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) == 24
assert len(all_plugins) == 25


def test_get_responsible_plugin():
Expand Down Expand Up @@ -69,6 +69,7 @@ def test_get_responsible_plugin():
yarn_plugin = PluginManager.get_responsible_plugin(plugins, "path/to/yarn-site.xml")
elasticsearch_plugin = PluginManager.get_responsible_plugin(plugins, "path/to/elasticsearch.yml")
kafka_plugin = PluginManager.get_responsible_plugin(plugins, "path/to/server.properties")
angular_plugin = PluginManager.get_responsible_plugin(plugins, "path/to/angular.json")

assert docker_plugin.concept_name == "docker"
assert maven_plugin.concept_name == "maven"
Expand All @@ -94,3 +95,4 @@ def test_get_responsible_plugin():
assert yarn_plugin.concept_name == "yarn"
assert elasticsearch_plugin.concept_name == "elasticsearch"
assert kafka_plugin.concept_name == "kafka"
assert angular_plugin.concept_name == "angular"
36 changes: 36 additions & 0 deletions tests/files/angular.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"my-app": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"architect": {
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"outputHashing": "all",
"buildOptimizer": true
}
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"scripts": []
}
}

}
}
}

0 comments on commit 4ab829a

Please sign in to comment.