generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 16
Abstract filesystem operations in file_utils.py #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """ | ||
| Tests for file_utils.py with filesystem abstraction. | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| from aws_doc_sdk_examples_tools.fs import RecordFs | ||
| from aws_doc_sdk_examples_tools.file_utils import walk_with_gitignore, get_files | ||
|
|
||
|
|
||
| class TestWalkWithGitignore: | ||
cpyle0819 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Test walk_with_gitignore with RecordFs.""" | ||
|
|
||
| def test_basic_directory_traversal(self): | ||
| """Test basic directory traversal without gitignore.""" | ||
| fs = RecordFs( | ||
| { | ||
| Path("/root/file1.py"): "print('file1')", | ||
| Path("/root/file2.js"): "console.log('file2')", | ||
| } | ||
| ) | ||
|
|
||
| files = list(walk_with_gitignore(Path("/root"), fs=fs)) | ||
|
|
||
| expected = [ | ||
| Path("/root/file1.py"), | ||
| Path("/root/file2.js"), | ||
| ] | ||
| assert sorted(files) == sorted(expected) | ||
|
|
||
| def test_gitignore_filtering(self): | ||
| """Test that gitignore rules are applied correctly.""" | ||
| fs = RecordFs( | ||
| { | ||
| Path("/root/.gitignore"): "*.tmp\n*.log\n", | ||
| Path("/root/keep.py"): "print('keep')", | ||
| Path("/root/ignore.tmp"): "temporary", | ||
| Path("/root/keep.js"): "console.log('keep')", | ||
| Path("/root/debug.log"): "log content", | ||
| } | ||
| ) | ||
|
|
||
| files = list(walk_with_gitignore(Path("/root"), fs=fs)) | ||
|
|
||
| # .gitignore files should not be included in results | ||
| expected = [ | ||
| Path("/root/keep.py"), | ||
| Path("/root/keep.js"), | ||
| ] | ||
| assert sorted(files) == sorted(expected) | ||
|
|
||
| def test_no_gitignore_file(self): | ||
| """Test directory traversal when no .gitignore exists.""" | ||
| fs = RecordFs( | ||
| { | ||
| Path("/root/file1.py"): "print('file1')", | ||
| Path("/root/file2.js"): "console.log('file2')", | ||
| Path("/root/file3.txt"): "text content", | ||
| } | ||
| ) | ||
|
|
||
| files = list(walk_with_gitignore(Path("/root"), fs=fs)) | ||
|
|
||
| expected = [ | ||
| Path("/root/file1.py"), | ||
| Path("/root/file2.js"), | ||
| Path("/root/file3.txt"), | ||
| ] | ||
| assert sorted(files) == sorted(expected) | ||
|
|
||
| def test_empty_directory(self): | ||
| """Test walking an empty directory.""" | ||
| fs = RecordFs({}) | ||
|
|
||
| files = list(walk_with_gitignore(Path("/empty"), fs=fs)) | ||
|
|
||
| assert files == [] | ||
|
|
||
| def test_directory_with_only_gitignore(self): | ||
| """Test directory that only contains .gitignore file.""" | ||
| fs = RecordFs( | ||
| { | ||
| Path("/root/.gitignore"): "*.tmp\n", | ||
| } | ||
| ) | ||
|
|
||
| files = list(walk_with_gitignore(Path("/root"), fs=fs)) | ||
|
|
||
| assert files == [] | ||
|
|
||
| def test_nested_gitignores(self): | ||
| """Test nested gitignore files with different rules.""" | ||
| fs = RecordFs( | ||
| { | ||
| # Root level gitignore ignores *.log files | ||
| Path("/root/.gitignore"): "*.log\n", | ||
| Path("/root/keep.py"): "print('keep')", | ||
| Path("/root/debug.log"): "root log", # Should be ignored | ||
| # Nested directory with its own gitignore ignoring *.tmp files | ||
| Path("/root/subdir/.gitignore"): "*.tmp\n", | ||
| Path("/root/subdir/keep.js"): "console.log('keep')", | ||
| Path( | ||
| "/root/subdir/ignore.tmp" | ||
| ): "temporary", # Should be ignored by subdir gitignore | ||
| Path( | ||
| "/root/subdir/keep.log" | ||
| ): "nested log", # Should be ignored by root gitignore | ||
| } | ||
| ) | ||
|
|
||
| files = list(walk_with_gitignore(Path("/root"), fs=fs)) | ||
|
|
||
| # Only files that don't match any gitignore pattern should be returned | ||
| expected = [ | ||
| Path("/root/keep.py"), | ||
| Path("/root/subdir/keep.js"), | ||
| ] | ||
| assert sorted(files) == sorted(expected) | ||
|
|
||
|
|
||
| class TestGetFiles: | ||
| """Test get_files with RecordFs.""" | ||
|
|
||
| def test_get_files_basic(self): | ||
| """Test basic get_files functionality.""" | ||
| fs = RecordFs( | ||
| { | ||
| Path("/root/file1.py"): "print('file1')", | ||
| Path("/root/file2.js"): "console.log('file2')", | ||
| } | ||
| ) | ||
|
|
||
| files = list(get_files(Path("/root"), fs=fs)) | ||
|
|
||
| expected = [ | ||
| Path("/root/file1.py"), | ||
| Path("/root/file2.js"), | ||
| ] | ||
| assert sorted(files) == sorted(expected) | ||
|
|
||
| def test_get_files_with_skip_function(self): | ||
| """Test get_files with skip function.""" | ||
| fs = RecordFs( | ||
| { | ||
| Path("/root/keep.py"): "print('keep')", | ||
| Path("/root/skip.py"): "print('skip')", | ||
| Path("/root/keep.js"): "console.log('keep')", | ||
| Path("/root/skip.js"): "console.log('skip')", | ||
| } | ||
| ) | ||
|
|
||
| def skip_function(path: Path) -> bool: | ||
| return "skip" in path.name | ||
|
|
||
| files = list(get_files(Path("/root"), skip=skip_function, fs=fs)) | ||
|
|
||
| expected = [ | ||
| Path("/root/keep.py"), | ||
| Path("/root/keep.js"), | ||
| ] | ||
| assert sorted(files) == sorted(expected) | ||
|
|
||
| def test_get_files_with_gitignore_and_skip(self): | ||
| """Test get_files with both gitignore and skip function.""" | ||
| fs = RecordFs( | ||
| { | ||
| Path("/root/.gitignore"): "*.tmp\n", | ||
| Path("/root/keep.py"): "print('keep')", | ||
| Path("/root/skip.py"): "print('skip')", | ||
| Path("/root/ignore.tmp"): "temporary", | ||
| Path("/root/keep.js"): "console.log('keep')", | ||
| } | ||
| ) | ||
|
|
||
| def skip_function(path: Path) -> bool: | ||
| return "skip" in path.name | ||
|
|
||
| files = list(get_files(Path("/root"), skip=skip_function, fs=fs)) | ||
|
|
||
| expected = [ | ||
| Path("/root/keep.py"), | ||
| Path("/root/keep.js"), | ||
| ] | ||
| assert sorted(files) == sorted(expected) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.