From 02308dab2a1c71562c2951564e3a1817d634bd7e Mon Sep 17 00:00:00 2001 From: Christian Long Date: Wed, 11 Mar 2015 16:46:43 -0500 Subject: [PATCH 1/2] Fix hook test on Windows This test was failing on Windows, for several reasons. Number one, the paths for the hook test were hard-coded with forward slashes as path separators. I replaced those with calls to os.path.join. Number two, the drive letter was getting in the way of the path manipulation. On Windows, I'm using os.path.splitdrive to fix that. --- CONTRIBUTORS.txt | 1 + flake8/tests/test_hooks.py | 32 ++++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 140b430..5acb13e 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -21,3 +21,4 @@ Contributors (by order of appearance) : - Florent Xicluna - Austin Morton - Michael McNeil Forbes +- Christian Long diff --git a/flake8/tests/test_hooks.py b/flake8/tests/test_hooks.py index 42293ed..db868bd 100644 --- a/flake8/tests/test_hooks.py +++ b/flake8/tests/test_hooks.py @@ -8,6 +8,7 @@ except ImportError: import mock import flake8.hooks +from flake8.util import is_windows def excluded(filename): @@ -15,24 +16,43 @@ def excluded(filename): class TestGitHook(unittest.TestCase): + if is_windows: + current_drive, ignore_tail = os.path.splitdrive(os.getcwd()) + fake_path_at_root_of_filesystem = os.path.join(current_drive, + os.path.sep, + 'fake', + 'tmp') + else: + fake_path_at_root_of_filesystem = os.path.join(os.path.sep, + 'fake', + 'tmp') + @mock.patch('os.makedirs') @mock.patch('flake8.hooks.open', create=True) @mock.patch('shutil.rmtree') - @mock.patch('tempfile.mkdtemp', return_value='/fake/tmp') + @mock.patch('tempfile.mkdtemp', + return_value=fake_path_at_root_of_filesystem) @mock.patch('flake8.hooks.run', - return_value=(None, ['foo/afile.py', 'foo/bfile.py'], None)) + return_value=(None, + [os.path.join('foo', 'afile.py'), + os.path.join('foo', 'bfile.py')], + None)) @mock.patch('flake8.hooks.get_style_guide') def test_prepends_tmp_directory_to_exclude(self, get_style_guide, run, *args): style_guide = get_style_guide.return_value = mock.Mock() - style_guide.options.exclude = ['foo/afile.py'] - style_guide.options.filename = ['foo/*'] + style_guide.options.exclude = [os.path.join('foo', 'afile.py')] + style_guide.options.filename = [os.path.join('foo', '*')] style_guide.excluded = excluded flake8.hooks.git_hook() - dirname, filename = os.path.split(os.path.abspath('foo/bfile.py')) - tmpdir = os.path.join('/fake/tmp', dirname[1:]) + dirname, filename = os.path.split( + os.path.abspath(os.path.join('foo', 'bfile.py'))) + if is_windows: + ignore_drive, dirname = os.path.splitdrive(dirname) + tmpdir = os.path.join(self.fake_path_at_root_of_filesystem, + dirname[1:]) tmpfile = os.path.join(tmpdir, 'bfile.py') style_guide.check_files.assert_called_once_with([tmpfile]) From 5dffadd67b7f9cbd30de68b20f6696809da79d5b Mon Sep 17 00:00:00 2001 From: Christian Long Date: Wed, 11 Mar 2015 21:20:28 -0500 Subject: [PATCH 2/2] Shorten variable name. Add comments --- flake8/tests/test_hooks.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/flake8/tests/test_hooks.py b/flake8/tests/test_hooks.py index db868bd..ba46794 100644 --- a/flake8/tests/test_hooks.py +++ b/flake8/tests/test_hooks.py @@ -17,21 +17,18 @@ def excluded(filename): class TestGitHook(unittest.TestCase): if is_windows: + # On Windows, absolute paths start with a drive letter, for example C: + # Here we build a fake absolute path starting with the current drive + # letter, for example C:\fake\temp current_drive, ignore_tail = os.path.splitdrive(os.getcwd()) - fake_path_at_root_of_filesystem = os.path.join(current_drive, - os.path.sep, - 'fake', - 'tmp') + fake_abs_path = os.path.join(current_drive, os.path.sep, 'fake', 'tmp') else: - fake_path_at_root_of_filesystem = os.path.join(os.path.sep, - 'fake', - 'tmp') + fake_abs_path = os.path.join(os.path.sep, 'fake', 'tmp') @mock.patch('os.makedirs') @mock.patch('flake8.hooks.open', create=True) @mock.patch('shutil.rmtree') - @mock.patch('tempfile.mkdtemp', - return_value=fake_path_at_root_of_filesystem) + @mock.patch('tempfile.mkdtemp', return_value=fake_abs_path) @mock.patch('flake8.hooks.run', return_value=(None, [os.path.join('foo', 'afile.py'), @@ -50,9 +47,10 @@ class TestGitHook(unittest.TestCase): dirname, filename = os.path.split( os.path.abspath(os.path.join('foo', 'bfile.py'))) if is_windows: + # In Windows, the absolute path in dirname will start with a drive + # letter. Here, we discad the drive letter. ignore_drive, dirname = os.path.splitdrive(dirname) - tmpdir = os.path.join(self.fake_path_at_root_of_filesystem, - dirname[1:]) + tmpdir = os.path.join(self.fake_abs_path, dirname[1:]) tmpfile = os.path.join(tmpdir, 'bfile.py') style_guide.check_files.assert_called_once_with([tmpfile])