Use pyfakefs instead of real files in tests

* adjust review findings
This commit is contained in:
Lorenz Egginger 2021-10-08 11:47:40 +02:00
parent 9d0b99f39f
commit 2f4236158c
4 changed files with 20 additions and 21 deletions

View file

@ -59,8 +59,8 @@ Attempts to load all json files to verify syntax.
Check for files that contain merge conflict strings. Check for files that contain merge conflict strings.
#### `check-number-of-lines-count` #### `check-number-of-lines-count`
Check if number of lines of a file is greater than a threshold. You can Check if the number of lines of a file is greater than a threshold.
configure this with the following commandline option: You can configure this with the following command line option:
- `--max-lines N` - maximum number of lines allowed in a file. - `--max-lines N` - maximum number of lines allowed in a file.
#### `check-shebang-scripts-are-executable` #### `check-shebang-scripts-are-executable`

View file

@ -12,7 +12,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
default=30, default=30,
type=int, type=int,
action='store', action='store',
help='Maximum allowable number of lines (default: 30)', help='Maximum allowed number of lines',
) )
args = parser.parse_args(argv) args = parser.parse_args(argv)

View file

@ -1,3 +1,4 @@
covdefaults covdefaults
coverage coverage
pyfakefs
pytest pytest

View file

@ -1,31 +1,29 @@
from pre_commit_hooks.check_number_of_lines_count import main from pre_commit_hooks.check_number_of_lines_count import main
def test_number_of_lines_count_bad(tmpdir): def test_number_of_lines_count_bad(fs):
max_lines = 30 max_lines = 42
line_contents = []
filename = tmpdir.join('lines_count_bad')
for line_number in range(max_lines + 1): filename = 'lines_count_bad'
line_contents.append(f'I am the {line_number+1}. line!') line_contents = [
f'I am the {line_number+1}. line!'
with open(filename, 'w') as file: for line_number in range(max_lines + 1)
file.write('\n'.join(line_contents)) ]
fs.create_file(filename, contents='\n'.join(line_contents))
ret = main([str(filename), f'--max-lines={max_lines}']) ret = main([str(filename), f'--max-lines={max_lines}'])
assert ret == 1 assert ret == 1
def test_number_of_lines_count_good(tmpdir): def test_number_of_lines_count_good(fs):
max_lines = 30 max_lines = 42
line_contents = []
filename = tmpdir.join('lines_count_good')
for line_number in range(max_lines): filename = 'lines_count_good'
line_contents.append(f'I am the {line_number+1}. line!') line_contents = [
f'I am the {line_number+1}. line!'
with open(filename, 'w') as file: for line_number in range(max_lines)
file.write('\n'.join(line_contents)) ]
fs.create_file(filename, contents='\n'.join(line_contents))
ret = main([str(filename), f'--max-lines={max_lines}']) ret = main([str(filename), f'--max-lines={max_lines}'])
assert ret == 0 assert ret == 0