|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +""" |
| 5 | +Tests for file_utils.py with filesystem abstraction. |
| 6 | +""" |
| 7 | + |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from .fs import RecordFs |
| 11 | +from .file_utils import walk_with_gitignore, get_files |
| 12 | + |
| 13 | + |
| 14 | +class TestWalkWithGitignore: |
| 15 | + """Test walk_with_gitignore with RecordFs.""" |
| 16 | + |
| 17 | + def test_basic_directory_traversal(self): |
| 18 | + """Test basic directory traversal without gitignore.""" |
| 19 | + fs = RecordFs( |
| 20 | + { |
| 21 | + Path("root/file1.py"): "print('file1')", |
| 22 | + Path("root/file2.js"): "console.log('file2')", |
| 23 | + } |
| 24 | + ) |
| 25 | + |
| 26 | + files = list(walk_with_gitignore(Path("root"), fs=fs)) |
| 27 | + |
| 28 | + expected = [ |
| 29 | + Path("root/file1.py"), |
| 30 | + Path("root/file2.js"), |
| 31 | + ] |
| 32 | + assert sorted(files) == sorted(expected) |
| 33 | + |
| 34 | + def test_gitignore_filtering(self): |
| 35 | + """Test that gitignore rules are applied correctly.""" |
| 36 | + fs = RecordFs( |
| 37 | + { |
| 38 | + Path("root/.gitignore"): "*.tmp\n*.log\n", |
| 39 | + Path("root/keep.py"): "print('keep')", |
| 40 | + Path("root/ignore.tmp"): "temporary", |
| 41 | + Path("root/keep.js"): "console.log('keep')", |
| 42 | + Path("root/debug.log"): "log content", |
| 43 | + } |
| 44 | + ) |
| 45 | + |
| 46 | + files = list(walk_with_gitignore(Path("root"), fs=fs)) |
| 47 | + |
| 48 | + # .gitignore files should not be included in results |
| 49 | + expected = [ |
| 50 | + Path("root/keep.py"), |
| 51 | + Path("root/keep.js"), |
| 52 | + ] |
| 53 | + assert sorted(files) == sorted(expected) |
| 54 | + |
| 55 | + def test_no_gitignore_file(self): |
| 56 | + """Test directory traversal when no .gitignore exists.""" |
| 57 | + fs = RecordFs( |
| 58 | + { |
| 59 | + Path("root/file1.py"): "print('file1')", |
| 60 | + Path("root/file2.js"): "console.log('file2')", |
| 61 | + Path("root/file3.txt"): "text content", |
| 62 | + } |
| 63 | + ) |
| 64 | + |
| 65 | + files = list(walk_with_gitignore(Path("root"), fs=fs)) |
| 66 | + |
| 67 | + expected = [ |
| 68 | + Path("root/file1.py"), |
| 69 | + Path("root/file2.js"), |
| 70 | + Path("root/file3.txt"), |
| 71 | + ] |
| 72 | + assert sorted(files) == sorted(expected) |
| 73 | + |
| 74 | + def test_empty_directory(self): |
| 75 | + """Test walking an empty directory.""" |
| 76 | + fs = RecordFs({}) |
| 77 | + |
| 78 | + files = list(walk_with_gitignore(Path("empty"), fs=fs)) |
| 79 | + |
| 80 | + assert files == [] |
| 81 | + |
| 82 | + def test_directory_with_only_gitignore(self): |
| 83 | + """Test directory that only contains .gitignore file.""" |
| 84 | + fs = RecordFs( |
| 85 | + { |
| 86 | + Path("root/.gitignore"): "*.tmp\n", |
| 87 | + } |
| 88 | + ) |
| 89 | + |
| 90 | + files = list(walk_with_gitignore(Path("root"), fs=fs)) |
| 91 | + |
| 92 | + assert files == [] |
| 93 | + |
| 94 | + |
| 95 | +class TestGetFiles: |
| 96 | + """Test get_files with RecordFs.""" |
| 97 | + |
| 98 | + def test_get_files_basic(self): |
| 99 | + """Test basic get_files functionality.""" |
| 100 | + fs = RecordFs( |
| 101 | + { |
| 102 | + Path("root/file1.py"): "print('file1')", |
| 103 | + Path("root/file2.js"): "console.log('file2')", |
| 104 | + } |
| 105 | + ) |
| 106 | + |
| 107 | + files = list(get_files(Path("root"), fs=fs)) |
| 108 | + |
| 109 | + expected = [ |
| 110 | + Path("root/file1.py"), |
| 111 | + Path("root/file2.js"), |
| 112 | + ] |
| 113 | + assert sorted(files) == sorted(expected) |
| 114 | + |
| 115 | + def test_get_files_with_skip_function(self): |
| 116 | + """Test get_files with skip function.""" |
| 117 | + fs = RecordFs( |
| 118 | + { |
| 119 | + Path("root/keep.py"): "print('keep')", |
| 120 | + Path("root/skip.py"): "print('skip')", |
| 121 | + Path("root/keep.js"): "console.log('keep')", |
| 122 | + Path("root/skip.js"): "console.log('skip')", |
| 123 | + } |
| 124 | + ) |
| 125 | + |
| 126 | + def skip_function(path: Path) -> bool: |
| 127 | + return "skip" in path.name |
| 128 | + |
| 129 | + files = list(get_files(Path("root"), skip=skip_function, fs=fs)) |
| 130 | + |
| 131 | + expected = [ |
| 132 | + Path("root/keep.py"), |
| 133 | + Path("root/keep.js"), |
| 134 | + ] |
| 135 | + assert sorted(files) == sorted(expected) |
| 136 | + |
| 137 | + def test_get_files_with_gitignore_and_skip(self): |
| 138 | + """Test get_files with both gitignore and skip function.""" |
| 139 | + fs = RecordFs( |
| 140 | + { |
| 141 | + Path("root/.gitignore"): "*.tmp\n", |
| 142 | + Path("root/keep.py"): "print('keep')", |
| 143 | + Path("root/skip.py"): "print('skip')", |
| 144 | + Path("root/ignore.tmp"): "temporary", |
| 145 | + Path("root/keep.js"): "console.log('keep')", |
| 146 | + } |
| 147 | + ) |
| 148 | + |
| 149 | + def skip_function(path: Path) -> bool: |
| 150 | + return "skip" in path.name |
| 151 | + |
| 152 | + files = list(get_files(Path("root"), skip=skip_function, fs=fs)) |
| 153 | + |
| 154 | + expected = [ |
| 155 | + Path("root/keep.py"), |
| 156 | + Path("root/keep.js"), |
| 157 | + ] |
| 158 | + assert sorted(files) == sorted(expected) |
0 commit comments