From c052a0b4007a8d814525bf5766bd08da38ca231d Mon Sep 17 00:00:00 2001 From: Roman <34835155+CuberHuber@users.noreply.github.com> Date: Fri, 24 Jan 2025 11:43:44 +0300 Subject: [PATCH] Merge pull request #56 from S3-Platform-Inc/feature/55-the-badge-test closes #55 feature(tests): added test for GitHub Badges in readme.md (#55) --- tests/fixtures/plugin_manifest.py | 28 ++++++++++++++++++++ tests/test_plugin_readme.py | 34 ++++++++++++++++++++++++ tests/test_plugin_root_file.py | 35 +++++++++++++++++++++++++ tests/test_plugin_root_files.py | 43 ------------------------------- 4 files changed, 97 insertions(+), 43 deletions(-) create mode 100644 tests/fixtures/plugin_manifest.py create mode 100644 tests/test_plugin_readme.py create mode 100644 tests/test_plugin_root_file.py delete mode 100644 tests/test_plugin_root_files.py diff --git a/tests/fixtures/plugin_manifest.py b/tests/fixtures/plugin_manifest.py new file mode 100644 index 0000000..82a10db --- /dev/null +++ b/tests/fixtures/plugin_manifest.py @@ -0,0 +1,28 @@ +from pathlib import Path + +import xml.etree.ElementTree as ET +from s3p_sdk.types.manifest import Manifest + +import pytest + + +ROOT_TAG: str = "project" +PROJECT_NAME_TAG: str = "name" +PROJECT_VERSION_TAG: str = "version" +XML_FILENAME: str = "plugin.xml" + + +@pytest.fixture(scope="session", autouse=True) +def fix_plugin_root_config_path() -> Path: + return Path(__file__).parent.parent.parent / XML_FILENAME + + +@pytest.fixture(scope="session", autouse=True) +def fix_plugin_manifest(fix_plugin_root_config_path) -> Manifest: + tree = ET.parse(str(fix_plugin_root_config_path)) + _version = tree.getroot().find(PROJECT_VERSION_TAG).text + _name = tree.getroot().attrib.get(PROJECT_NAME_TAG) + return Manifest( + version=_version, + plugin_name=_name, + ) diff --git a/tests/test_plugin_readme.py b/tests/test_plugin_readme.py new file mode 100644 index 0000000..09285ae --- /dev/null +++ b/tests/test_plugin_readme.py @@ -0,0 +1,34 @@ +import re +from pathlib import Path + +from tests.fixtures.plugin_manifest import fix_plugin_manifest, fix_plugin_root_config_path +import pytest + + +@pytest.mark.pre_set +class TestPluginREADME: + + @pytest.fixture(scope="class", autouse=True) + def readme_content(self) -> str: + with open(Path(__file__).parent.parent / 'readme.md', 'r') as file: + readme_content = file.read() + return readme_content + + def test_badges_name(self, fix_plugin_manifest, readme_content): + """Проверка соответствие имен репозитория в ссылках на шильдики GitHub""" + + print(fix_plugin_manifest.plugin_name) + badge_pattern = rf'\[\!\[.*?\]\(https://github\.com/S3-Platform-Inc/{fix_plugin_manifest.plugin_name.replace("_", "-")}/actions/workflows/.*?\.yml/badge\.svg\)\]' + + found_lines = re.findall(badge_pattern, readme_content, re.MULTILINE) + assert len(found_lines) == 3, "Обновите readme.md файл. Укажите валидный url для GitHub Badges" + + def test_title(self, fix_plugin_manifest, readme_content): + title_pattern = r'^# S3 Platform Plugin Template' + + matched = re.match(title_pattern, readme_content) + + if fix_plugin_manifest.plugin_name == "s3_platform_plugin_template": + assert matched + else: + assert not matched, "Измените название плагина в readme.md файле" diff --git a/tests/test_plugin_root_file.py b/tests/test_plugin_root_file.py new file mode 100644 index 0000000..7d22300 --- /dev/null +++ b/tests/test_plugin_root_file.py @@ -0,0 +1,35 @@ +import os +import re + +import xml.etree.ElementTree as ET +from tests.fixtures.plugin_manifest import fix_plugin_root_config_path, XML_FILENAME, ROOT_TAG, PROJECT_NAME_TAG, \ + PROJECT_VERSION_TAG + +import pytest + + +@pytest.mark.pre_set +class TestPluginRootFile: + + def test_check_plugin_xml_file(self, fix_plugin_root_config_path): + """Наличие главного файла плагина""" + assert os.path.exists(str(fix_plugin_root_config_path)), f"S3P plugin должен содержать файл `{XML_FILENAME}` в корне репозитория" + + def test_check_plugin_xml_structure(self, fix_plugin_root_config_path): + """Проверяет структуру плагина""" + tree = ET.parse(str(fix_plugin_root_config_path)) + assert tree.getroot().tag == ROOT_TAG,\ + f"Не найден тег `{ROOT_TAG}` в корне `{XML_FILENAME}`" + assert tree.getroot().attrib.get(PROJECT_NAME_TAG), \ + f"Не найдено поле `{PROJECT_NAME_TAG}` в теги `{ROOT_TAG}`" + assert tree.getroot().find(PROJECT_VERSION_TAG).tag == PROJECT_VERSION_TAG,\ + f"Не найден тег `{PROJECT_VERSION_TAG}` в теги `{ROOT_TAG}`" + + def test_format_version_check(self, fix_plugin_root_config_path): + """Проверяет формат версии плагина""" + pattern = r'^(0*[1-9]\d*|0*\d+\.\d+)$' + + tree = ET.parse(str(fix_plugin_root_config_path)) + _version = tree.getroot().find(PROJECT_VERSION_TAG).text + assert re.match(pattern, _version), \ + f"{_version} не соответствует шаблону версий. см. в документации" diff --git a/tests/test_plugin_root_files.py b/tests/test_plugin_root_files.py deleted file mode 100644 index 72def80..0000000 --- a/tests/test_plugin_root_files.py +++ /dev/null @@ -1,43 +0,0 @@ -import importlib.util -import os -import re -from pathlib import Path - -import xml.etree.ElementTree as ET - -import pytest - - -@pytest.mark.pre_set -class TestPluginRootFiles: - ROOT_TAG: str = "project" - PROJECT_NAME_TAG: str = "name" - PROJECT_VERSION_TAG: str = "version" - XML_FILENAME: str = "plugin.xml" - - @pytest.fixture(autouse=True) - def plugin_root_config(self) -> Path: - return Path(__file__).parent.parent / self.XML_FILENAME - - def test_check_plugin_xml_file(self, plugin_root_config): - """Наличие главного файла плагина""" - assert os.path.exists(str(plugin_root_config)), f"S3P plugin должен содержать файл `{self.XML_FILENAME}` в корне репозитория" - - def test_check_plugin_xml_structure(self, plugin_root_config): - """Проверяет структуру плагина""" - tree = ET.parse(str(plugin_root_config)) - assert tree.getroot().tag == self.ROOT_TAG,\ - f"Не найден тег `{self.ROOT_TAG}` в корне `{self.XML_FILENAME}`" - assert tree.getroot().attrib.get(self.PROJECT_NAME_TAG), \ - f"Не найдено поле `{self.PROJECT_NAME_TAG}` в теги `{self.ROOT_TAG}`" - assert tree.getroot().find(self.PROJECT_VERSION_TAG).tag == self.PROJECT_VERSION_TAG,\ - f"Не найден тег `{self.PROJECT_VERSION_TAG}` в теги `{self.ROOT_TAG}`" - - def test_format_version_check(self, plugin_root_config): - """Проверяет формат версии плагина""" - pattern = r'^(0*[1-9]\d*|0*\d+\.\d+)$' - - tree = ET.parse(str(plugin_root_config)) - _version = tree.getroot().find(self.PROJECT_VERSION_TAG).text - assert re.match(pattern, _version), \ - f"{_version} не соответствует шаблону версий. см. в документации"