From a76b4b3a1313725aac182c6ccba2a66e32edfae4 Mon Sep 17 00:00:00 2001 From: Cody Scott Date: Mon, 5 Feb 2024 16:38:18 -0500 Subject: [PATCH] Fix ignore list Files inside a .git/ folder were being removed even with FREEZER_DESTINATION_IGNORE set. --- docs/index.rst | 2 +- flask_frozen/__init__.py | 24 ++++++++++++++---------- tests/test_frozen_flask.py | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index ae103ec..be9be7a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -266,7 +266,7 @@ are accepted: As in ``.gitignore`` files, patterns apply to the whole path if they contain a slash ``/``, to each slash-separated part otherwise. - For example, this could be set to ``['.git*']`` + For example, this could be set to ``['*.git*']`` if the destination is a git repository. .. versionadded:: 0.10 diff --git a/flask_frozen/__init__.py b/flask_frozen/__init__.py index 38dee06..31d0c12 100644 --- a/flask_frozen/__init__.py +++ b/flask_frozen/__init__.py @@ -428,16 +428,20 @@ def walk_directory(root, ignore=()): """ for dir, dirs, filenames in os.walk(root): - for filename in filenames: - path = str((Path(dir) / filename).relative_to(root)) - if os.sep != '/': - path = path.replace(os.sep, '/') - for pattern in ignore: - if fnmatch(path if '/' in pattern else filename, pattern): - break - else: - # See https://github.com/SimonSapin/Frozen-Flask/issues/5 - yield normalize('NFC', path) + for pattern in ignore: + if fnmatch(dir, pattern): + break + else: + for filename in filenames: + path = str((Path(dir) / filename).relative_to(root)) + if os.sep != '/': + path = path.replace(os.sep, '/') + for pattern in ignore: + if fnmatch(path if '/' in pattern else filename, pattern): + break + else: + # See https://github.com/SimonSapin/Frozen-Flask/issues/5 + yield normalize('NFC', path) @contextmanager diff --git a/tests/test_frozen_flask.py b/tests/test_frozen_flask.py index e3215f2..686271c 100644 --- a/tests/test_frozen_flask.py +++ b/tests/test_frozen_flask.py @@ -45,6 +45,20 @@ def test_walk_directory(): assert { filename for filename in walk_directory(directory) if not filename.endswith(('.pyc', '.pyo', '.css'))} == paths + + git_dir = directory / '.git' + git_dir.mkdir() + git_file = git_dir / 'keep' + with git_file.open('w') as keep_file: + keep_file.write('keep') + try: + found = set( + walk_directory(directory, ('*.git', '*.pyc', '*.pyo', '*.css')) + ) + assert found == paths + finally: + git_file.unlink() + git_dir.rmdir() def test_warnings_share_common_superclass():