From ac38f2ab7a988ea977fdfddca40cc8f0bb487565 Mon Sep 17 00:00:00 2001 From: Neraste Date: Sun, 16 Feb 2025 17:49:55 +0100 Subject: [PATCH 1/5] Update files to remove path --- src/dakara_base/config.py | 9 ++------ tests/test_config.py | 47 ++++++++++++++++----------------------- 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/src/dakara_base/config.py b/src/dakara_base/config.py index c53c198..6b994a4 100644 --- a/src/dakara_base/config.py +++ b/src/dakara_base/config.py @@ -35,6 +35,7 @@ import logging import shutil from collections import UserDict +from importlib.resources import as_file, files import coloredlogs import progressbar @@ -42,12 +43,6 @@ import yaml.parser from environs import Env, EnvError -try: - from importlib.resources import path - -except ImportError: - from importlib_resources import path - from dakara_base.directory import directories from dakara_base.exceptions import DakaraError from dakara_base.utils import strtobool @@ -304,7 +299,7 @@ def create_config_file(resource, filename, force=False): force (bool): If True, config file in user directory is overwritten if it existed already. Otherwise, prompt the user. """ - with path(resource, filename) as origin: + with as_file(files(resource).joinpath(filename)) as origin: # get the file destination = directories.user_config_path / filename diff --git a/tests/test_config.py b/tests/test_config.py index 28d34eb..798bc42 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,14 +1,8 @@ import os -from unittest import TestCase -from unittest.mock import ANY, MagicMock, PropertyMock, patch - -try: - from importlib.resources import path - -except ImportError: - from importlib_resources import path - +from importlib.resources import as_file, files from pathlib import Path +from unittest import TestCase +from unittest.mock import ANY, PropertyMock, patch from environs import Env from platformdirs import PlatformDirs @@ -165,7 +159,7 @@ def test_load_file_success(self): # call the method with self.assertLogs("dakara_base.config", "DEBUG") as logger: - with path("tests.resources", "config.yaml") as file: + with as_file(files("tests.resources").joinpath("config.yaml")) as file: config.load_file(Path(file)) # assert the result @@ -196,7 +190,7 @@ def test_load_file_fail_parser_error(self, mocked_safe_load): # call the method with self.assertLogs("dakara_base.config", "DEBUG"): - with path("tests.resources", "config.yaml") as file: + with as_file(files("tests.resources").joinpath("config.yaml")) as file: with self.assertRaisesRegex( ConfigParseError, "Unable to parse config file" ): @@ -207,7 +201,7 @@ def test_config_env(self): config = Config("DAKARA") with self.assertLogs("dakara_base.config", "DEBUG"): - with path("tests.resources", "config.yaml") as file: + with as_file(files("tests.resources").joinpath("config.yaml")) as file: config.load_file(Path(file)) self.assertNotEqual(config.get("key").get("subkey"), "myvalue") @@ -281,15 +275,6 @@ def test_configure_logger_no_level(self, mocked_set_level): mocked_set_level.assert_called_with("INFO") -# fix required for Python 3.8 as you seemingly cannot use an invalid path as a -# context manager -def mock_context_manager(return_value): - mock = MagicMock() - mock.__enter__.return_value = return_value - - return mock - - @patch("dakara_base.config.shutil.copyfile", autospec=True) @patch.object(Path, "exists", autospec=True) @patch.object(Path, "mkdir", autospec=True) @@ -299,16 +284,18 @@ def mock_context_manager(return_value): new_callable=PropertyMock(return_value=Path("path") / "to" / "directory"), ) @patch( - "dakara_base.config.path", - return_value=mock_context_manager(Path("path") / "to" / "source"), + "dakara_base.config.as_file", + return_value=Path("path") / "to" / "source", autospec=True, ) +@patch("dakara_base.config.files", autospec=True) class CreateConfigFileTestCase(TestCase): """Test the config file creator.""" def test_create_empty( self, - mocked_path, + mocked_files, + mocked_as_file, mocked_user_config_dir, mocked_mkdir, mocked_exists, @@ -323,7 +310,8 @@ def test_create_empty( create_config_file("module.resources", "config.yaml") # assert the call - mocked_path.assert_called_with("module.resources", "config.yaml") + mocked_files.assert_called_with("module.resources") + mocked_files.return_value.joinpath.assert_called_with("config.yaml") mocked_mkdir.assert_called_with(ANY, parents=True, exist_ok=True) mocked_exists.assert_called_with(ANY) mocked_copyfile.assert_called_with( @@ -345,7 +333,8 @@ def test_create_empty( def test_create_existing_no( self, mocked_input, - mocked_path, + mocked_files, + mocked_as_file, mocked_user_config_dir, mocked_mkdir, mocked_exists, @@ -371,7 +360,8 @@ def test_create_existing_no( def test_create_existing_invalid_input( self, mocked_input, - mocked_path, + mocked_files, + mocked_as_file, mocked_user_config_dir, mocked_mkdir, mocked_exists, @@ -392,7 +382,8 @@ def test_create_existing_invalid_input( def test_create_existing_force( self, mocked_input, - mocked_path, + mocked_files, + mocked_as_file, mocked_user_config_dir, mocked_mkdir, mocked_exists, From c94f2fd4256d464693ab0e1647baf0ac57e91f47 Mon Sep 17 00:00:00 2001 From: Neraste Date: Sun, 16 Feb 2025 17:51:22 +0100 Subject: [PATCH 2/5] Make a test more precise --- tests/test_config.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 798bc42..2f06cc4 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -2,7 +2,7 @@ from importlib.resources import as_file, files from pathlib import Path from unittest import TestCase -from unittest.mock import ANY, PropertyMock, patch +from unittest.mock import PropertyMock, patch from environs import Env from platformdirs import PlatformDirs @@ -312,8 +312,10 @@ def test_create_empty( # assert the call mocked_files.assert_called_with("module.resources") mocked_files.return_value.joinpath.assert_called_with("config.yaml") - mocked_mkdir.assert_called_with(ANY, parents=True, exist_ok=True) - mocked_exists.assert_called_with(ANY) + mocked_mkdir.assert_called_with( + Path("path/to/directory"), parents=True, exist_ok=True + ) + mocked_exists.assert_called_with(Path("path/to/directory/config.yaml")) mocked_copyfile.assert_called_with( Path("path") / "to" / "source", Path("path") / "to" / "directory" / "config.yaml", From cd9f85ec1aee908dcf0c82e91ab409bba71a742e Mon Sep 17 00:00:00 2001 From: Neraste Date: Sun, 16 Feb 2025 18:06:14 +0100 Subject: [PATCH 3/5] Fix incorrect mock --- tests/test_config.py | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 2f06cc4..f99bd77 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -285,7 +285,6 @@ def test_configure_logger_no_level(self, mocked_set_level): ) @patch( "dakara_base.config.as_file", - return_value=Path("path") / "to" / "source", autospec=True, ) @patch("dakara_base.config.files", autospec=True) @@ -304,6 +303,9 @@ def test_create_empty( """Test create the config file in an empty directory.""" # setup mocks mocked_exists.return_value = False + mocked_as_file.return_value.__enter__.return_value = ( + Path("path") / "to" / "source" + ) # call the function with self.assertLogs("dakara_base.config") as logger: @@ -346,6 +348,9 @@ def test_create_existing_no( # setup mocks mocked_exists.return_value = True mocked_input.return_value = "no" + mocked_as_file.return_value.__enter__.return_value = ( + Path("path") / "to" / "source" + ) # call the function create_config_file("module.resources", "config.yaml") @@ -358,28 +363,6 @@ def test_create_existing_no( ) ) - @patch("dakara_base.config.input") - def test_create_existing_invalid_input( - self, - mocked_input, - mocked_files, - mocked_as_file, - mocked_user_config_dir, - mocked_mkdir, - mocked_exists, - mocked_copyfile, - ): - """Test create the config file in a non empty directory with invalid input.""" - # setup mocks - mocked_exists.return_value = True - mocked_input.return_value = "" - - # call the function - create_config_file("module.resources", "config.yaml") - - # assert the call - mocked_copyfile.assert_not_called() - @patch("dakara_base.config.input") def test_create_existing_force( self, @@ -392,6 +375,11 @@ def test_create_existing_force( mocked_copyfile, ): """Test create the config file in a non empty directory with force overwrite.""" + # setup mocks + mocked_as_file.return_value.__enter__.return_value = ( + Path("path") / "to" / "source" + ) + # call the function create_config_file("module.resources", "config.yaml", force=True) From 9d6731c81009278844ba4d3e76009549782066d7 Mon Sep 17 00:00:00 2001 From: Neraste Date: Sun, 16 Feb 2025 18:08:41 +0100 Subject: [PATCH 4/5] Simplify call to copyfile --- src/dakara_base/config.py | 4 ++-- tests/test_config.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dakara_base/config.py b/src/dakara_base/config.py index 6b994a4..c709018 100644 --- a/src/dakara_base/config.py +++ b/src/dakara_base/config.py @@ -33,9 +33,9 @@ """ import logging -import shutil from collections import UserDict from importlib.resources import as_file, files +from shutil import copyfile import coloredlogs import progressbar @@ -316,7 +316,7 @@ def create_config_file(resource, filename, force=False): return # copy file - shutil.copyfile(origin, destination) + copyfile(origin, destination) logger.info("Config created in '{}'".format(destination)) diff --git a/tests/test_config.py b/tests/test_config.py index f99bd77..9d2f46f 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -275,7 +275,7 @@ def test_configure_logger_no_level(self, mocked_set_level): mocked_set_level.assert_called_with("INFO") -@patch("dakara_base.config.shutil.copyfile", autospec=True) +@patch("dakara_base.config.copyfile", autospec=True) @patch.object(Path, "exists", autospec=True) @patch.object(Path, "mkdir", autospec=True) @patch.object( From d3647ee8ca3fb19892b77c98e60db77261aa119a Mon Sep 17 00:00:00 2001 From: Neraste Date: Sun, 16 Feb 2025 18:09:46 +0100 Subject: [PATCH 5/5] Simplify logger call --- src/dakara_base/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dakara_base/config.py b/src/dakara_base/config.py index c709018..504ec69 100644 --- a/src/dakara_base/config.py +++ b/src/dakara_base/config.py @@ -317,7 +317,7 @@ def create_config_file(resource, filename, force=False): # copy file copyfile(origin, destination) - logger.info("Config created in '{}'".format(destination)) + logger.info("Config created in '%s'", destination) class ConfigError(DakaraError):