style(tests): 📝 clean test_cli.py docstrings

This commit is contained in:
2026-09-16 15:31:33 +02:00
parent cf09ab0c6a
commit 6909ee7acf
+45 -77
View File
@@ -12,22 +12,18 @@ runner = CliRunner()
@pytest.fixture(name="output_path") @pytest.fixture(name="output_path")
def get_output_path_fixture(tmp_path: Path): def get_output_path_fixture(tmp_path: Path):
""" """Return output directory path of all logs and output files will be located during tests.
Fixture that return output directory path where all logs and output files
will be located during tests.
Args: Args:
tmp_path: Temporary root path for testing results tmp_path: Temporary root path for testing results
Returns:
None
""" """
return tmp_path / "output" return tmp_path / "output"
def get_argument_from_files(log_dir: Path, output_path: Path) -> list[str]: def get_argument_from_files(log_dir: Path, output_path: Path) -> list[str]:
""" """Return an argument list to call cli.
Utility function that implement an argument list to call cli
Args: Args:
log_dir: Path to the logwatcher directory log_dir: Path to the logwatcher directory
output_path: Temporary path for all output generated during testing output_path: Temporary path for all output generated during testing
@@ -47,12 +43,7 @@ def get_argument_from_files(log_dir: Path, output_path: Path) -> list[str]:
def test_cli_help(): def test_cli_help():
""" """Test the help command and assert that all options are listed."""
Test the help command and assert that all options are listed
Returns:
None
"""
results = runner.invoke(app, ["--help"]) results = runner.invoke(app, ["--help"])
assert results.exit_code == 0 assert results.exit_code == 0
assert "--verbose" in results.output assert "--verbose" in results.output
@@ -62,26 +53,18 @@ def test_cli_help():
def test_version_cli(): def test_version_cli():
""" """Test the version option. The version must be printed."""
Test the version option. The version must be printed
Returns:
None
"""
result = runner.invoke(app, ["--version"]) result = runner.invoke(app, ["--version"])
assert result.exit_code == 0 assert result.exit_code == 0
assert f"logwatcher version: {__version__}" in result.output assert f"logwatcher version: {__version__}" in result.output
def test_is_log_file(tmp_path: Path): def test_is_log_file(tmp_path: Path):
""" """Assert only .log, .txt or file without extensions are valid.
Assert only .log, .txt or file without extensions are valid
Args: Args:
tmp_path: Temporary root path for testing results tmp_path: Temporary root path for testing results
Returns:
None
""" """
# set up files # set up files
Path(tmp_path / "a.log").touch() Path(tmp_path / "a.log").touch()
@@ -101,16 +84,13 @@ def test_is_log_file(tmp_path: Path):
def test_get_files_from_dir_empty_dir(tmp_path: Path): def test_get_files_from_dir_empty_dir(tmp_path: Path):
""" """Assert that a non-existing directory (or wrong path) cannot be given as a working input directory.
Assert that a non-existing directory (or wrong path) cannot be
given as a working input directory.
Assert that everything works even if the directory is empty. Assert that everything works even if the directory is empty.
Args: Args:
tmp_path: Temporary root path for testing results tmp_path: Temporary root path for testing results
Returns:
None
""" """
with pytest.raises(FileNotFoundError): with pytest.raises(FileNotFoundError):
_get_files_from_dir(tmp_path / "input_test") _get_files_from_dir(tmp_path / "input_test")
@@ -121,14 +101,11 @@ def test_get_files_from_dir_empty_dir(tmp_path: Path):
def test_get_files_from_dir_not_empty_dir(tmp_path: Path): def test_get_files_from_dir_not_empty_dir(tmp_path: Path):
""" """Assert the right files are retrieved if the directory is not empty.
Assert the right files are retrieved if the directory is not empty.
Args: Args:
tmp_path: Temporary root path for testing results tmp_path: Temporary root path for testing results
Returns:
None
""" """
# create directory # create directory
input_test_dir = tmp_path / "input_test" input_test_dir = tmp_path / "input_test"
@@ -148,15 +125,12 @@ def test_get_files_from_dir_not_empty_dir(tmp_path: Path):
assert {file.name for file in file_list} == {"a.log", "a.txt", "a"} assert {file.name for file in file_list} == {"a.log", "a.txt", "a"}
def test_cli_input_files_and_input_dir(output_path: Path): def test_cli_from_files_input_files_and_input_dir(output_path: Path):
""" """Assert that input files and input dir options cannot be provided in the same command.
Assert that input files and input dir options cannot be provided in the same command.
Args: Args:
tmp_path: Temporary root path for testing results output_path: directory containing all files generated by the process
Returns:
None
""" """
results = runner.invoke( results = runner.invoke(
app, app,
@@ -175,32 +149,28 @@ def test_cli_input_files_and_input_dir(output_path: Path):
assert not output_path.exists() assert not output_path.exists()
def test_cli_neither_input_files_nor_input_dir(): def test_cli_from_files_neither_input_files_nor_input_dir():
""" """Test calling cli without providing input files or input dir options.
Test calling cli without providing input files or input dir options.
Must return an error on provided parameters (BadParameter = exit code 2) Must return an error on provided parameters (BadParameter = exit code 2)
Returns:
None
""" """
results = runner.invoke(app, ["from-files"]) results = runner.invoke(app, ["from-files"])
assert results.exit_code == 2 assert results.exit_code == 2
assert "Either input_files or input_dir must be provided" in results.output assert "Either input_files or input_dir must be provided" in results.output
def test_cli_bad_input_file(tmp_path: Path, output_path: Path, invalid_log_dir: Path): def test_cli_from_files_bad_input_file(tmp_path: Path, output_path: Path, invalid_log_dir: Path):
""" """Test calling cli with bad input file (directory instead of file).
Test calling cli with bad input file (directory instead of file)
Must return an exit code at 1 and the ValueError exception raised Must return an exit code at 1 and the ValueError exception raised
while `_files_to_log_entries()` was running. while `_files_to_log_entries()` was running.
Args: Args:
tmp_path: Temporary root path for testing results tmp_path: Temporary root path for testing results
output_path: Temporary path for all output generated during testing output_path: Temporary path for all output generated during testing
invalid_log_dir: Fixture directory of invalid logs invalid_log_dir: Fixture of directory of invalid logs
Returns:
None
""" """
directory = str(tmp_path) directory = str(tmp_path)
results = runner.invoke( results = runner.invoke(
@@ -215,18 +185,16 @@ def test_cli_bad_input_file(tmp_path: Path, output_path: Path, invalid_log_dir:
) # would be created if `directory` was a valid parameter ) # would be created if `directory` was a valid parameter
def test_cli_empty_input_files(output_path: Path, invalid_log_dir: Path): def test_cli_from_files_empty_input_files(output_path: Path, invalid_log_dir: Path):
""" """Test calling cli with empty input files.
Test calling cli with empty input files.
Must pass and generate reports that mention reading a file only. Must pass and generate reports that mention reading a file only.
All other parameters must be at 0. All other parameters must be at 0.
Args: Args:
output_path: Temporary path for all output generated during testing output_path: Temporary path for all output generated during testing
invalid_log_dir: Fixture directory of invalid logs invalid_log_dir: Fixture of directory of invalid logs
Returns:
None
""" """
empty_file = str(next(invalid_log_dir.glob("empty_file.txt"))) empty_file = str(next(invalid_log_dir.glob("empty_file.txt")))
results = runner.invoke( results = runner.invoke(
@@ -245,20 +213,16 @@ def test_cli_empty_input_files(output_path: Path, invalid_log_dir: Path):
assert "Nombre total d'erreur(s)\t: 0" in content assert "Nombre total d'erreur(s)\t: 0" in content
def test_cli_input_files_or_input_dir( def test_cli_from_files_input_files_or_input_dir(
tmp_path: Path, output_path: Path, original_log_dir: Path tmp_path: Path, output_path: Path, original_log_dir: Path
): ):
""" """Assert that calling the CLI with either input files or input dir options is a valid process.
Assert that calling the CLI with either input files or input dir options
is a valid process.
Args: Args:
tmp_path: Temporary root path for testing results tmp_path: Temporary root path for testing results
output_path: Temporary path for all output generated during testing output_path: Temporary path for all output generated during testing
original_log_dir: Fixture directory of initial logs received by n2 technicians original_log_dir: Fixture of directory of initial logs received by n2 technicians
Returns:
None
""" """
# output for input-files option # output for input-files option
input_files_output = output_path / "input_files" input_files_output = output_path / "input_files"
@@ -308,7 +272,14 @@ def test_cli_input_files_or_input_dir(
) )
def test_cli_input_files_one_file(valid_log_dir: Path, output_path: Path): def test_cli_from_files_input_files_one_file(valid_log_dir: Path, output_path: Path):
"""Assert that CLI can analyze a single valid file.
Args:
valid_log_dir: Fixture of directory of valid logs
output_path: Fixture of directory receiving all file generated during process.
"""
mixed_logs_file = valid_log_dir / "mixed_logs.txt" mixed_logs_file = valid_log_dir / "mixed_logs.txt"
result = runner.invoke( result = runner.invoke(
app, app,
@@ -335,8 +306,9 @@ def test_cli_input_files_one_file(valid_log_dir: Path, output_path: Path):
assert "Nombre total d'erreur(s)\t: 0" not in content assert "Nombre total d'erreur(s)\t: 0" not in content
def test_cli_empty_input_directory(tmp_path: Path, output_path: Path): def test_cli_from_files_empty_input_directory(tmp_path: Path, output_path: Path):
""" """Assert CLI can run on empty directory.
Assert that CLI can run on empty directory without error and Assert that CLI can run on empty directory without error and
generates reports that indicates no file read. generates reports that indicates no file read.
@@ -344,8 +316,6 @@ def test_cli_empty_input_directory(tmp_path: Path, output_path: Path):
tmp_path: Temporary root path for testing results tmp_path: Temporary root path for testing results
output_path: Temporary path for all output generated during testing output_path: Temporary path for all output generated during testing
Returns:
None
""" """
input_dir = Path(tmp_path / "input_test") input_dir = Path(tmp_path / "input_test")
input_dir.mkdir(parents=True) input_dir.mkdir(parents=True)
@@ -373,17 +343,16 @@ def test_cli_empty_input_directory(tmp_path: Path, output_path: Path):
assert "Nombre total d'erreur(s)\t: 0" in content assert "Nombre total d'erreur(s)\t: 0" in content
def test_cli_invalid_logs(output_path: Path, invalid_log_dir: Path): def test_cli_from_files_invalid_logs(output_path: Path, invalid_log_dir: Path):
""" """Assert CLI return empty values if logs are invalid.
On invalid logs (empty files, log from different sources than MDC LAME...), On invalid logs (empty files, log from different sources than MDC LAME...),
CLI should run without error and creates report files with metrics at 0. CLI should run without error and creates report files with metrics at 0.
Args: Args:
output_path: Temporary path for all output generated during testing output_path: Temporary path for all output generated during testing
invalid_log_dir: Fixture directory of invalid logs invalid_log_dir: Fixture of directory of invalid logs
Returns:
None
""" """
arguments = get_argument_from_files(invalid_log_dir, output_path) arguments = get_argument_from_files(invalid_log_dir, output_path)
results = runner.invoke(app, arguments) results = runner.invoke(app, arguments)
@@ -402,17 +371,16 @@ def test_cli_invalid_logs(output_path: Path, invalid_log_dir: Path):
assert "Nombre total d'erreur(s)\t: 0" in content assert "Nombre total d'erreur(s)\t: 0" in content
def test_cli_valid_logs(output_path: Path, valid_log_dir: Path): def test_cli_from_files_valid_logs(output_path: Path, valid_log_dir: Path):
""" """Assert everything works when logs are valid.
On valid test, everything should work successfully. Metrics On valid test, everything should work successfully. Metrics
should not be at 0. should not be at 0.
Args: Args:
output_path: Temporary path for all output generated during testing output_path: Temporary path for all output generated during testing
valid_log_dir: Fixture directory of valid logs valid_log_dir: Fixture of directory of valid logs
Returns:
None
""" """
arguments = get_argument_from_files(valid_log_dir, output_path) arguments = get_argument_from_files(valid_log_dir, output_path)
results = runner.invoke(app, arguments) results = runner.invoke(app, arguments)