aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBill Wendling <morbo@google.com>2018-05-14 23:54:19 -0700
committerGitHub <noreply@github.com>2018-05-14 23:54:19 -0700
commitea27f55c4c0e399d15feea18461a989626ed7d62 (patch)
tree302e922da063bfc4e071f8e872efca7aaf757853
parentdd7be4369da7698e8c450aa6c4a9de7ac6da0169 (diff)
parentc167c48808cead50d434416468574d68fb408785 (diff)
downloadyapf-ea27f55c4c0e399d15feea18461a989626ed7d62.tar.gz
Merge pull request #548 from DHager/bugfix_exclude_hiddendirs
Bug: Does not exclude hidden folders in a relative context
-rw-r--r--yapf/yapflib/file_resources.py5
-rw-r--r--yapftests/file_resources_test.py72
2 files changed, 76 insertions, 1 deletions
diff --git a/yapf/yapflib/file_resources.py b/yapf/yapflib/file_resources.py
index 5468ced..67eea61 100644
--- a/yapf/yapflib/file_resources.py
+++ b/yapf/yapflib/file_resources.py
@@ -148,7 +148,10 @@ def _FindPythonFiles(filenames, recursive, exclude):
def IsIgnored(path, exclude):
"""Return True if filename matches any patterns in exclude."""
- return any(fnmatch.fnmatch(path.lstrip('./'), e.rstrip('/')) for e in exclude)
+ path = path.lstrip("/")
+ while path.startswith("./"):
+ path = path[2:]
+ return any(fnmatch.fnmatch(path, e.rstrip('/')) for e in exclude)
def IsPythonFile(filename):
diff --git a/yapftests/file_resources_test.py b/yapftests/file_resources_test.py
index 4440e07..5f06e3e 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,15 @@ 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 +148,68 @@ 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/')