Merge pull request #126 from sanmai-NL/enhance_file_extnsn_pttrns_trlng_whtspc_fxr Add file patterns for trailing whitespace fixer
diff --git a/pre_commit_hooks/debug_statement_hook.py b/pre_commit_hooks/debug_statement_hook.py index 8e02480..52fe72e 100644 --- a/pre_commit_hooks/debug_statement_hook.py +++ b/pre_commit_hooks/debug_statement_hook.py
@@ -7,7 +7,7 @@ import traceback -DEBUG_STATEMENTS = set(['pdb', 'ipdb', 'pudb', 'q']) +DEBUG_STATEMENTS = set(['pdb', 'ipdb', 'pudb', 'q', 'rdb']) DebugStatement = collections.namedtuple(
diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index 916735c..36a90eb 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py
@@ -24,6 +24,26 @@ f.write(new_contents) +def parse_indent(s): + # type: (str) -> str + try: + int_indentation_spec = int(s) + except ValueError: + if not s.strip(): + return s + else: + raise ValueError( + 'Non-whitespace JSON indentation delimiter supplied. ', + ) + else: + if int_indentation_spec >= 0: + return int_indentation_spec * ' ' + else: + raise ValueError( + 'Negative integer supplied to construct JSON indentation delimiter. ', + ) + + def pretty_format_json(argv=None): parser = argparse.ArgumentParser() parser.add_argument( @@ -34,9 +54,9 @@ ) parser.add_argument( '--indent', - type=int, - default=2, - help='Number of indent spaces used to pretty-format files', + type=parse_indent, + default=' ', + help='String used as delimiter for one indentation level', ) parser.add_argument( '--no-sort-keys',
diff --git a/testing/resources/tab_pretty_formatted_json.json b/testing/resources/tab_pretty_formatted_json.json new file mode 100644 index 0000000..abda648 --- /dev/null +++ b/testing/resources/tab_pretty_formatted_json.json
@@ -0,0 +1,9 @@ +{ + "alist": [ + 2, + 34, + 234 + ], + "blah": null, + "foo": "bar" +}
diff --git a/tests/check_added_large_files_test.py b/tests/check_added_large_files_test.py index 99c60cb..ce15f5c 100644 --- a/tests/check_added_large_files_test.py +++ b/tests/check_added_large_files_test.py
@@ -80,11 +80,18 @@ def test_allows_gitlfs(temp_git_dir): # pragma: no cover with temp_git_dir.as_cwd(): # Work around https://github.com/github/git-lfs/issues/913 - cmd_output('git', 'commit', '--allow-empty', '-m', 'foo') + cmd_output( + 'git', + 'commit', + '--no-gpg-sign', + '--allow-empty', + '-m', + 'foo', + ) cmd_output('git', 'lfs', 'install') temp_git_dir.join('f.py').write('a' * 10000) cmd_output('git', 'lfs', 'track', 'f.py') - cmd_output('git', 'add', '.') + cmd_output('git', 'add', '--', '.') # Should succeed assert main(('--maxkb', '9', 'f.py')) == 0 @@ -96,8 +103,8 @@ cmd_output('git', 'lfs', 'track', 'a.bin', 'b.bin') # First add the file we're going to move temp_git_dir.join('a.bin').write('a' * 10000) - cmd_output('git', 'add', '.') - cmd_output('git', 'commit', '-am', 'foo') + cmd_output('git', 'add', '--', '.') + cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'foo') # Now move it and make sure the hook still succeeds cmd_output('git', 'mv', 'a.bin', 'b.bin') assert main(('--maxkb', '9', 'b.bin')) == 0
diff --git a/tests/check_case_conflict_test.py b/tests/check_case_conflict_test.py index b5f538a..077b41b 100644 --- a/tests/check_case_conflict_test.py +++ b/tests/check_case_conflict_test.py
@@ -41,7 +41,7 @@ with temp_git_dir.as_cwd(): temp_git_dir.join('f.py').write("print('hello world')") cmd_output('git', 'add', 'f.py') - cmd_output('git', 'commit', '--no-verify', '-m', 'Add f.py') + cmd_output('git', 'commit', '--no-gpg-sign', '-n', '-m', 'Add f.py') temp_git_dir.join('F.py').write("print('hello world')") cmd_output('git', 'add', 'F.py')
diff --git a/tests/check_merge_conflict_test.py b/tests/check_merge_conflict_test.py index a7dbea3..8141ade 100644 --- a/tests/check_merge_conflict_test.py +++ b/tests/check_merge_conflict_test.py
@@ -23,23 +23,23 @@ repo2 = tmpdir.join('repo2') repo2_f1 = repo2.join('f1') - cmd_output('git', 'init', repo1.strpath) + cmd_output('git', 'init', '--', repo1.strpath) with repo1.as_cwd(): repo1_f1.ensure() - cmd_output('git', 'add', repo1_f1.strpath) - cmd_output('git', 'commit', '-m', 'commit1') + cmd_output('git', 'add', '--', repo1_f1.strpath) + cmd_output('git', 'commit', '--no-gpg-sign', '-m', 'commit1') cmd_output('git', 'clone', repo1.strpath, repo2.strpath) # Commit in master with repo1.as_cwd(): repo1_f1.write('parent\n') - cmd_output('git', 'commit', '-am', 'master commit2') + cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'master commit2') # Commit in clone and pull with repo2.as_cwd(): repo2_f1.write('child\n') - cmd_output('git', 'commit', '-am', 'clone commit2') + cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'clone commit2') cmd_output('git', 'pull', retcode=None) # We should end up in a merge conflict! f1 = repo2_f1.read() @@ -73,21 +73,21 @@ cmd_output('git', 'init', repo1.strpath) with repo1.as_cwd(): repo1_f1.ensure() - cmd_output('git', 'add', repo1_f1.strpath) - cmd_output('git', 'commit', '-m' 'commit1') + cmd_output('git', 'add', '--', repo1_f1.strpath) + cmd_output('git', 'commit', '--no-gpg-sign', '-m', 'commit1') cmd_output('git', 'clone', repo1.strpath, repo2.strpath) # Commit in master with repo1.as_cwd(): repo1_f1.write('parent\n') - cmd_output('git', 'commit', '-am', 'master commit2') + cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'master commit2') # Commit in clone and pull without committing with repo2.as_cwd(): repo2_f2.write('child\n') - cmd_output('git', 'add', repo2_f2.strpath) - cmd_output('git', 'commit', '-m', 'clone commit2') + cmd_output('git', 'add', '--', repo2_f2.strpath) + cmd_output('git', 'commit', '--no-gpg-sign', '-m', 'clone commit2') cmd_output('git', 'pull', '--no-commit') # We should end up in a pending merge assert repo2_f1.read() == 'parent\n'
diff --git a/tests/conftest.py b/tests/conftest.py index c3dc342..87fec70 100644 --- a/tests/conftest.py +++ b/tests/conftest.py
@@ -10,5 +10,5 @@ @pytest.yield_fixture def temp_git_dir(tmpdir): git_dir = tmpdir.join('gits') - cmd_output('git', 'init', git_dir.strpath) + cmd_output('git', 'init', '--', git_dir.strpath) yield git_dir
diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 3d533e4..d9061d3 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py
@@ -2,10 +2,21 @@ import pytest +from pre_commit_hooks.pretty_format_json import parse_indent from pre_commit_hooks.pretty_format_json import pretty_format_json from testing.util import get_resource_path +def test_parse_indent(): + assert parse_indent('0') == '' + assert parse_indent('2') == ' ' + assert parse_indent('\t') == '\t' + with pytest.raises(ValueError): + parse_indent('a') + with pytest.raises(ValueError): + parse_indent('-2') + + @pytest.mark.parametrize(('filename', 'expected_retval'), ( ('not_pretty_formatted_json.json', 1), ('unsorted_pretty_formatted_json.json', 1), @@ -26,6 +37,17 @@ assert ret == expected_retval +@pytest.mark.parametrize(('filename', 'expected_retval'), ( + ('not_pretty_formatted_json.json', 1), + ('unsorted_pretty_formatted_json.json', 1), + ('pretty_formatted_json.json', 1), + ('tab_pretty_formatted_json.json', 0), +)) +def test_tab_pretty_format_json(filename, expected_retval): + ret = pretty_format_json(['--indent', '\t', get_resource_path(filename)]) + assert ret == expected_retval + + def test_autofix_pretty_format_json(tmpdir): srcfile = tmpdir.join('to_be_json_formatted.json') shutil.copyfile(