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 1 commit
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
cpyle0819 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,158 @@ | ||
| # 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 .fs import RecordFs | ||
cpyle0819 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from .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')", | ||
cpyle0819 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 == [] | ||
|
|
||
|
|
||
| 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) | ||
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.