blob: c16bf5a95bb8eae6b9f3377e2222364884c232cc [file] [log] [blame]
Anthony Sottile301195e2021-07-07 14:06:01 -04001import shutil
Anthony Sottile3f6f23d2015-12-25 09:25:14 -08002
3import pytest
4
Anthony Sottilee0a6e662014-12-31 12:21:21 -08005from pre_commit_hooks.check_added_large_files import find_large_added_files
6from pre_commit_hooks.check_added_large_files import main
Anthony Sottile713fab42015-03-20 13:52:21 -07007from pre_commit_hooks.util import cmd_output
Mikhail Khvoinitsky10c5e4e2021-06-23 03:10:13 +03008from testing.util import git_commit
Anthony Sottilee0a6e662014-12-31 12:21:21 -08009
10
11def test_nothing_added(temp_git_dir):
Anthony Sottilea99475a2016-05-27 14:09:50 -070012 with temp_git_dir.as_cwd():
Anthony Sottilee0a6e662014-12-31 12:21:21 -080013 assert find_large_added_files(['f.py'], 0) == 0
14
15
16def test_adding_something(temp_git_dir):
Anthony Sottilea99475a2016-05-27 14:09:50 -070017 with temp_git_dir.as_cwd():
18 temp_git_dir.join('f.py').write("print('hello world')")
Anthony Sottile713fab42015-03-20 13:52:21 -070019 cmd_output('git', 'add', 'f.py')
Anthony Sottilee0a6e662014-12-31 12:21:21 -080020
21 # Should fail with max size of 0
22 assert find_large_added_files(['f.py'], 0) == 1
23
24
25def test_add_something_giant(temp_git_dir):
Anthony Sottilea99475a2016-05-27 14:09:50 -070026 with temp_git_dir.as_cwd():
27 temp_git_dir.join('f.py').write('a' * 10000)
Anthony Sottilee0a6e662014-12-31 12:21:21 -080028
29 # Should not fail when not added
30 assert find_large_added_files(['f.py'], 0) == 0
31
Anthony Sottile713fab42015-03-20 13:52:21 -070032 cmd_output('git', 'add', 'f.py')
Anthony Sottilee0a6e662014-12-31 12:21:21 -080033
34 # Should fail with strict bound
35 assert find_large_added_files(['f.py'], 0) == 1
36
37 # Should also fail with actual bound
38 assert find_large_added_files(['f.py'], 9) == 1
39
40 # Should pass with higher bound
41 assert find_large_added_files(['f.py'], 10) == 0
42
43
Marcus Shawcroft012bb062020-09-16 06:26:11 +010044def test_enforce_all(temp_git_dir):
45 with temp_git_dir.as_cwd():
46 temp_git_dir.join('f.py').write('a' * 10000)
47
48 # Should fail, when not staged with enforce_all
49 assert find_large_added_files(['f.py'], 0, enforce_all=True) == 1
50
51 # Should pass, when not staged without enforce_all
52 assert find_large_added_files(['f.py'], 0, enforce_all=False) == 0
53
54
Anthony Sottilee0a6e662014-12-31 12:21:21 -080055def test_added_file_not_in_pre_commits_list(temp_git_dir):
Anthony Sottilea99475a2016-05-27 14:09:50 -070056 with temp_git_dir.as_cwd():
57 temp_git_dir.join('f.py').write("print('hello world')")
Anthony Sottile713fab42015-03-20 13:52:21 -070058 cmd_output('git', 'add', 'f.py')
Anthony Sottilee0a6e662014-12-31 12:21:21 -080059
60 # Should pass even with a size of 0
61 assert find_large_added_files(['g.py'], 0) == 0
62
63
64def test_integration(temp_git_dir):
Anthony Sottilea99475a2016-05-27 14:09:50 -070065 with temp_git_dir.as_cwd():
Anthony Sottilee0a6e662014-12-31 12:21:21 -080066 assert main(argv=[]) == 0
67
Anthony Sottilea99475a2016-05-27 14:09:50 -070068 temp_git_dir.join('f.py').write('a' * 10000)
Anthony Sottile713fab42015-03-20 13:52:21 -070069 cmd_output('git', 'add', 'f.py')
Anthony Sottilee0a6e662014-12-31 12:21:21 -080070
71 # Should not fail with default
72 assert main(argv=['f.py']) == 0
73
74 # Should fail with --maxkb
75 assert main(argv=['--maxkb', '9', 'f.py']) == 1
Anthony Sottile3f6f23d2015-12-25 09:25:14 -080076
77
78def has_gitlfs():
Anthony Sottile301195e2021-07-07 14:06:01 -040079 return shutil.which('git-lfs') is not None
Anthony Sottile3f6f23d2015-12-25 09:25:14 -080080
81
82xfailif_no_gitlfs = pytest.mark.xfail(
83 not has_gitlfs(), reason='This test requires git-lfs',
84)
85
86
87@xfailif_no_gitlfs
Anthony Sottile91ee0102021-12-20 23:09:53 -050088def test_allows_gitlfs(temp_git_dir): # pragma: no cover
Anthony Sottilea99475a2016-05-27 14:09:50 -070089 with temp_git_dir.as_cwd():
Anthony Sottile91ee0102021-12-20 23:09:53 -050090 cmd_output('git', 'lfs', 'install', '--local')
Anthony Sottilea99475a2016-05-27 14:09:50 -070091 temp_git_dir.join('f.py').write('a' * 10000)
Anthony Sottile3f6f23d2015-12-25 09:25:14 -080092 cmd_output('git', 'lfs', 'track', 'f.py')
Sander Maijers9e89b762016-06-12 18:49:44 +020093 cmd_output('git', 'add', '--', '.')
Anthony Sottile3f6f23d2015-12-25 09:25:14 -080094 # Should succeed
95 assert main(('--maxkb', '9', 'f.py')) == 0
96
97
98@xfailif_no_gitlfs
Anthony Sottile91ee0102021-12-20 23:09:53 -050099def test_moves_with_gitlfs(temp_git_dir): # pragma: no cover
Anthony Sottilea99475a2016-05-27 14:09:50 -0700100 with temp_git_dir.as_cwd():
Anthony Sottile91ee0102021-12-20 23:09:53 -0500101 cmd_output('git', 'lfs', 'install', '--local')
Anthony Sottile3f6f23d2015-12-25 09:25:14 -0800102 cmd_output('git', 'lfs', 'track', 'a.bin', 'b.bin')
103 # First add the file we're going to move
Anthony Sottilea99475a2016-05-27 14:09:50 -0700104 temp_git_dir.join('a.bin').write('a' * 10000)
Sander Maijers9e89b762016-06-12 18:49:44 +0200105 cmd_output('git', 'add', '--', '.')
Mikhail Khvoinitsky10c5e4e2021-06-23 03:10:13 +0300106 git_commit('-am', 'foo')
Anthony Sottile3f6f23d2015-12-25 09:25:14 -0800107 # Now move it and make sure the hook still succeeds
108 cmd_output('git', 'mv', 'a.bin', 'b.bin')
109 assert main(('--maxkb', '9', 'b.bin')) == 0
Marcus Shawcroft012bb062020-09-16 06:26:11 +0100110
111
112@xfailif_no_gitlfs
Anthony Sottile91ee0102021-12-20 23:09:53 -0500113def test_enforce_allows_gitlfs(temp_git_dir): # pragma: no cover
Marcus Shawcroft012bb062020-09-16 06:26:11 +0100114 with temp_git_dir.as_cwd():
Anthony Sottile91ee0102021-12-20 23:09:53 -0500115 cmd_output('git', 'lfs', 'install', '--local')
Marcus Shawcroft012bb062020-09-16 06:26:11 +0100116 temp_git_dir.join('f.py').write('a' * 10000)
117 cmd_output('git', 'lfs', 'track', 'f.py')
118 cmd_output('git', 'add', '--', '.')
119 # With --enforce-all large files on git lfs should succeed
120 assert main(('--enforce-all', '--maxkb', '9', 'f.py')) == 0
Alex Martani03a65ca2021-10-21 15:29:54 -0700121
122
Anthony Sottile91ee0102021-12-20 23:09:53 -0500123@xfailif_no_gitlfs
124def test_enforce_allows_gitlfs_after_commit(temp_git_dir): # pragma: no cover
Alex Martani03a65ca2021-10-21 15:29:54 -0700125 with temp_git_dir.as_cwd():
Anthony Sottile91ee0102021-12-20 23:09:53 -0500126 cmd_output('git', 'lfs', 'install', '--local')
Alex Martani03a65ca2021-10-21 15:29:54 -0700127 temp_git_dir.join('f.py').write('a' * 10000)
128 cmd_output('git', 'lfs', 'track', 'f.py')
129 cmd_output('git', 'add', '--', '.')
130 git_commit('-am', 'foo')
131 # With --enforce-all large files on git lfs should succeed
132 assert main(('--enforce-all', '--maxkb', '9', 'f.py')) == 0