From 98d3d50295c3e686913e4c01ca0de2c523aae33b Mon Sep 17 00:00:00 2001 From: "Eric N. Vander Weele" Date: Sun, 12 Jan 2020 16:26:26 -0800 Subject: [PATCH 1/2] tests: Ensure patched `os.getcwd()` is an absolute path `os.getcwd()` returns an absolute path; thus, the patched paths should be absolute as well. This is an incremental change towards removing the `ConfigFileFinder` attributes `.parent` and `.tail` to be localized to `.generate_possible_local_files()`. Without this, the tests fail when moving the patching because `os.path.abspath()` calls `os.getcwd()`, expecting `os.getcwd()` to be an absolute path. --- tests/unit/test_config_file_finder.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_config_file_finder.py b/tests/unit/test_config_file_finder.py index 44da8d4..c0231f8 100644 --- a/tests/unit/test_config_file_finder.py +++ b/tests/unit/test_config_file_finder.py @@ -48,15 +48,15 @@ def test_cli_config_double_read(): @pytest.mark.parametrize('cwd,expected', [ # Root directory of project - ('.', + (os.path.abspath('.'), [os.path.abspath('setup.cfg'), os.path.abspath('tox.ini')]), # Subdirectory of project directory - ('src', + (os.path.abspath('src'), [os.path.abspath('setup.cfg'), os.path.abspath('tox.ini')]), # Outside of project directory - ('/', + (os.path.abspath('/'), []), ]) def test_generate_possible_local_files(cwd, expected): From e887ef33177b1d7617c6f1cf816e323886863b64 Mon Sep 17 00:00:00 2001 From: "Eric N. Vander Weele" Date: Sun, 12 Jan 2020 16:37:25 -0800 Subject: [PATCH 2/2] config: Remove ConfigFileFinder 'parent' and 'tail' attributes These attributes are only needed within the `.generate_possible_local_files()` method. Therefore, just obtain the current working directory at the beginning of the method and reduce the lifetime state of the `ConfigFileFinder` object. --- src/flake8/options/config.py | 5 +---- tests/unit/test_config_file_finder.py | 9 +++++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index 7261bcd..dab16c4 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -51,8 +51,6 @@ class ConfigFileFinder(object): self.local_directory = os.path.abspath(os.curdir) - self.parent = self.tail = os.getcwd() - # caches to avoid double-reading config files self._local_configs = None self._local_found_files = [] # type: List[str] @@ -96,8 +94,7 @@ class ConfigFileFinder(object): def generate_possible_local_files(self): """Find and generate all local config files.""" - tail = self.tail - parent = self.parent + parent = tail = os.getcwd() found_config_files = False while tail and not found_config_files: for project_filename in self.project_filenames: diff --git a/tests/unit/test_config_file_finder.py b/tests/unit/test_config_file_finder.py index c0231f8..d03118a 100644 --- a/tests/unit/test_config_file_finder.py +++ b/tests/unit/test_config_file_finder.py @@ -61,11 +61,12 @@ def test_cli_config_double_read(): ]) def test_generate_possible_local_files(cwd, expected): """Verify generation of all possible config paths.""" - with mock.patch.object(os, 'getcwd', return_value=cwd): - finder = config.ConfigFileFinder('flake8', []) + finder = config.ConfigFileFinder('flake8', []) - assert (list(finder.generate_possible_local_files()) - == expected) + with mock.patch.object(os, 'getcwd', return_value=cwd): + config_files = list(finder.generate_possible_local_files()) + + assert config_files == expected @pytest.mark.parametrize('extra_config_files,expected', [