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