Skip to content

Commit 9784905

Browse files
committed
feat: Update module to work with any repo.
Signed-off-by: Mateusz Chrominski <[email protected]>
1 parent 409acc7 commit 9784905

File tree

25 files changed

+335
-526
lines changed

25 files changed

+335
-526
lines changed

README.md

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,67 @@
33
44
# MFD Code Quality
55

6-
## Usage
7-
8-
This module provides set of code quality related methods with corresponding command line commands.
9-
10-
### Available commands
11-
12-
When installing `mfd-code-quality` package all these commands will become available from command line - in the same way as `pip` for example.\
13-
So you can just type `mfd-help` in your terminal without a need to call it from Python.
14-
15-
16-
> `mfd-configure-code-standard`\
17-
> Prepare code standard configuration files into repository and setup pre commit.
6+
This module provides a set of code quality methods.\
7+
Installing this package will also **add the corresponding executable files to your environment**, allowing you to run each code quality check with a simple command-line call.
188

19-
> `mfd-create-config-files`\
20-
> Prepare code standard configuration files into repository without setup of pre commit.\
21-
> Mechanism of creating configs is the same as in 'mfd-configure-code-standard'.
22-
23-
> `mfd-code-standard`\
24-
Run code standard test using ruff (format, check) or flake8. Depending on what is available. It copies configuration files before code standard check and remove their after check.
25-
> It's not required to call `mfd-configure-code-standard` or `mfd-create-config-files` before running this command.
26-
27-
> `mfd-import-testing`\
28-
Run import testing of each Python file to check import problems.
9+
## Usage
2910

30-
> `mfd-system-tests`\
31-
Run system tests.
11+
![](docs/code_quality_demo.gif)
3212

33-
> `mfd-unit-tests`\
34-
Run unittests, print actual coverage, but don't check its value.
13+
When installing `mfd-code-quality` package all these commands will become available from command-line - in the same way as `pip` for example.\
14+
So you can just type i.e. `mfd-help` in your terminal without a need to call it from Python.\
15+
If command requires configuration, such config will be automatically created and removed after.
3516

36-
> `mfd-unit-tests-with-coverage`\
37-
Run unittests and check if diff coverage (new code coverage) is reaching the threshold.
17+
### Available commands
3818

39-
> `mfd-all-checks`\
40-
Run all available checks.
19+
| Command | Description |
20+
|--------------------------------|-----------------------------------------------------------------------------------------------|
21+
| `mfd-help` | Log available commands. |
22+
| `mfd-code-standard` | Check code standard using Ruff (`format`, `check`) or flake8. Depending on what is available. |
23+
| `mfd-code-format` | Format code using `ruff check --fix` and `ruff format`. |
24+
| `mfd-import-tests` | Try to import each Python file to check import problems. |
25+
| `mfd-system-tests` | Run system tests. |
26+
| `mfd-unit-tests` | Run unit tests. |
27+
| `mfd-unit-tests-with-coverage` | Run unittests and check if diff coverage (new code coverage) is reaching the threshold (**80%**). |
28+
| `mfd-all-checks` | Run all available checks. |
29+
30+
### Available arguments (for all commands)
31+
32+
* `-p` / `--project-dir` - path to the root directory (default: current working directory)
33+
34+
* `-v` / `--verbose` - enable verbose output
35+
36+
> [!NOTE]
37+
> All commands are expected to be run from the root directory of the project.\
38+
> Recommended file structure:
39+
> ```
40+
> <project>
41+
> ├── <package>
42+
> │ ├── __init__.py
43+
> │ ├── ...
44+
> ├── tests
45+
> │ ├── system
46+
> │ │ ├── __init__.py
47+
> │ │ └── ...
48+
> │ ├── unit
49+
> │ │ ├── __init__.py
50+
> │ │ └── ...
51+
> │ └── __init__.py
52+
> ├── pyproject.toml
53+
> ├── README.md
54+
> └── ...
55+
> ```
4156
42-
> `mfd-help`\
43-
Log available commands.
57+
### Configuration files
4458
45-
> `mfd-format-code`\
46-
Format code using ruff check --fix and ruff format
59+
We are using 2 configuration files (created/modified/removed automatically):
4760
48-
All commands can be combined with `--project-dir` parameter, which should point to the root directory of your repository.
49-
If this parameter is not given current working directory will be assumed to be root directory.
61+
* `ruff.toml` - for ruff configuration
5062
51-
### Configuration files
52-
We are using 3 configuration files:
53-
- `ruff.toml` - for ruff configuration
54-
- `pyproject.toml` - for project/generic configuration
55-
- `.pre-commit-config.yaml` - for pre-commit configuration
63+
* `pyproject.toml` - for project/generic configuration
5664
5765
### Custom configuration
66+
5867
Some modules have custom configuration files. Files are stored in `mfd_code_quality/code_standard/config_per_module` directory. Configuration files are merged with generic one during configuration process.
5968
6069
## OS supported:

docs/code_quality_demo.gif

12.2 MB
Loading

mfd_code_quality/code_standard/.pre-commit-config.yaml

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

mfd_code_quality/code_standard/checks.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .configure import delete_config_files, create_config_files
1010
from ..utils import get_root_dir, set_up_logging, set_cwd
1111

12-
logger = logging.getLogger(__name__)
12+
logger = logging.getLogger("mfd-code-quality.code_standard")
1313

1414

1515
def _test_flake8() -> bool:
@@ -29,7 +29,10 @@ def _test_ruff_format() -> bool:
2929
:return: True if there is nothing to format, False - otherwise.
3030
"""
3131
logger.info("Checking 'ruff format --check'...")
32-
ruff_format_outcome = run((sys.executable, "-m", "ruff", "format", "--check"), cwd=get_root_dir())
32+
ruff_format_outcome = run(
33+
(sys.executable, "-m", "ruff", "format", "--check"), capture_output=True, text=True, cwd=get_root_dir()
34+
)
35+
logger.info(f"Output: {ruff_format_outcome.stdout.strip()}")
3336
return ruff_format_outcome.returncode == 0
3437

3538

@@ -40,7 +43,8 @@ def _test_ruff_check() -> bool:
4043
:return: True if ruff check did not find any issues, False - otherwise.
4144
"""
4245
logger.info("Checking 'ruff check'...")
43-
ruff_run_outcome = run((sys.executable, "-m", "ruff", "check"), cwd=get_root_dir())
46+
ruff_run_outcome = run((sys.executable, "-m", "ruff", "check"), capture_output=True, text=True, cwd=get_root_dir())
47+
logger.info(f"Output: {ruff_run_outcome.stdout.strip()}")
4448
return ruff_run_outcome.returncode == 0
4549

4650

@@ -53,7 +57,7 @@ def _get_available_code_standard_module() -> str:
5357
:return: flake8 or ruff
5458
:raises Exception: When no code standard module is available
5559
"""
56-
code_standard_modules = ["flake8", "ruff"]
60+
code_standard_modules = ["ruff", "flake8"]
5761
pip_list = run((sys.executable, "-m", "pip", "list"), capture_output=True, text=True, cwd=get_root_dir())
5862
for code_standard_module in code_standard_modules:
5963
if f"{code_standard_module} " in pip_list.stdout:
@@ -78,7 +82,7 @@ def _run_code_standard_tests(with_configs: bool = True) -> bool:
7882
code_standard_module = _get_available_code_standard_module()
7983
if code_standard_module == "ruff":
8084
if with_configs:
81-
logger.info("Prepare configuration files required for checks.")
85+
logger.debug("Prepare configuration files required for checks.")
8286
create_config_files()
8387
results.append(_test_ruff_format())
8488
results.append(_test_ruff_check())
@@ -91,15 +95,15 @@ def _run_code_standard_tests(with_configs: bool = True) -> bool:
9195
else:
9296
if code_standard_module == "ruff":
9397
logger.info(
94-
"Ruff check was called correctly, however check failed. "
95-
"For fixing code standard call 'mfd-format-code' first."
96-
"\nIf you want to see more details what is wrong, call 'ruff format --diff'.\n"
98+
"Ruff check was called correctly, however check failed.\n"
99+
"For fixing code standard call 'mfd-code-format' first.\n"
100+
"If you want to see more details what is wrong, call 'ruff format --diff'.\n"
97101
)
98102
message = "Code standard check FAILED."
99103
logger.info(message)
100104
finally:
101105
if code_standard_module == "ruff" and with_configs:
102-
logger.info("Delete configuration files.")
106+
logger.debug("Delete configuration files.")
103107
delete_config_files()
104108

105109
return return_val

0 commit comments

Comments
 (0)