aboutsummaryrefslogtreecommitdiff
path: root/tests/test_core_logging.py
diff options
context:
space:
mode:
authorDiego Russo <diego.russo@arm.com>2023-02-02 22:04:05 +0000
committerDiego Russo <diego.russo@arm.com>2023-02-03 17:48:11 +0000
commitf1eaff3c9790464bed3183ff76555cf815166f50 (patch)
treebba288bb051925a692e1e1f998f7cc48df755df6 /tests/test_core_logging.py
parentd1b2374cda6811a93d1174400fc2eecd7100a8c3 (diff)
downloadmlia-f1eaff3c9790464bed3183ff76555cf815166f50.tar.gz
MLIA-782 Remove --output parameter
* Remove --output parameter from argument parser * Remove FormattedFilePath class and its presence across the codebase * Move logging module from cli to core * The output format is now injected in the execution context and used across MLIA * Depending on the output format, TextReporter and JSONReporter have been created and used accordingly. * The whole output to standard output and/or logfile is driven via the logging module: the only case where the print is used is when the --json parameter is specified. This is needed becase all output (including third party application as well) needs to be disabled otherwise it might corrupt the json output in the standard output. * Debug information is logged into the log file and printed to stdout when the output format is plain_text. * Update E2E test and config to cope with the new mechanism of outputting json data to standard output. Change-Id: I4395800b0b1af4d24406a828d780bdeef98cd413
Diffstat (limited to 'tests/test_core_logging.py')
-rw-r--r--tests/test_core_logging.py106
1 files changed, 106 insertions, 0 deletions
diff --git a/tests/test_core_logging.py b/tests/test_core_logging.py
new file mode 100644
index 0000000..e021e26
--- /dev/null
+++ b/tests/test_core_logging.py
@@ -0,0 +1,106 @@
+# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates.
+# SPDX-License-Identifier: Apache-2.0
+"""Tests for the module cli.logging."""
+from __future__ import annotations
+
+import logging
+from pathlib import Path
+
+import pytest
+
+from mlia.core.logging import setup_logging
+from tests.utils.logging import clear_loggers
+
+
+def teardown_function() -> None:
+ """Perform action after test completion.
+
+ This function is launched automatically by pytest after each test
+ in this module.
+ """
+ clear_loggers()
+
+
+@pytest.mark.parametrize(
+ "logs_dir, verbose, expected_output, expected_log_file_content",
+ [
+ (
+ None,
+ None,
+ "cli info\n",
+ None,
+ ),
+ (
+ None,
+ True,
+ """mlia.backend.manager - DEBUG - backends debug
+mlia.cli - INFO - cli info
+mlia.cli - DEBUG - cli debug
+""",
+ None,
+ ),
+ (
+ "logs",
+ True,
+ """mlia.backend.manager - DEBUG - backends debug
+mlia.cli - INFO - cli info
+mlia.cli - DEBUG - cli debug
+""",
+ """mlia.backend.manager - DEBUG - backends debug
+mlia.cli - INFO - cli info
+mlia.cli - DEBUG - cli debug
+""",
+ ),
+ ],
+)
+def test_setup_logging(
+ tmp_path: Path,
+ capfd: pytest.CaptureFixture,
+ logs_dir: str,
+ verbose: bool,
+ expected_output: str,
+ expected_log_file_content: str,
+) -> None:
+ """Test function setup_logging."""
+ logs_dir_path = tmp_path / logs_dir if logs_dir else None
+
+ setup_logging(logs_dir_path, verbose)
+
+ backend_logger = logging.getLogger("mlia.backend.manager")
+ backend_logger.debug("backends debug")
+
+ cli_logger = logging.getLogger("mlia.cli")
+ cli_logger.info("cli info")
+ cli_logger.debug("cli debug")
+
+ stdout, _ = capfd.readouterr()
+ assert stdout == expected_output
+
+ check_log_assertions(logs_dir_path, expected_log_file_content)
+
+
+def check_log_assertions(
+ logs_dir_path: Path | None, expected_log_file_content: str
+) -> None:
+ """Test assertions for log file."""
+ if logs_dir_path is not None:
+ assert logs_dir_path.is_dir()
+
+ items = list(logs_dir_path.iterdir())
+ assert len(items) == 1
+
+ log_file_path = items[0]
+ assert log_file_path.is_file()
+
+ log_file_name = log_file_path.name
+ assert log_file_name == "mlia.log"
+
+ with open(log_file_path, encoding="utf-8") as log_file:
+ log_content = log_file.read()
+
+ expected_lines = expected_log_file_content.split("\n")
+ produced_lines = log_content.split("\n")
+
+ assert len(expected_lines) == len(produced_lines)
+ for expected, produced in zip(expected_lines, produced_lines):
+ assert expected in produced