-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
141 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[bumpversion] | ||
current_version = 1.7.0 | ||
current_version = 1.8.0 | ||
|
||
[bumpversion:file:setup.cfg] | ||
|
||
|
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
/docs/_build/ | ||
/dev.md | ||
__dev__.py | ||
.vscode | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
|
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,48 @@ | ||
"""Check that a set of lines are included inside the content of a file.""" | ||
|
||
import argparse | ||
import sys | ||
|
||
|
||
def smart_quoted(value): | ||
return ( | ||
f"'{value}'" | ||
if "'" not in value | ||
else (f'"{value}"' if '"' not in value else f"'{value}'") | ||
) | ||
|
||
|
||
def file_check_lines(filename, expected_lines, quiet=False): | ||
with open(filename, encoding="utf-8") as f: | ||
lines = f.read().splitlines() | ||
|
||
expected_lines = [line.strip("\r\n") for line in expected_lines if line] | ||
if not expected_lines: | ||
sys.stderr.write("Any valid non empty expected line passed as argument\n") | ||
return 1 | ||
|
||
retcode = 0 | ||
for expected_line in expected_lines: | ||
if expected_line in lines: | ||
continue | ||
|
||
retcode = 1 | ||
if not quiet: | ||
sys.stderr.write( | ||
f"Line {smart_quoted(expected_line)} not found in file {filename}\n" | ||
) | ||
|
||
return retcode | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-q", "--quiet", action="store_true", help="Supress output") | ||
parser.add_argument("lines_files", nargs='+', help="Lines and a filename to check for content") | ||
args = parser.parse_args() | ||
|
||
return file_check_lines(args.lines_files[-1], args.lines_files[:-1], quiet=args.quiet) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
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,49 @@ | ||
import contextlib | ||
import io | ||
import tempfile | ||
|
||
import pytest | ||
|
||
from hooks.file_check_lines import file_check_lines, smart_quoted | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"quiet", | ||
(True, False), | ||
ids=("quiet=True", "quiet=False"), | ||
) | ||
@pytest.mark.parametrize( | ||
("content", "lines", "expected_exitcode", "expected_missing_lines"), | ||
( | ||
pytest.param("foo\nbar\nbaz\n", ["foo", "bar"], 0, [], id="ok"), | ||
pytest.param("foo\n\n\n", ["bar", "baz"], 1, ["bar", "baz"], id="fail"), | ||
pytest.param( | ||
"foo\n\n\r\n\r\n\n", ["\r\n", "\n", "baz"], 1, ["baz"], id="ignore-newlines" | ||
), | ||
pytest.param( | ||
"foo\n\n\n", ["\r\n", "\n", "baz"], 1, ["baz"], id="ignore-empty-lines" | ||
), | ||
), | ||
) | ||
def test_file_check_lines( | ||
quiet, content, lines, expected_exitcode, expected_missing_lines | ||
): | ||
stderr = io.StringIO() | ||
with tempfile.NamedTemporaryFile() as f: | ||
f.write(content.encode("utf-8")) | ||
f.seek(0) | ||
|
||
with contextlib.redirect_stderr(stderr): | ||
exitcode = file_check_lines(f.name, lines, quiet=quiet) | ||
|
||
assert exitcode == expected_exitcode | ||
|
||
if quiet: | ||
assert not stderr.getvalue() | ||
else: | ||
stderr_lines = stderr.getvalue().splitlines() | ||
for expected_missing_line in expected_missing_lines: | ||
assert ( | ||
f"Line {smart_quoted(expected_missing_line)} not" | ||
f" found in file {f.name}" | ||
) in stderr_lines |