|
1 | 1 | import logging |
2 | 2 | import os.path |
3 | 3 | import re |
| 4 | +import shutil |
4 | 5 | from pathlib import Path |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 | from _pytest.logging import LogCaptureFixture |
8 | 9 |
|
9 | 10 | from codestripper.code_stripper import strip_files |
10 | 11 | from codestripper.utils import FileUtils |
| 12 | +from codestripper.utils.enums import UnexpectedInputOptions |
11 | 13 |
|
12 | 14 | test_project_dir = os.path.join(Path(__file__).parent.absolute()) |
13 | 15 |
|
14 | 16 |
|
| 17 | +def test_project_with_unknown_extension_fail(monkeypatch: pytest.MonkeyPatch): |
| 18 | + monkeypatch.chdir(test_project_dir) |
| 19 | + files = FileUtils(["**/*.java", "pom.xml", "**/*.test"], working_directory="testproject").get_matching_files() |
| 20 | + |
| 21 | + with pytest.raises(Exception): |
| 22 | + strip_files(files, "testproject", output="out",unknown_extension=UnexpectedInputOptions.FAIL, fail_on_error=True) |
| 23 | + |
| 24 | + |
| 25 | +def test_project_with_unknown_extension_ignore(monkeypatch: pytest.MonkeyPatch): |
| 26 | + monkeypatch.chdir(test_project_dir) |
| 27 | + files = FileUtils(["**/*.java", "pom.xml", "test.test"], working_directory="testproject").get_matching_files() |
| 28 | + stripped = strip_files(files, "testproject", output="out", unknown_extension=UnexpectedInputOptions.IGNORE) |
| 29 | + assert "test.test" not in stripped |
| 30 | + |
| 31 | + |
| 32 | +def test_project_with_unknown_extension_include(monkeypatch: pytest.MonkeyPatch): |
| 33 | + monkeypatch.chdir(test_project_dir) |
| 34 | + files = FileUtils(["**/*.java", "pom.xml", "test.test"], working_directory="testproject").get_matching_files() |
| 35 | + stripped = strip_files(files, "testproject", output="out", unknown_extension=UnexpectedInputOptions.INCLUDE) |
| 36 | + assert "test.test" in stripped |
| 37 | + |
| 38 | + |
15 | 39 | def test_project(monkeypatch: pytest.MonkeyPatch, caplog: LogCaptureFixture): |
16 | 40 | monkeypatch.chdir(test_project_dir) |
17 | 41 | with caplog.at_level(logging.INFO, logger='codestripper'): |
@@ -132,3 +156,30 @@ def test_non_fail_on_error(monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCa |
132 | 156 | strip_files(files, "files", output="out", fail_on_error=False) |
133 | 157 | errors = [rec.message for rec in caplog.records] |
134 | 158 | assert len(errors) == 4 |
| 159 | + |
| 160 | + |
| 161 | +def test_project_with_binary_fail(monkeypatch: pytest.MonkeyPatch): |
| 162 | + monkeypatch.chdir(test_project_dir) |
| 163 | + shutil.rmtree("out", ignore_errors=True) |
| 164 | + files = FileUtils(["**/*.java", "pom.xml", "test.jpg"], working_directory="testproject").get_matching_files() |
| 165 | + |
| 166 | + with pytest.raises(Exception): |
| 167 | + strip_files(files, "testproject", output="out",binary=UnexpectedInputOptions.FAIL, fail_on_error=True) |
| 168 | + |
| 169 | + |
| 170 | +def test_project_with_binary_ignore(monkeypatch: pytest.MonkeyPatch): |
| 171 | + monkeypatch.chdir(test_project_dir) |
| 172 | + shutil.rmtree("out", ignore_errors=True) |
| 173 | + files = FileUtils(["**/*.java", "pom.xml", "test.jpg"], working_directory="testproject").get_matching_files() |
| 174 | + |
| 175 | + stripped = strip_files(files, "testproject", output="out",binary=UnexpectedInputOptions.IGNORE) |
| 176 | + assert "test.jpg" not in stripped |
| 177 | + |
| 178 | + |
| 179 | +def test_project_with_binary_include(monkeypatch: pytest.MonkeyPatch): |
| 180 | + monkeypatch.chdir(test_project_dir) |
| 181 | + shutil.rmtree("out", ignore_errors=True) |
| 182 | + files = FileUtils(["**/*.java", "pom.xml", "test.jpg"], working_directory="testproject").get_matching_files() |
| 183 | + |
| 184 | + stripped = strip_files(files, "testproject", output="out",binary=UnexpectedInputOptions.INCLUDE) |
| 185 | + assert "test.jpg" in stripped |
0 commit comments