Skip to content

Commit

Permalink
Check for ignore condition for each directory (#412)
Browse files Browse the repository at this point in the history
Check the ignore glob on each directory before walking it.
Add unit test to catch regressions
  • Loading branch information
athackst committed Oct 6, 2022
1 parent be814dc commit 90cb0d2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
27 changes: 14 additions & 13 deletions mkdocs_simple_plugin/simple.py
Expand Up @@ -77,20 +77,25 @@ def __init__(
def get_files(self) -> list:
"""Get a list of files to process, excluding ignored files."""
files = []
# Get all of the folders that match the include pattern.
# Get all of the entries that match the include pattern.
entries = []
for entry in self.include_folders:
entries.extend(pathlib.Path().glob(entry))
# Get all of the files in the folder that aren't ignored.
for pattern in self.include_folders:
entries.extend(pathlib.Path().glob(pattern))
# Ignore any entries that match the ignore pattern
entries[:] = [
entry for entry in entries
if not self.is_path_ignored(str(entry))]
# Add any files
files[:] = [
os.path.normpath(entry) for entry in entries if entry.is_file()]
# Iterate through directories to get files
for entry in entries:
# Add files to list
if entry.is_file() and not self.is_path_ignored(entry):
files.extend([os.path.normpath(entry)])
# Add all files in folders to list
for root, _, filenames in os.walk(entry):
for root, directories, filenames in os.walk(entry):
files.extend([os.path.join(root, f)
for f in filenames if not self.is_ignored(root, f)]
)
directories[:] = [
d for d in directories if not self.is_ignored(root, d)]
return files

def is_ignored(self, base_path: str, name: str) -> bool:
Expand Down Expand Up @@ -123,10 +128,6 @@ def is_path_ignored(self, path: str = None) -> bool:
if any(fnmatch.fnmatch(path, filter)
for filter in self.ignore_glob):
return True
# Check for ignore folder in patterns
if any(fnmatch.fnmatch(base_path, filter)
for filter in self.ignore_glob):
return True
return False

def should_copy_file(self, name: str) -> bool:
Expand Down
51 changes: 46 additions & 5 deletions tests/test_simple.py
Expand Up @@ -156,7 +156,7 @@ def test_get_files(self):
simple_test = simple.Simple(**self.default_settings)
# /foo
# ├── baz.md
# ├── .mkdocsignore
# ├── .pages
# └── bar
# ├── spam.md // ignored
# ├── hello.txt
Expand All @@ -168,7 +168,7 @@ def test_get_files(self):
# └── day.md
# boo.md
self.fs.create_file("/foo/baz.md")
self.fs.create_file("/foo/.mkdocsignore", contents="bar/spam.md*")
self.fs.create_file("/foo/.pages")
self.fs.create_file("/foo/bar/spam.md")
self.fs.create_file("/foo/bar/hello.txt")
self.fs.create_file("/foo/bar/eggs.md")
Expand All @@ -179,15 +179,56 @@ def test_get_files(self):

files = simple_test.get_files()
self.assertIn("foo/baz.md", files)
self.assertIn("foo/.mkdocsignore", files)
self.assertIn("foo/.pages", files)
self.assertIn("foo/bar/hello.txt", files)
self.assertIn("foo/bar/eggs.md", files)
self.assertNotIn("foo/bar/spam.md", files)
self.assertIn("foo/bar/spam.md", files)
self.assertIn("foo/bat/hello.md", files)
self.assertIn("foo/bat/world.md", files)
self.assertIn("goo/day.md", files)
self.assertIn("boo.md", files)
self.assertEqual(8, len(files))
self.assertEqual(9, len(files))

def test_get_files_ignore_folders(self):
"""Test getting all files not ignored."""
simple_test = simple.Simple(**self.default_settings)
# /foo
# ├── baz.md
# ├── .mkdocsignore
# └── bar // ignore content in this folder
# ├── spam.md
# ├── hello.txt
# └── eggs.md
# └── bat
# ├── hello.md
# └── world.md
# /goo
# └── day.md
# └── night.md // ignored
# boo.md
self.fs.create_file("/foo/baz.md")
self.fs.create_file("/foo/.mkdocsignore", contents="goo/night.md")
self.fs.create_file("/foo/bar/spam.md")
self.fs.create_file("/foo/bar/hello.txt")
self.fs.create_file("/foo/bar/eggs.md")
self.fs.create_file("/foo/bar/bat/hello.md")
self.fs.create_file("/foo/bar/bat/world.md")
self.fs.create_file("/goo/day.md")
self.fs.create_file("boo.md")

simple_test.ignore_glob = set(["foo/bar"])
files = simple_test.get_files()
self.assertIn("foo/baz.md", files)
self.assertIn("foo/.mkdocsignore", files)
self.assertNotIn("foo/bar/hello.txt", files)
self.assertNotIn("foo/bar/eggs.md", files)
self.assertNotIn("foo/bar/spam.md", files)
self.assertNotIn("foo/bar/bat/hello.md", files)
self.assertNotIn("foo/bar/bat/world.md", files)
self.assertIn("goo/day.md", files)
self.assertNotIn("goo/night.md", files)
self.assertIn("boo.md", files)
self.assertEqual(4, len(files))

def test_build_docs(self):
"""Test build docs."""
Expand Down

0 comments on commit 90cb0d2

Please sign in to comment.