blob: 54c4e6890c3741566dbfb11d95adf0a93538fa50 [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Anthony Sottile301195e2021-07-07 14:06:01 -04003import shutil
Anthony Sottile3f6f23d2015-12-25 09:25:14 -08004
5import pytest
6
Anthony Sottilee0a6e662014-12-31 12:21:21 -08007from pre_commit_hooks.check_added_large_files import find_large_added_files
8from pre_commit_hooks.check_added_large_files import main
Anthony Sottile713fab42015-03-20 13:52:21 -07009from pre_commit_hooks.util import cmd_output
Mikhail Khvoinitsky10c5e4e2021-06-23 03:10:13 +030010from testing.util import git_commit
Anthony Sottilee0a6e662014-12-31 12:21:21 -080011
12
13def test_nothing_added(temp_git_dir):
Anthony Sottilea99475a2016-05-27 14:09:50 -070014 with temp_git_dir.as_cwd():
Anthony Sottilee0a6e662014-12-31 12:21:21 -080015 assert find_large_added_files(['f.py'], 0) == 0
16
17
18def test_adding_something(temp_git_dir):
Anthony Sottilea99475a2016-05-27 14:09:50 -070019 with temp_git_dir.as_cwd():
20 temp_git_dir.join('f.py').write("print('hello world')")
Anthony Sottile713fab42015-03-20 13:52:21 -070021 cmd_output('git', 'add', 'f.py')
Anthony Sottilee0a6e662014-12-31 12:21:21 -080022
23 # Should fail with max size of 0
24 assert find_large_added_files(['f.py'], 0) == 1
25
26
27def test_add_something_giant(temp_git_dir):
Anthony Sottilea99475a2016-05-27 14:09:50 -070028 with temp_git_dir.as_cwd():
29 temp_git_dir.join('f.py').write('a' * 10000)
Anthony Sottilee0a6e662014-12-31 12:21:21 -080030
31 # Should not fail when not added
32 assert find_large_added_files(['f.py'], 0) == 0
33
Anthony Sottile713fab42015-03-20 13:52:21 -070034 cmd_output('git', 'add', 'f.py')
Anthony Sottilee0a6e662014-12-31 12:21:21 -080035
36 # Should fail with strict bound
37 assert find_large_added_files(['f.py'], 0) == 1
38
39 # Should also fail with actual bound
40 assert find_large_added_files(['f.py'], 9) == 1
41
42 # Should pass with higher bound
43 assert find_large_added_files(['f.py'], 10) == 0
44
45
Marcus Shawcroft012bb062020-09-16 06:26:11 +010046def test_enforce_all(temp_git_dir):
47 with temp_git_dir.as_cwd():
48 temp_git_dir.join('f.py').write('a' * 10000)
49
50 # Should fail, when not staged with enforce_all
51 assert find_large_added_files(['f.py'], 0, enforce_all=True) == 1
52
53 # Should pass, when not staged without enforce_all
54 assert find_large_added_files(['f.py'], 0, enforce_all=False) == 0
55
56
Anthony Sottilee0a6e662014-12-31 12:21:21 -080057def test_added_file_not_in_pre_commits_list(temp_git_dir):
Anthony Sottilea99475a2016-05-27 14:09:50 -070058 with temp_git_dir.as_cwd():
59 temp_git_dir.join('f.py').write("print('hello world')")
Anthony Sottile713fab42015-03-20 13:52:21 -070060 cmd_output('git', 'add', 'f.py')
Anthony Sottilee0a6e662014-12-31 12:21:21 -080061
62 # Should pass even with a size of 0
63 assert find_large_added_files(['g.py'], 0) == 0
64
65
66def test_integration(temp_git_dir):
Anthony Sottilea99475a2016-05-27 14:09:50 -070067 with temp_git_dir.as_cwd():
Anthony Sottilee0a6e662014-12-31 12:21:21 -080068 assert main(argv=[]) == 0
69
Anthony Sottilea99475a2016-05-27 14:09:50 -070070 temp_git_dir.join('f.py').write('a' * 10000)
Anthony Sottile713fab42015-03-20 13:52:21 -070071 cmd_output('git', 'add', 'f.py')
Anthony Sottilee0a6e662014-12-31 12:21:21 -080072
73 # Should not fail with default
74 assert main(argv=['f.py']) == 0
75
76 # Should fail with --maxkb
77 assert main(argv=['--maxkb', '9', 'f.py']) == 1
Anthony Sottile3f6f23d2015-12-25 09:25:14 -080078
79
80def has_gitlfs():
Anthony Sottile301195e2021-07-07 14:06:01 -040081 return shutil.which('git-lfs') is not None
Anthony Sottile3f6f23d2015-12-25 09:25:14 -080082
83
84xfailif_no_gitlfs = pytest.mark.xfail(
85 not has_gitlfs(), reason='This test requires git-lfs',
86)
87
88
89@xfailif_no_gitlfs
Anthony Sottile91ee0102021-12-20 23:09:53 -050090def test_allows_gitlfs(temp_git_dir): # pragma: no cover
Anthony Sottilea99475a2016-05-27 14:09:50 -070091 with temp_git_dir.as_cwd():
Anthony Sottile91ee0102021-12-20 23:09:53 -050092 cmd_output('git', 'lfs', 'install', '--local')
Anthony Sottilea99475a2016-05-27 14:09:50 -070093 temp_git_dir.join('f.py').write('a' * 10000)
Anthony Sottile3f6f23d2015-12-25 09:25:14 -080094 cmd_output('git', 'lfs', 'track', 'f.py')
Sander Maijers9e89b762016-06-12 18:49:44 +020095 cmd_output('git', 'add', '--', '.')
Anthony Sottile3f6f23d2015-12-25 09:25:14 -080096 # Should succeed
97 assert main(('--maxkb', '9', 'f.py')) == 0
98
99
100@xfailif_no_gitlfs
Anthony Sottile91ee0102021-12-20 23:09:53 -0500101def test_moves_with_gitlfs(temp_git_dir): # pragma: no cover
Anthony Sottilea99475a2016-05-27 14:09:50 -0700102 with temp_git_dir.as_cwd():
Anthony Sottile91ee0102021-12-20 23:09:53 -0500103 cmd_output('git', 'lfs', 'install', '--local')
Anthony Sottile3f6f23d2015-12-25 09:25:14 -0800104 cmd_output('git', 'lfs', 'track', 'a.bin', 'b.bin')
105 # First add the file we're going to move
Anthony Sottilea99475a2016-05-27 14:09:50 -0700106 temp_git_dir.join('a.bin').write('a' * 10000)
Sander Maijers9e89b762016-06-12 18:49:44 +0200107 cmd_output('git', 'add', '--', '.')
Mikhail Khvoinitsky10c5e4e2021-06-23 03:10:13 +0300108 git_commit('-am', 'foo')
Anthony Sottile3f6f23d2015-12-25 09:25:14 -0800109 # Now move it and make sure the hook still succeeds
110 cmd_output('git', 'mv', 'a.bin', 'b.bin')
111 assert main(('--maxkb', '9', 'b.bin')) == 0
Marcus Shawcroft012bb062020-09-16 06:26:11 +0100112
113
114@xfailif_no_gitlfs
Anthony Sottile91ee0102021-12-20 23:09:53 -0500115def test_enforce_allows_gitlfs(temp_git_dir): # pragma: no cover
Marcus Shawcroft012bb062020-09-16 06:26:11 +0100116 with temp_git_dir.as_cwd():
Anthony Sottile91ee0102021-12-20 23:09:53 -0500117 cmd_output('git', 'lfs', 'install', '--local')
Marcus Shawcroft012bb062020-09-16 06:26:11 +0100118 temp_git_dir.join('f.py').write('a' * 10000)
119 cmd_output('git', 'lfs', 'track', 'f.py')
120 cmd_output('git', 'add', '--', '.')
121 # With --enforce-all large files on git lfs should succeed
122 assert main(('--enforce-all', '--maxkb', '9', 'f.py')) == 0
Alex Martani03a65ca2021-10-21 15:29:54 -0700123
124
Anthony Sottile91ee0102021-12-20 23:09:53 -0500125@xfailif_no_gitlfs
126def test_enforce_allows_gitlfs_after_commit(temp_git_dir): # pragma: no cover
Alex Martani03a65ca2021-10-21 15:29:54 -0700127 with temp_git_dir.as_cwd():
Anthony Sottile91ee0102021-12-20 23:09:53 -0500128 cmd_output('git', 'lfs', 'install', '--local')
Alex Martani03a65ca2021-10-21 15:29:54 -0700129 temp_git_dir.join('f.py').write('a' * 10000)
130 cmd_output('git', 'lfs', 'track', 'f.py')
131 cmd_output('git', 'add', '--', '.')
132 git_commit('-am', 'foo')
133 # With --enforce-all large files on git lfs should succeed
134 assert main(('--enforce-all', '--maxkb', '9', 'f.py')) == 0