aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarien Hager <dhager@gmi-mr.com>2018-03-30 19:10:01 -0700
committerDarien Hager <dhager@gmi-mr.com>2018-03-30 19:10:01 -0700
commitbb795494351d1e2c77912ce7fcddc42433b73565 (patch)
tree62602165adc9f9f8c7abb01a01d09c883ff9adf0
parent7cfb6b10b65e6db1f09583e5cc6a5f5bb7937f4f (diff)
downloadyapf-bb795494351d1e2c77912ce7fcddc42433b73565.tar.gz
Add failing unit-test demonstrating that exclusion patterns can fail when using a relative target directory and trying to exclude a child hidden folder.
-rw-r--r--yapftests/file_resources_test.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/yapftests/file_resources_test.py b/yapftests/file_resources_test.py
index 4440e07..72ff5a6 100644
--- a/yapftests/file_resources_test.py
+++ b/yapftests/file_resources_test.py
@@ -14,6 +14,7 @@
# limitations under the License.
"""Tests for yapf.file_resources."""
+import contextlib
import os
import shutil
import tempfile
@@ -26,6 +27,14 @@ from yapf.yapflib import py3compat
from yapftests import utils
+@contextlib.contextmanager
+def _restore_working_dir():
+ curdir = os.getcwd()
+ try:
+ yield
+ finally:
+ os.chdir(curdir)
+
class GetDefaultStyleForDirTest(unittest.TestCase):
def setUp(self):
@@ -138,6 +147,69 @@ class GetCommandLineFilesTest(unittest.TestCase):
os.path.join(tdir2, 'testfile2.py'),
]))
+ def test_find_with_excluded_hidden_dirs(self):
+ tdir1 = self._make_test_dir('.test1')
+ tdir2 = self._make_test_dir('test_2')
+ tdir3 = self._make_test_dir('test.3')
+ files = [
+ os.path.join(tdir1, 'testfile1.py'),
+ os.path.join(tdir2, 'testfile2.py'),
+ os.path.join(tdir3, 'testfile3.py'),
+ ]
+ _touch_files(files)
+
+ actual = file_resources.GetCommandLineFiles(
+ [self.test_tmpdir], recursive=True, exclude=['*.test1*'])
+
+ self.assertEqual(
+ sorted(
+ actual
+ ),
+ sorted([
+ os.path.join(tdir2, 'testfile2.py'),
+ os.path.join(tdir3, 'testfile3.py'),
+ ]))
+
+ def test_find_with_excluded_hidden_dirs_relative(self):
+ """
+ A regression test against a specific case where a hidden directory (one
+ beginning with a period) is being excluded, but it is also an immediate
+ child of the current directory which has been specified in a relative
+ manner.
+
+ At its core, the bug has to do with overzelous stripping of "./foo" so that
+ it removes too much from "./.foo" .
+ """
+ tdir1 = self._make_test_dir('.test1')
+ tdir2 = self._make_test_dir('test_2')
+ tdir3 = self._make_test_dir('test.3')
+ files = [
+ os.path.join(tdir1, 'testfile1.py'),
+ os.path.join(tdir2, 'testfile2.py'),
+ os.path.join(tdir3, 'testfile3.py'),
+ ]
+ _touch_files(files)
+
+ # We must temporarily change the current directory, so that we test against
+ # patterns like ./.test1/file instead of /tmp/foo/.test1/file
+ with _restore_working_dir():
+
+ os.chdir(self.test_tmpdir)
+ actual = file_resources.GetCommandLineFiles(
+ [os.path.relpath(self.test_tmpdir)],
+ recursive=True, exclude=['*.test1*'])
+
+ self.assertEqual(
+ sorted(
+ actual
+ ),
+ sorted([
+ os.path.join(os.path.relpath(self.test_tmpdir),
+ os.path.basename(tdir2), 'testfile2.py'),
+ os.path.join(os.path.relpath(self.test_tmpdir),
+ os.path.basename(tdir3), 'testfile3.py'),
+ ]))
+
def test_find_with_excluded_dirs(self):
tdir1 = self._make_test_dir('test1')
tdir2 = self._make_test_dir('test2/testinner/')