From 6909ee7acf32d12ad087311bfbe8a00cd4a53020 Mon Sep 17 00:00:00 2001 From: maurane Date: Wed, 16 Sep 2026 15:31:33 +0200 Subject: [PATCH] =?UTF-8?q?style(tests):=20=F0=9F=93=9D=20clean=20`test=5F?= =?UTF-8?q?cli.py`=20docstrings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_cli.py | 124 +++++++++++++++++----------------------------- 1 file changed, 46 insertions(+), 78 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index cca3311..7d613ed 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -12,22 +12,18 @@ runner = CliRunner() @pytest.fixture(name="output_path") def get_output_path_fixture(tmp_path: Path): - """ - Fixture that return output directory path where all logs and output files - will be located during tests. + """Return output directory path of all logs and output files will be located during tests. Args: tmp_path: Temporary root path for testing results - Returns: - None """ return tmp_path / "output" def get_argument_from_files(log_dir: Path, output_path: Path) -> list[str]: - """ - Utility function that implement an argument list to call cli + """Return an argument list to call cli. + Args: log_dir: Path to the logwatcher directory 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(): - """ - Test the help command and assert that all options are listed - - Returns: - None - """ + """Test the help command and assert that all options are listed.""" results = runner.invoke(app, ["--help"]) assert results.exit_code == 0 assert "--verbose" in results.output @@ -62,26 +53,18 @@ def test_cli_help(): def test_version_cli(): - """ - Test the version option. The version must be printed - - Returns: - None - """ + """Test the version option. The version must be printed.""" result = runner.invoke(app, ["--version"]) assert result.exit_code == 0 assert f"logwatcher version: {__version__}" in result.output 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: tmp_path: Temporary root path for testing results - Returns: - None """ # set up files 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): - """ - 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. Args: tmp_path: Temporary root path for testing results - Returns: - None """ with pytest.raises(FileNotFoundError): _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): - """ - 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: tmp_path: Temporary root path for testing results - Returns: - None """ # create directory 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"} -def test_cli_input_files_and_input_dir(output_path: Path): - """ - Assert that input files and input dir options cannot be provided in the same command. +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. Args: - tmp_path: Temporary root path for testing results + output_path: directory containing all files generated by the process - Returns: - None """ results = runner.invoke( app, @@ -175,32 +149,28 @@ def test_cli_input_files_and_input_dir(output_path: Path): assert not output_path.exists() -def test_cli_neither_input_files_nor_input_dir(): - """ - Test calling cli without providing input files or input dir options. +def test_cli_from_files_neither_input_files_nor_input_dir(): + """Test calling cli without providing input files or input dir options. + Must return an error on provided parameters (BadParameter = exit code 2) - Returns: - None """ results = runner.invoke(app, ["from-files"]) assert results.exit_code == 2 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): - """ - Test calling cli with bad input file (directory instead of file) +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). + Must return an exit code at 1 and the ValueError exception raised while `_files_to_log_entries()` was running. Args: tmp_path: Temporary root path for testing results 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) 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 -def test_cli_empty_input_files(output_path: Path, invalid_log_dir: Path): - """ - Test calling cli with empty input files. +def test_cli_from_files_empty_input_files(output_path: Path, invalid_log_dir: Path): + """Test calling cli with empty input files. + Must pass and generate reports that mention reading a file only. All other parameters must be at 0. Args: 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"))) 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 -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 ): - """ - 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: tmp_path: Temporary root path for testing results 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 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" result = runner.invoke( 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 -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 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 output_path: Temporary path for all output generated during testing - Returns: - None """ input_dir = Path(tmp_path / "input_test") 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 -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...), CLI should run without error and creates report files with metrics at 0. Args: 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) 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 -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 should not be at 0. Args: 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) results = runner.invoke(app, arguments) @@ -429,4 +397,4 @@ def test_cli_valid_logs(output_path: Path, valid_log_dir: Path): content = file.read_text(encoding="windows-1252") assert re.search(f"Période\t : {date_pattern} -> {date_pattern}\n", content) assert f"Fichier(s) lu(s)\t : {nb_files}" in content - assert "Nombre total d'erreur(s)\t: 0" not in content + assert "Nombre total d'erreur(s)\t: 0" not in content \ No newline at end of file