|
7 | 7 | import pytest |
8 | 8 |
|
9 | 9 | from ddev.cli.size.utils.common_funcs import ( |
| 10 | + _matches_gitignore, |
10 | 11 | check_python_version, |
11 | 12 | compress, |
12 | 13 | convert_to_human_readable_size, |
@@ -115,14 +116,34 @@ def test_convert_to_human_readable_size(size_bytes, expected_string): |
115 | 116 | pytest.param("__pycache__/file.py", False, id="pycache"), |
116 | 117 | pytest.param("datadog_checks_dev/example.py", False, id="checks_dev"), |
117 | 118 | pytest.param(".git/config", False, id="git"), |
| 119 | + pytest.param("datadog_checks/module/cache.pyc", False, id="gitignore_glob_ext"), |
| 120 | + pytest.param("datadog_checks/module/__pycache__/foo.py", False, id="gitignore_glob_dir"), |
118 | 121 | ], |
119 | 122 | ) |
120 | 123 | def test_is_valid_integration_file(file_path, expected): |
121 | 124 | repo_path = "fake_repo" |
122 | | - with patch("ddev.cli.size.utils.common_funcs.get_gitignore_files", return_value=set()): |
| 125 | + gitignore_patterns = ["*.pyc", "__pycache__"] |
| 126 | + with patch("ddev.cli.size.utils.common_funcs.get_gitignore_files", return_value=gitignore_patterns): |
123 | 127 | assert is_valid_integration_file(to_native_path(file_path), repo_path) is expected |
124 | 128 |
|
125 | 129 |
|
| 130 | +@pytest.mark.parametrize( |
| 131 | + "path, patterns, expected", |
| 132 | + [ |
| 133 | + pytest.param("foo/bar/baz.pyc", ["*.pyc"], True, id="glob_extension_match"), |
| 134 | + pytest.param("foo/bar/baz.py", ["*.pyc"], False, id="glob_extension_no_match"), |
| 135 | + pytest.param("foo/__pycache__/module.py", ["__pycache__"], True, id="dir_segment_match"), |
| 136 | + pytest.param("foo/bar/module.py", ["__pycache__"], False, id="dir_segment_no_match"), |
| 137 | + pytest.param("foo/bar/notes.log", ["*.log"], True, id="glob_log_match"), |
| 138 | + pytest.param("foo/bar/notes.txt", ["*.log"], False, id="glob_log_no_match"), |
| 139 | + pytest.param("foo/bar/baz.py", ["*.pyc", "__pycache__", "*.log"], False, id="no_pattern_matches"), |
| 140 | + pytest.param("foo/__pycache__/baz.pyc", ["*.pyc", "__pycache__"], True, id="multiple_patterns_first_matches"), |
| 141 | + ], |
| 142 | +) |
| 143 | +def test_matches_gitignore(path, patterns, expected): |
| 144 | + assert _matches_gitignore(to_native_path(path), patterns) is expected |
| 145 | + |
| 146 | + |
126 | 147 | def test_get_dependencies_list(): |
127 | 148 | file_content = "dependency1 @ https://example.com/dependency1/dependency1-1.1.1-.whl\ndependency2 @ https://example.com/dependency2/dependency2-1.1.1-.whl" |
128 | 149 | mock_open_obj = mock_open(read_data=file_content) |
@@ -267,13 +288,10 @@ def test_check_version(py_version, expected): |
267 | 288 | assert check_python_version("fake_repo", "integration1", py_version) is expected |
268 | 289 |
|
269 | 290 |
|
270 | | -def test_get_gitignore_files(): |
271 | | - mock_gitignore = f"__pycache__{os.sep}\n*.log\n" # Sample .gitignore file |
272 | | - repo_path = "fake_repo" |
273 | | - with patch("builtins.open", mock_open(read_data=mock_gitignore)): |
274 | | - with patch("ddev.cli.size.utils.common_funcs.os.path.exists", return_value=True): |
275 | | - ignored_patterns = get_gitignore_files(repo_path) |
276 | | - assert ignored_patterns == ["__pycache__" + os.sep, "*.log"] |
| 291 | +def test_get_gitignore_files(tmp_path): |
| 292 | + gitignore = tmp_path / ".gitignore" |
| 293 | + gitignore.write_text(f"__pycache__{os.sep}\n*.log\n") |
| 294 | + assert get_gitignore_files(tmp_path) == ["__pycache__" + os.sep, "*.log"] |
277 | 295 |
|
278 | 296 |
|
279 | 297 | def test_compress(): |
|
0 commit comments