Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not stop when scanning a file ending with \0 (#155)
Extract the decoding logic used when reading files from Docker images and reuse it when scanning files from the file system.
- Loading branch information
1 parent
7c34d65
commit 9483b88
Showing
4 changed files
with
79 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,58 @@ | ||
from pathlib import Path | ||
from typing import Callable | ||
|
||
import pytest | ||
|
||
from ggshield.path import generate_files_from_paths | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["filename", "input_content", "expected_content"], | ||
[ | ||
("normal.txt", "Normal", "Normal"), | ||
("0-inside.txt", "Ins\0de", "Ins de"), | ||
], | ||
) | ||
def test_generate_files_from_paths( | ||
tmp_path, filename: str, input_content: str, expected_content: str | ||
): | ||
""" | ||
GIVEN a file | ||
WHEN calling generate_files_from_paths() on it | ||
THEN it returns the expected File instance | ||
AND the content of the File instance is what is expected | ||
""" | ||
path = tmp_path / filename | ||
Path(path).write_text(input_content) | ||
|
||
files = list(generate_files_from_paths([str(path)], verbose=False)) | ||
|
||
file = files[0] | ||
assert file.filename == str(path) | ||
assert file.document == expected_content | ||
|
||
assert len(files) == 1 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["filename", "creator"], | ||
[ | ||
("a_binary_file.tar", lambda x: x.write_text("Uninteresting")), | ||
("big_file", lambda x: x.write_text(2_000_000 * " ")), | ||
("i_am_a_dir", lambda x: x.mkdir()), | ||
], | ||
) | ||
def test_generate_files_from_paths_skips_files( | ||
tmp_path, filename: str, creator: Callable[[Path], None] | ||
): | ||
""" | ||
GIVEN a file which should be skipped | ||
WHEN calling generate_files_from_paths() on it | ||
THEN it should return an empty list | ||
""" | ||
path = tmp_path / filename | ||
creator(path) | ||
|
||
files = list(generate_files_from_paths([str(path)], verbose=False)) | ||
|
||
assert files == [] |