Anthony Sottile | 301195e | 2021-07-07 14:06:01 -0400 | [diff] [blame] | 1 | import shutil |
Anthony Sottile | 3f6f23d | 2015-12-25 09:25:14 -0800 | [diff] [blame] | 2 | |
| 3 | import pytest |
| 4 | |
Anthony Sottile | e0a6e66 | 2014-12-31 12:21:21 -0800 | [diff] [blame] | 5 | from pre_commit_hooks.check_added_large_files import find_large_added_files |
| 6 | from pre_commit_hooks.check_added_large_files import main |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 7 | from pre_commit_hooks.util import cmd_output |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 8 | from testing.util import git_commit |
Anthony Sottile | e0a6e66 | 2014-12-31 12:21:21 -0800 | [diff] [blame] | 9 | |
| 10 | |
| 11 | def test_nothing_added(temp_git_dir): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 12 | with temp_git_dir.as_cwd(): |
Anthony Sottile | e0a6e66 | 2014-12-31 12:21:21 -0800 | [diff] [blame] | 13 | assert find_large_added_files(['f.py'], 0) == 0 |
| 14 | |
| 15 | |
| 16 | def test_adding_something(temp_git_dir): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 17 | with temp_git_dir.as_cwd(): |
| 18 | temp_git_dir.join('f.py').write("print('hello world')") |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 19 | cmd_output('git', 'add', 'f.py') |
Anthony Sottile | e0a6e66 | 2014-12-31 12:21:21 -0800 | [diff] [blame] | 20 | |
| 21 | # Should fail with max size of 0 |
| 22 | assert find_large_added_files(['f.py'], 0) == 1 |
| 23 | |
| 24 | |
| 25 | def test_add_something_giant(temp_git_dir): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 26 | with temp_git_dir.as_cwd(): |
| 27 | temp_git_dir.join('f.py').write('a' * 10000) |
Anthony Sottile | e0a6e66 | 2014-12-31 12:21:21 -0800 | [diff] [blame] | 28 | |
| 29 | # Should not fail when not added |
| 30 | assert find_large_added_files(['f.py'], 0) == 0 |
| 31 | |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 32 | cmd_output('git', 'add', 'f.py') |
Anthony Sottile | e0a6e66 | 2014-12-31 12:21:21 -0800 | [diff] [blame] | 33 | |
| 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 Shawcroft | 012bb06 | 2020-09-16 06:26:11 +0100 | [diff] [blame] | 44 | def 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 Sottile | e0a6e66 | 2014-12-31 12:21:21 -0800 | [diff] [blame] | 55 | def test_added_file_not_in_pre_commits_list(temp_git_dir): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 56 | with temp_git_dir.as_cwd(): |
| 57 | temp_git_dir.join('f.py').write("print('hello world')") |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 58 | cmd_output('git', 'add', 'f.py') |
Anthony Sottile | e0a6e66 | 2014-12-31 12:21:21 -0800 | [diff] [blame] | 59 | |
| 60 | # Should pass even with a size of 0 |
| 61 | assert find_large_added_files(['g.py'], 0) == 0 |
| 62 | |
| 63 | |
| 64 | def test_integration(temp_git_dir): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 65 | with temp_git_dir.as_cwd(): |
Anthony Sottile | e0a6e66 | 2014-12-31 12:21:21 -0800 | [diff] [blame] | 66 | assert main(argv=[]) == 0 |
| 67 | |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 68 | temp_git_dir.join('f.py').write('a' * 10000) |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 69 | cmd_output('git', 'add', 'f.py') |
Anthony Sottile | e0a6e66 | 2014-12-31 12:21:21 -0800 | [diff] [blame] | 70 | |
| 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 Sottile | 3f6f23d | 2015-12-25 09:25:14 -0800 | [diff] [blame] | 76 | |
| 77 | |
| 78 | def has_gitlfs(): |
Anthony Sottile | 301195e | 2021-07-07 14:06:01 -0400 | [diff] [blame] | 79 | return shutil.which('git-lfs') is not None |
Anthony Sottile | 3f6f23d | 2015-12-25 09:25:14 -0800 | [diff] [blame] | 80 | |
| 81 | |
| 82 | xfailif_no_gitlfs = pytest.mark.xfail( |
| 83 | not has_gitlfs(), reason='This test requires git-lfs', |
| 84 | ) |
| 85 | |
| 86 | |
| 87 | @xfailif_no_gitlfs |
Anthony Sottile | 91ee010 | 2021-12-20 23:09:53 -0500 | [diff] [blame^] | 88 | def test_allows_gitlfs(temp_git_dir): # pragma: no cover |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 89 | with temp_git_dir.as_cwd(): |
Anthony Sottile | 91ee010 | 2021-12-20 23:09:53 -0500 | [diff] [blame^] | 90 | cmd_output('git', 'lfs', 'install', '--local') |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 91 | temp_git_dir.join('f.py').write('a' * 10000) |
Anthony Sottile | 3f6f23d | 2015-12-25 09:25:14 -0800 | [diff] [blame] | 92 | cmd_output('git', 'lfs', 'track', 'f.py') |
Sander Maijers | 9e89b76 | 2016-06-12 18:49:44 +0200 | [diff] [blame] | 93 | cmd_output('git', 'add', '--', '.') |
Anthony Sottile | 3f6f23d | 2015-12-25 09:25:14 -0800 | [diff] [blame] | 94 | # Should succeed |
| 95 | assert main(('--maxkb', '9', 'f.py')) == 0 |
| 96 | |
| 97 | |
| 98 | @xfailif_no_gitlfs |
Anthony Sottile | 91ee010 | 2021-12-20 23:09:53 -0500 | [diff] [blame^] | 99 | def test_moves_with_gitlfs(temp_git_dir): # pragma: no cover |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 100 | with temp_git_dir.as_cwd(): |
Anthony Sottile | 91ee010 | 2021-12-20 23:09:53 -0500 | [diff] [blame^] | 101 | cmd_output('git', 'lfs', 'install', '--local') |
Anthony Sottile | 3f6f23d | 2015-12-25 09:25:14 -0800 | [diff] [blame] | 102 | cmd_output('git', 'lfs', 'track', 'a.bin', 'b.bin') |
| 103 | # First add the file we're going to move |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 104 | temp_git_dir.join('a.bin').write('a' * 10000) |
Sander Maijers | 9e89b76 | 2016-06-12 18:49:44 +0200 | [diff] [blame] | 105 | cmd_output('git', 'add', '--', '.') |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 106 | git_commit('-am', 'foo') |
Anthony Sottile | 3f6f23d | 2015-12-25 09:25:14 -0800 | [diff] [blame] | 107 | # 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 Shawcroft | 012bb06 | 2020-09-16 06:26:11 +0100 | [diff] [blame] | 110 | |
| 111 | |
| 112 | @xfailif_no_gitlfs |
Anthony Sottile | 91ee010 | 2021-12-20 23:09:53 -0500 | [diff] [blame^] | 113 | def test_enforce_allows_gitlfs(temp_git_dir): # pragma: no cover |
Marcus Shawcroft | 012bb06 | 2020-09-16 06:26:11 +0100 | [diff] [blame] | 114 | with temp_git_dir.as_cwd(): |
Anthony Sottile | 91ee010 | 2021-12-20 23:09:53 -0500 | [diff] [blame^] | 115 | cmd_output('git', 'lfs', 'install', '--local') |
Marcus Shawcroft | 012bb06 | 2020-09-16 06:26:11 +0100 | [diff] [blame] | 116 | 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 Martani | 03a65ca | 2021-10-21 15:29:54 -0700 | [diff] [blame] | 121 | |
| 122 | |
Anthony Sottile | 91ee010 | 2021-12-20 23:09:53 -0500 | [diff] [blame^] | 123 | @xfailif_no_gitlfs |
| 124 | def test_enforce_allows_gitlfs_after_commit(temp_git_dir): # pragma: no cover |
Alex Martani | 03a65ca | 2021-10-21 15:29:54 -0700 | [diff] [blame] | 125 | with temp_git_dir.as_cwd(): |
Anthony Sottile | 91ee010 | 2021-12-20 23:09:53 -0500 | [diff] [blame^] | 126 | cmd_output('git', 'lfs', 'install', '--local') |
Alex Martani | 03a65ca | 2021-10-21 15:29:54 -0700 | [diff] [blame] | 127 | 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 |