mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-09 12:54:17 +00:00
Merge pull request #97 from pre-commit/dont_write_files_that_arent_cleaned_up
Write to temp directories in such a way that files get cleaned up
This commit is contained in:
commit
2c62e4aafc
1 changed files with 8 additions and 14 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import tempfile
|
import io
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
@ -15,24 +15,18 @@ def test_pretty_format_json(filename, expected_retval):
|
||||||
assert ret == expected_retval
|
assert ret == expected_retval
|
||||||
|
|
||||||
|
|
||||||
def test_autofix_pretty_format_json():
|
def test_autofix_pretty_format_json(tmpdir):
|
||||||
toformat_file = tempfile.NamedTemporaryFile(delete=False, mode='w+')
|
srcfile = tmpdir.join('to_be_json_formatted.json')
|
||||||
|
with io.open(get_resource_path('not_pretty_formatted_json.json')) as f:
|
||||||
# copy our file to format there
|
srcfile.write_text(f.read(), 'UTF-8')
|
||||||
model_file = open(get_resource_path('not_pretty_formatted_json.json'), 'r')
|
|
||||||
model_contents = model_file.read()
|
|
||||||
model_file.close()
|
|
||||||
|
|
||||||
toformat_file.write(model_contents)
|
|
||||||
toformat_file.close()
|
|
||||||
|
|
||||||
# now launch the autofix on that file
|
# now launch the autofix on that file
|
||||||
ret = pretty_format_json(['--autofix', toformat_file.name])
|
ret = pretty_format_json(['--autofix', srcfile.strpath])
|
||||||
# it should have formatted it
|
# it should have formatted it
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
|
|
||||||
# file already good
|
# file was formatted (shouldn't trigger linter again)
|
||||||
ret = pretty_format_json([toformat_file.name])
|
ret = pretty_format_json([srcfile.strpath])
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue