Skip to content

Commit

Permalink
Addressing issues found by Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhoyer committed Aug 16, 2024
1 parent 49081fb commit be76429
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/tmt_web/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/tmt_web/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/tmt_web/utils/git_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand All @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand All @@ -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'<html>' in data
assert '<html>' 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")
Expand Down Expand Up @@ -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):
Expand All @@ -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"])
27 changes: 14 additions & 13 deletions tests/unit/test_git_handler.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_html_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<br>' in data
8 changes: 4 additions & 4 deletions tests/unit/test_json_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_yaml_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit be76429

Please sign in to comment.