From be764296f993faf3f17890b732f3c3381cae5223 Mon Sep 17 00:00:00 2001 From: Martin Hoyer Date: Fri, 16 Aug 2024 13:55:33 +0200 Subject: [PATCH] Addressing issues found by Ruff --- src/tmt_web/api.py | 4 ++-- src/tmt_web/service.py | 10 +++++----- src/tmt_web/utils/git_handler.py | 6 +++--- tests/test_api.py | 15 ++++++++------- tests/unit/test_git_handler.py | 27 ++++++++++++++------------- tests/unit/test_html_generator.py | 4 ++-- tests/unit/test_json_generator.py | 8 ++++---- tests/unit/test_yaml_generator.py | 8 ++++---- 8 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/tmt_web/api.py b/src/tmt_web/api.py index 35f3b94..9037ab7 100644 --- a/src/tmt_web/api.py +++ b/src/tmt_web/api.py @@ -7,8 +7,8 @@ from pydantic import BaseModel from starlette.responses import HTMLResponse -from src import service -from src.generators import html_generator +from tmt_web import service +from tmt_web.generators import html_generator app = FastAPI() format_html = False diff --git a/src/tmt_web/service.py b/src/tmt_web/service.py index aebbea7..eccbf2a 100644 --- a/src/tmt_web/service.py +++ b/src/tmt_web/service.py @@ -5,9 +5,9 @@ from celery.app import Celery from tmt.utils import Path -from src.generators import html_generator as html -from src.generators import json_generator, yaml_generator -from src.utils import git_handler as utils +from tmt_web.generators import html_generator as html +from tmt_web.generators import json_generator, yaml_generator +from tmt_web.utils import git_handler as utils logger = tmt.Logger(logging.getLogger("tmt-logger")) @@ -178,9 +178,9 @@ def main(test_url: str | None, logger.print("Starting...", color="blue") if test_name is not None and plan_name is None: return process_test_request(test_url, test_name, test_ref, test_path, True, out_format) - elif plan_name is not None and test_name is None: + if plan_name is not None and test_name is None: return process_plan_request(plan_url, plan_name, plan_ref, plan_path, True, out_format) - elif plan_name is not None and test_name is not None: + if plan_name is not None and test_name is not None: return process_testplan_request(test_url, test_name, test_ref, test_path, plan_url, plan_name, plan_ref, plan_path, out_format) return None diff --git a/src/tmt_web/utils/git_handler.py b/src/tmt_web/utils/git_handler.py index e77e04d..5574619 100644 --- a/src/tmt_web/utils/git_handler.py +++ b/src/tmt_web/utils/git_handler.py @@ -26,7 +26,7 @@ def checkout_branch(path: Path, logger: Logger, ref: str) -> None: def clone_repository(url: str, logger: Logger, ref: str) -> None: """ Clones the repository from the given URL. - Raises FileExistsError if the repository is already cloned and raises Exception if the cloning fails. + Raises FileExistsError if the repository is already cloned or Exception if the cloning fails. :param ref: Name of the ref to check out :param url: URL to the repository :param logger: Instance of Logger @@ -63,7 +63,7 @@ def get_path_to_repository(url: str) -> Path: :return: Path to the cloned repository """ repo_name = url.rstrip('/').rsplit('/', 1)[-1] - root_dir = Path(__file__).resolve().parents[2] # going up from src/utils/git_handler.py + root_dir = Path(__file__).resolve().parents[2] # going up from tmt_web/utils/git_handler.py return root_dir / os.getenv("CLONE_DIR_PATH", "./.repos/") / repo_name @@ -83,7 +83,7 @@ def clear_tmp_dir(logger: Logger) -> None: :return: """ logger.print("Clearing the .tmp directory...") - root_dir = Path(__file__).resolve().parents[2] # going up from src/utils/git_handler.py + root_dir = Path(__file__).resolve().parents[2] # going up from tmt_web/utils/git_handler.py path = root_dir / os.getenv("CLONE_DIR_PATH", "./.repos/") try: rmtree(path) diff --git a/tests/test_api.py b/tests/test_api.py index 706b65f..8ec5273 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -2,11 +2,12 @@ import time import pytest -from src.api import app from fastapi.testclient import TestClient +from tmt_web.api import app -@pytest.fixture() + +@pytest.fixture def client(): return TestClient(app) @@ -16,7 +17,7 @@ class TestApi: This class tests the behaviour of the API directly """ @pytest.fixture(autouse=True) - def setup(self): + def _setup(self): os.environ["USE_CELERY"] = "false" def test_basic_test_request_json(self, client): @@ -38,7 +39,7 @@ def test_basic_test_request_html(self, client): data = response.content.decode("utf-8") print(data) assert "500" not in data - assert f'' in data + assert '' in data def test_basic_test_request_yaml(self, client): response = client.get("/?test-url=https://github.com/teemtee/tmt&test-name=/tests/core/smoke&format=yaml") @@ -97,7 +98,7 @@ class TestCelery: This class tests the API with the Celery instance """ @pytest.fixture(autouse=True) - def setup(self): + def _setup(self): os.environ["USE_CELERY"] = "true" def test_basic_test_request(self, client): @@ -114,6 +115,6 @@ def test_basic_test_request(self, client): assert "https://github.com/teemtee/tmt/tree/main/tests/core/smoke/main.fmf" in result break elif json_data["status"] == "FAILURE": - assert False + pytest.fail("status = FAILURE: " + json_data["result"]) else: - assert False + pytest.fail("Unknown status: " + json_data["status"]) diff --git a/tests/unit/test_git_handler.py b/tests/unit/test_git_handler.py index dc1a028..2dc6a53 100644 --- a/tests/unit/test_git_handler.py +++ b/tests/unit/test_git_handler.py @@ -1,16 +1,17 @@ +import contextlib +import logging import os import time from pathlib import Path import pytest import tmt -import logging -from src.utils import git_handler +from tmt_web.utils import git_handler class TestGitHandler: - logger = tmt.Logger(logging.Logger("tmt-logger")) + logger = tmt.Logger(logging.getLogger("tmt-logger")) def test_clear_tmp_dir(self): # Create test directory if it doesn't exist @@ -36,23 +37,23 @@ def test_clone_repository(self): while git_handler.check_if_repository_exists("https://github.com/teemtee/tmt") is True: git_handler.clear_tmp_dir(self.logger) time.sleep(1) - git_handler.clone_repository(url="https://github.com/teemtee/tmt", logger=self.logger, ref="default") + git_handler.clone_repository(url="https://github.com/teemtee/tmt", + logger=self.logger, ref="default") def test_clone_repository_even_if_exists(self): - try: - git_handler.clone_repository(url="https://github.com/teemtee/tmt", logger=self.logger, ref="default") - except FileExistsError: - pass + with contextlib.suppress(FileExistsError): + git_handler.clone_repository(url="https://github.com/teemtee/tmt", + logger=self.logger, ref="default") def test_clone_checkout_branch(self): - try: - git_handler.clone_repository(url="https://github.com/teemtee/tmt", logger=self.logger, ref="quay") - except FileExistsError: - pass + with contextlib.suppress(FileExistsError): + git_handler.clone_repository(url="https://github.com/teemtee/tmt", + logger=self.logger, ref="quay") def test_clone_checkout_branch_exception(self): with pytest.raises(AttributeError): - git_handler.clone_repository(url="https://github.com/teemtee/tmt", logger=self.logger, ref="quadd") + git_handler.clone_repository(url="https://github.com/teemtee/tmt", + logger=self.logger, ref="quadd") def test_checkout_branch(self): self.test_clone_repository_even_if_exists() diff --git a/tests/unit/test_html_generator.py b/tests/unit/test_html_generator.py index 824dfd3..757e672 100644 --- a/tests/unit/test_html_generator.py +++ b/tests/unit/test_html_generator.py @@ -2,12 +2,12 @@ import tmt -from src.generators import html_generator +from tmt_web.generators import html_generator class TestHtmlGenerator: def test_generate_test_html(self): - logger = tmt.Logger(logging.Logger("tmt-logger")) + logger = tmt.Logger(logging.getLogger("tmt-logger")) test = tmt.Tree(logger=logger).tests(names=["/tests/objects/sample_test"])[0] data = html_generator.generate_test_html_page(test, logger) assert 'name: /tests/objects/sample_test
' in data diff --git a/tests/unit/test_json_generator.py b/tests/unit/test_json_generator.py index ffdde83..3b98fab 100644 --- a/tests/unit/test_json_generator.py +++ b/tests/unit/test_json_generator.py @@ -2,24 +2,24 @@ import tmt -from src.generators import json_generator +from tmt_web.generators import json_generator class TestJsonGenerator: def test_generate_test_json(self): - logger = tmt.Logger(logging.Logger("tmt-logger")) + logger = tmt.Logger(logging.getLogger("tmt-logger")) test = tmt.Tree(logger=logger).tests(names=["/tests/objects/sample_test"])[0] data = json_generator.generate_test_json(test, logger) assert '"name": "/tests/objects/sample_test"' in data def test_generate_plan_json(self): - logger = tmt.Logger(logging.Logger("tmt-logger")) + logger = tmt.Logger(logging.getLogger("tmt-logger")) plan = tmt.Tree(logger=logger).plans(names=["/tests/objects/sample_plan"])[0] data = json_generator.generate_plan_json(plan, logger) assert '"name": "/tests/objects/sample_plan"' in data def test_generate_testplan_json(self): - logger = tmt.Logger(logging.Logger("tmt-logger")) + logger = tmt.Logger(logging.getLogger("tmt-logger")) test = tmt.Tree(logger=logger).tests(names=["/tests/objects/sample_test"])[0] plan = tmt.Tree(logger=logger).plans(names=["/tests/objects/sample_plan"])[0] data = json_generator.generate_testplan_json(test, plan, logger) diff --git a/tests/unit/test_yaml_generator.py b/tests/unit/test_yaml_generator.py index 357d3d7..cb62d04 100644 --- a/tests/unit/test_yaml_generator.py +++ b/tests/unit/test_yaml_generator.py @@ -2,24 +2,24 @@ import tmt -from src.generators import yaml_generator +from tmt_web.generators import yaml_generator class TestYamlGenerator: def test_generate_test_yaml(self): - logger = tmt.Logger(logging.Logger("tmt-logger")) + logger = tmt.Logger(logging.getLogger("tmt-logger")) test = tmt.Tree(logger=logger).tests(names=["/tests/objects/sample_test"])[0] data = yaml_generator.generate_test_yaml(test, logger) assert 'name: /tests/objects/sample_test' in data def test_generate_plan_yaml(self): - logger = tmt.Logger(logging.Logger("tmt-logger")) + logger = tmt.Logger(logging.getLogger("tmt-logger")) plan = tmt.Tree(logger=logger).plans(names=["/tests/objects/sample_plan"])[0] data = yaml_generator.generate_plan_yaml(plan, logger) assert 'name: /tests/objects/sample_plan' in data def test_generate_testplan_yaml(self): - logger = tmt.Logger(logging.Logger("tmt-logger")) + logger = tmt.Logger(logging.getLogger("tmt-logger")) test = tmt.Tree(logger=logger).tests(names=["/tests/objects/sample_test"])[0] plan = tmt.Tree(logger=logger).plans(names=["/tests/objects/sample_plan"])[0] data = yaml_generator.generate_testplan_yaml(test, plan, logger)