Skip to content

Commit 17cb83b

Browse files
authored
simplify setting log level and use sys.exit(1) on failure (#101)
* simplify setting log level and use sys.exit(1) on failure * set debug log if verbose argument is passed, remove exceptions use sys.exit(1) * add comment as adviced
1 parent bba9a5e commit 17cb83b

File tree

5 files changed

+26
-32
lines changed

5 files changed

+26
-32
lines changed

apps/polarion/exceptions.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

apps/polarion/polarion_set_automated.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
import logging
33
import os
44
from simple_logger.logger import get_logger
5-
5+
import sys
66
import click
77

8-
from apps.polarion.exceptions import PolarionTestCaseApprovalError
98
from apps.polarion.polarion_utils import get_polarion_project_id, find_polarion_ids, update_polarion_ids
109
from typing import List, Dict, Optional
1110

@@ -48,16 +47,18 @@ def remove_approved_tests(
4847
@click.option("--verbose", default=False, is_flag=True)
4948
def polarion_approve_automate(config_file_path: str, project_id: str, branch: str, verbose: bool) -> None:
5049
if verbose:
51-
LOGGER.setLevel(logging.INFO)
52-
else:
53-
logging.disable(logging.ERROR)
50+
LOGGER.setLevel(logging.DEBUG)
51+
# since the utilities are in apps.polarion.polarion_utils, we need to change log level
52+
# for apps.polarion.polarion_utils as well
53+
logging.getLogger("apps.polarion.polarion_utils").setLevel(logging.DEBUG)
54+
5455
polarion_project_id = project_id or get_polarion_project_id(
5556
config_file_path=config_file_path, util_name="pyutils-polarion-set-automated"
5657
)
5758
added_polarions = {}
5859
if added_ids := find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="added", branch=branch):
5960
added_polarions = approve_tests(polarion_project_id=polarion_project_id, added_ids=added_ids)
60-
LOGGER.info(f"Following polarion ids were marked automated and approved: {added_polarions.get('updated')}")
61+
LOGGER.debug(f"Following polarion ids were marked automated and approved: {added_polarions.get('updated')}")
6162

6263
removed_polarions = remove_approved_tests(
6364
polarion_project_id=polarion_project_id, added_ids=added_ids, branch=branch
@@ -69,7 +70,7 @@ def polarion_approve_automate(config_file_path: str, project_id: str, branch: st
6970
if added_polarions.get("failed"):
7071
error += f" Added ids:: {added_polarions.get('failed')}."
7172
LOGGER.error(error)
72-
raise PolarionTestCaseApprovalError(error)
73+
sys.exit(1)
7374

7475

7576
if __name__ == "__main__":

apps/polarion/polarion_utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22
import re
3-
4-
import click
3+
import sys
54
from simple_logger.logger import get_logger
65
import shlex
76
import subprocess
@@ -23,7 +22,7 @@ def git_diff(branch: str) -> str:
2322
def git_diff_lines(branch: str) -> Dict[str, List[str]]:
2423
diff: Dict[str, List[str]] = {}
2524
for line in git_diff(branch=branch).splitlines():
26-
LOGGER.info(line)
25+
LOGGER.debug(line)
2726
if line.startswith("+"):
2827
diff.setdefault("added", []).append(line)
2928
if line.startswith("-"):
@@ -42,7 +41,7 @@ def validate_polarion_requirements(
4241

4342
for _id in polarion_test_ids:
4443
has_req = False
45-
LOGGER.info(f"Checking if {_id} verifies any requirement")
44+
LOGGER.debug(f"Checking if {_id} verifies any requirement")
4645
tc = TestCase(project_id=polarion_project_id, work_item_id=_id)
4746
for link in tc.linked_work_items:
4847
try:
@@ -70,7 +69,7 @@ def get_polarion_project_id(util_name: str, config_file_path: str) -> str:
7069
polarion_project_id = get_util_config(util_name=util_name, config_file_path=config_file_path).get("project_id")
7170
if not polarion_project_id:
7271
LOGGER.error("Polarion project id must be passed via config file or command line")
73-
raise click.Abort()
72+
sys.exit(1)
7473
return polarion_project_id
7574

7675

@@ -91,7 +90,7 @@ def update_polarion_ids(
9190
if is_approved:
9291
tc.status = APPROVED
9392
tc.update()
94-
LOGGER.info(f"Polarion {id}: marked as: {automation_status}, approved status set: {is_approved}")
93+
LOGGER.debug(f"Polarion {id}: marked as: {automation_status}, approved status set: {is_approved}")
9594
updated_ids.setdefault("updated", []).append(id)
9695
except PyleroLibException as polarion_exception:
9796
error = f"{id}: {polarion_exception}"

apps/polarion/polarion_verify_tc_requirements.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from simple_logger.logger import get_logger
33
import os
44
import click
5+
import sys
56

6-
from apps.polarion.exceptions import PolarionTestCaseWithoutRequirementError
77
from apps.polarion.polarion_utils import validate_polarion_requirements, find_polarion_ids, get_polarion_project_id
88

99
LOGGER = get_logger(name="polarion-verify-tc-requirements")
@@ -21,21 +21,22 @@
2121
@click.option("--verbose", default=False, is_flag=True)
2222
def has_verify(config_file_path: str, project_id: str, branch: str, verbose: bool) -> None:
2323
if verbose:
24-
LOGGER.setLevel(logging.INFO)
25-
else:
26-
logging.disable(logging.ERROR)
24+
LOGGER.setLevel(logging.DEBUG)
25+
# since the utilities are in apps.polarion.polarion_utils, we need to change log level
26+
# for apps.polarion.polarion_utils as well
27+
logging.getLogger("apps.polarion.polarion_utils").setLevel(logging.DEBUG)
28+
2729
polarion_project_id = project_id or get_polarion_project_id(
2830
config_file_path=config_file_path, util_name="pyutils-polarion-verify-tc-requirements"
2931
)
3032
if added_ids := find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="added", branch=branch):
31-
LOGGER.info(f"Checking following ids: {added_ids}")
33+
LOGGER.debug(f"Checking following ids: {added_ids}")
3234
if tests_with_missing_requirements := validate_polarion_requirements(
3335
polarion_test_ids=added_ids,
3436
polarion_project_id=polarion_project_id,
3537
):
36-
raise PolarionTestCaseWithoutRequirementError(
37-
f"TestCases with missing requirement: {tests_with_missing_requirements}"
38-
)
38+
LOGGER.error(f"TestCases with missing requirement: {tests_with_missing_requirements}")
39+
sys.exit(1)
3940

4041

4142
if __name__ == "__main__":

apps/unused_code/unused_code.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import logging
33
import os
44
import subprocess
5+
import sys
6+
57
import click
68
from simple_logger.logger import get_logger
79

@@ -69,10 +71,7 @@ def is_ignore_function_list(ignore_prefix_list: List[str], function: ast.Functio
6971
def get_unused_functions(
7072
config_file_path: Any, exclude_files: Any, exclude_function_prefixes: Any, verbose: bool
7173
) -> Any:
72-
if verbose:
73-
LOGGER.setLevel(logging.DEBUG)
74-
else:
75-
logging.disable(logging.CRITICAL)
74+
LOGGER.setLevel(logging.DEBUG if verbose else logging.INFO)
7675

7776
_unused_functions = []
7877
unused_code_config = get_util_config(util_name="pyutils-unusedcode", config_file_path=config_file_path)
@@ -104,7 +103,7 @@ def get_unused_functions(
104103
)
105104
if _unused_functions:
106105
click.echo("\n".join(_unused_functions))
107-
raise click.Abort()
106+
sys.exit(1)
108107

109108

110109
if __name__ == "__main__":

0 commit comments

Comments
 (0)