From 349b1342ebf6a65dff6508d8c0caa427bab9ac25 Mon Sep 17 00:00:00 2001 From: ianhelle Date: Sun, 20 Oct 2024 13:47:14 -0700 Subject: [PATCH] Fixing unit test break for mp_user_session Adding ruff to pre-commit-config.yaml --- .pre-commit-config.yaml | 14 ++++++--- conda/conda-reqs-dev.txt | 1 + msticpy/init/mp_user_session.py | 2 +- requirements-dev.txt | 1 + tests/init/test_mp_user_session.py | 46 ++++++++---------------------- 5 files changed, 25 insertions(+), 39 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ea67846b0..a794c8889 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -41,10 +41,16 @@ repos: - id: pydocstyle args: - --convention=numpy - # - repo: https://github.com/pre-commit/mirrors-mypy - # rev: v1.0.1 - # hooks: - # - id: mypy + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.7.0 + hooks: + # Run the linter. + - id: ruff + types_or: [ python, pyi, jupyter ] + args: + - msticpy + - --fix - repo: local hooks: - id: check_reqs_all diff --git a/conda/conda-reqs-dev.txt b/conda/conda-reqs-dev.txt index be1f960f3..8dc758ce9 100644 --- a/conda/conda-reqs-dev.txt +++ b/conda/conda-reqs-dev.txt @@ -26,6 +26,7 @@ pyroma>=3.1 pytest-check>=1.0.1 pytest-xdist>=2.5.0 respx>=0.20.1 +ruff>=0.6.6 sphinx_rtd_theme>=0.5.1 sphinx>=2.1.2 virtualenv diff --git a/msticpy/init/mp_user_session.py b/msticpy/init/mp_user_session.py index 11372c28d..511a2e217 100644 --- a/msticpy/init/mp_user_session.py +++ b/msticpy/init/mp_user_session.py @@ -96,8 +96,8 @@ def load_user_session( by default False. """ + current_level = logger.level if verbose: - current_level = logger.level if current_level > logging.INFO: logger.setLevel(logging.INFO) try: diff --git a/requirements-dev.txt b/requirements-dev.txt index 41ab3c5f3..98b435dd6 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -33,6 +33,7 @@ pytest>=5.0.1 readthedocs-sphinx-ext==2.2.5 responses>=0.13.2 respx>=0.20.1 +ruff>=0.6.6 sphinx-rtd-theme>=1.0.0 sphinx>=5.0.1,<8.0.0 types-attrs>=19.0.0 diff --git a/tests/init/test_mp_user_session.py b/tests/init/test_mp_user_session.py index 85e419823..0b1086ed4 100644 --- a/tests/init/test_mp_user_session.py +++ b/tests/init/test_mp_user_session.py @@ -14,23 +14,6 @@ __author__ = "Ian Hellen" -@patch("builtins.print") -@patch("msticpy.init.mp_user_session.Path.exists", return_value=False) -def test_load_user_session_file_not_found(mock_exists, mock_print): - """ - Test that the function prints an error message when the session file is not found. - - Parameters - ---------- - mock_exists : unittest.mock.Mock - Mock for Path.exists method. - mock_print : unittest.mock.Mock - Mock for builtins.print function. - """ - load_user_session("non_existent_file.yaml") - mock_print.assert_called_with("Session file non_existent_file.yaml not found.") - - @pytest.fixture def mp_session_yaml(tmp_path): """ @@ -243,11 +226,8 @@ def test_load_user_session_loads_config( assert "Components" in user_config -@patch("builtins.print") @patch("msticpy.init.mp_user_session.Path.read_text") -def test_load_user_session_bad_missing_file( - mock_path_read_text, mock_print, mp_session_yaml -): +def test_load_user_session_bad_missing_file(mock_path_read_text, mp_session_yaml): """ Test that the function handles invalid YAML content gracefully. @@ -257,8 +237,8 @@ def test_load_user_session_bad_missing_file( Mock for Path().read_text method. """ - load_user_session("non_existent_file.yaml") - mock_print.assert_called_with("Session file non_existent_file.yaml not found.") + with pytest.raises(FileNotFoundError): + load_user_session("non_existent_file.yaml") invalid_yaml = "QueryProviders: [" mock_path_read_text.return_value = invalid_yaml @@ -267,11 +247,11 @@ def test_load_user_session_bad_missing_file( load_user_session(mp_session_yaml) -@patch("builtins.print") +@patch("msticpy.init.mp_user_session.logger") @patch("msticpy.init.mp_user_session.QueryProvider", autospec=True) @patch("msticpy.init.mp_user_session._load_mp_components") def test_query_provider_instantiate_fail( - mock_load_components, mock_query_prov, mock_print, mp_session_yaml + mock_load_components, mock_query_prov, mock_logger, mp_session_yaml ): """ Test load_user_session function. @@ -282,8 +262,8 @@ def test_query_provider_instantiate_fail( Mocked _load_mp_components function. mock_load_providers : MagicMock Mocked _load_query_providers function. - mock_print : MagicMock - Mocked print function. + mock_logger : MagicMock + Mocked logger. tmp_yaml_file : pathlib.Path Path to the temporary YAML file. @@ -296,15 +276,15 @@ def test_query_provider_instantiate_fail( load_user_session(session_file=mp_session_yaml, namespace=namespace) - mock_print.assert_called_with( - "Failed to create MyQueryProvider: Failed to instantiate QueryProvider" + mock_logger.exception.assert_called_with( + "Failed to initialize %s", "MyQueryProvider", exc_info=True ) mock_load_components.assert_called_once() mock_query_prov.assert_called_with("MSSentinel", debug=True) assert "MyQueryProvider" not in namespace # Fail at connect call - mock_print.reset_mock() + mock_logger.reset_mock() mock_query_prov.side_effect = None mock_qp_instance = MagicMock() mock_query_prov.return_value = mock_qp_instance @@ -315,9 +295,7 @@ def test_query_provider_instantiate_fail( mock_qp_instance.connect.assert_called_once_with( workspace="MySoc", auth_methods=["cli", "device_code"] ) - assert ( - mock_print.call_args_list[1][0][0] - == "Failed to connect to MyQueryProvider: Failed to connect QueryProvider" + mock_logger.exception.assert_called_with( + "Failed to connect to %s", "MyQueryProvider", exc_info=True ) - assert mock_print.call_args_list[0][0][0] == "Connecting to MyQueryProvider" assert "MyQueryProvider" not in namespace