generated from NicolaiRuckel/python-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cargo plugin and tests, update Toml plugin
- Loading branch information
Sebastian Simon
committed
Oct 9, 2024
1 parent
cb5a1ee
commit a405148
Showing
8 changed files
with
198 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# 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/>. | ||
from cfgnet.plugins.file_type.toml_plugin import TomlPlugin | ||
from cfgnet.config_types.config_types import ConfigType | ||
|
||
|
||
class CargoPlugin(TomlPlugin): | ||
def __init__(self): | ||
super().__init__("cargo") | ||
self.excluded_keys = [ | ||
"description", | ||
"authors", | ||
"maintainers", | ||
"readme", | ||
"keywords", | ||
"classifiers", | ||
] | ||
|
||
def is_responsible(self, abs_file_path: str) -> bool: | ||
if abs_file_path.endswith("Cargo.toml"): | ||
return True | ||
return False | ||
|
||
@staticmethod | ||
def get_config_type(option_name: str) -> ConfigType: | ||
""" | ||
Find config type based on option name. | ||
:param option_name: name of option | ||
:return: config type | ||
""" | ||
if option_name in ( | ||
"homepage", | ||
"repository", | ||
"documentation", | ||
"urls", | ||
"url", | ||
): | ||
return ConfigType.URL | ||
if option_name in ("include", "exclude"): | ||
return ConfigType.PATH | ||
if option_name in ("version", "dependencies", "dev-dependencies"): | ||
return ConfigType.VERSION_NUMBER | ||
return ConfigType.UNKNOWN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# 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.cargo_plugin import CargoPlugin | ||
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 = CargoPlugin() | ||
return plugin | ||
|
||
|
||
def test_is_responsible(get_plugin): | ||
plugin = get_plugin | ||
|
||
assert plugin.is_responsible("tests/files/Cargo.toml") | ||
assert not plugin.is_responsible("tests/files/test.toml") | ||
|
||
|
||
def test_parse_pyproject_file(get_plugin): | ||
plugin = get_plugin | ||
file = os.path.abspath("tests/files/Cargo.toml") | ||
|
||
artifact = plugin.parse_file(file, "Cargo.toml") | ||
nodes = artifact.get_nodes() | ||
ids = {node.id for node in nodes} | ||
|
||
assert artifact is not None | ||
assert len(nodes) == 13 | ||
|
||
assert make_id("Cargo.toml", "file", "Cargo.toml") in ids | ||
assert make_id("Cargo.toml", "package", "name", "example") in ids | ||
assert make_id("Cargo.toml", "package", "version", "0.1.0") in ids | ||
assert make_id("Cargo.toml", "package", "edition", "2021") in ids | ||
assert make_id("Cargo.toml", "package", "license", "MIT") in ids | ||
assert make_id("Cargo.toml", "package", "homepage", "https://example.com") in ids | ||
assert make_id("Cargo.toml", "package", "workspace", "path/to/workspace/root") in ids | ||
assert make_id("Cargo.toml", "dependencies", "serde", "1.0") in ids | ||
assert make_id("Cargo.toml", "dependencies", "rand", "0.8") in ids | ||
assert make_id("Cargo.toml", "dev-dependencies", "tokio", "version", "1") in ids | ||
assert make_id("Cargo.toml", "dev-dependencies", "tokio", "features", "['full']") in ids | ||
assert make_id("Cargo.toml", "features", "default", "['serde']") in ids | ||
assert make_id("Cargo.toml", "profile", "release", "opt-level", "3") in ids | ||
|
||
|
||
def test_config_types(get_plugin): | ||
plugin = get_plugin | ||
file = os.path.abspath("tests/files/Cargo.toml") | ||
|
||
artifact = plugin.parse_file(file, "Cargo.toml") | ||
nodes = artifact.get_nodes() | ||
|
||
name_node = next(filter(lambda x: x.id == make_id("Cargo.toml", "package", "name", "example"), nodes)) | ||
path_node = next(filter(lambda x: x.id == make_id("Cargo.toml", "package", "workspace", "path/to/workspace/root"), nodes)) | ||
number_node = next(filter(lambda x: x.id == make_id("Cargo.toml", "package", "edition", "2021"), nodes)) | ||
dep_node = next(filter(lambda x: x.id == make_id("Cargo.toml", "dependencies", "serde", "1.0"), nodes)) | ||
url_node = next(filter(lambda x: x.id == make_id("Cargo.toml", "package", "homepage", "https://example.com"), nodes)) | ||
version_node = next(filter(lambda x: x.id == make_id("Cargo.toml", "package", "version", "0.1.0"), nodes)) | ||
license_node = next(filter(lambda x: x.id == make_id("Cargo.toml", "package", "license", "MIT"), nodes)) | ||
number_node2 = next(filter(lambda x: x.id == make_id("Cargo.toml", "profile", "release", "opt-level", "3"), nodes)) | ||
|
||
assert path_node.config_type == ConfigType.PATH | ||
assert url_node.config_type == ConfigType.URL | ||
assert license_node.config_type == ConfigType.LICENSE | ||
assert dep_node.config_type == ConfigType.VERSION_NUMBER | ||
assert number_node.config_type == ConfigType.NUMBER | ||
assert version_node.config_type == ConfigType.VERSION_NUMBER | ||
assert name_node.config_type == ConfigType.NAME | ||
assert number_node2.config_type == ConfigType.NUMBER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.