Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Krystian Chmura | f681234 | 2021-03-19 00:59:31 +0100 | [diff] [blame] | 3 | import sys |
| 4 | |
| 5 | import pytest |
| 6 | |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 7 | from pre_commit_hooks.check_case_conflict import find_conflicting_filenames |
| 8 | from pre_commit_hooks.check_case_conflict import main |
Krystian Chmura | f681234 | 2021-03-19 00:59:31 +0100 | [diff] [blame] | 9 | from pre_commit_hooks.check_case_conflict import parents |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 10 | from pre_commit_hooks.util import cmd_output |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 11 | from testing.util import git_commit |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 12 | |
Krystian Chmura | f681234 | 2021-03-19 00:59:31 +0100 | [diff] [blame] | 13 | skip_win32 = pytest.mark.skipif( |
| 14 | sys.platform == 'win32', |
| 15 | reason='case conflicts between directories and files', |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | def test_parents(): |
| 20 | assert set(parents('a')) == set() |
| 21 | assert set(parents('a/b')) == {'a'} |
| 22 | assert set(parents('a/b/c')) == {'a/b', 'a'} |
| 23 | assert set(parents('a/b/c/d')) == {'a/b/c', 'a/b', 'a'} |
| 24 | |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 25 | |
| 26 | def test_nothing_added(temp_git_dir): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 27 | with temp_git_dir.as_cwd(): |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 28 | assert find_conflicting_filenames(['f.py']) == 0 |
| 29 | |
| 30 | |
| 31 | def test_adding_something(temp_git_dir): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 32 | with temp_git_dir.as_cwd(): |
| 33 | temp_git_dir.join('f.py').write("print('hello world')") |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 34 | cmd_output('git', 'add', 'f.py') |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 35 | |
| 36 | assert find_conflicting_filenames(['f.py']) == 0 |
| 37 | |
| 38 | |
| 39 | def test_adding_something_with_conflict(temp_git_dir): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 40 | with temp_git_dir.as_cwd(): |
| 41 | temp_git_dir.join('f.py').write("print('hello world')") |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 42 | cmd_output('git', 'add', 'f.py') |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 43 | temp_git_dir.join('F.py').write("print('hello world')") |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 44 | cmd_output('git', 'add', 'F.py') |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 45 | |
| 46 | assert find_conflicting_filenames(['f.py', 'F.py']) == 1 |
| 47 | |
| 48 | |
Krystian Chmura | f681234 | 2021-03-19 00:59:31 +0100 | [diff] [blame] | 49 | @skip_win32 # pragma: win32 no cover |
| 50 | def test_adding_files_with_conflicting_directories(temp_git_dir): |
| 51 | with temp_git_dir.as_cwd(): |
| 52 | temp_git_dir.mkdir('dir').join('x').write('foo') |
| 53 | temp_git_dir.mkdir('DIR').join('y').write('foo') |
| 54 | cmd_output('git', 'add', '-A') |
| 55 | |
| 56 | assert find_conflicting_filenames([]) == 1 |
| 57 | |
| 58 | |
| 59 | @skip_win32 # pragma: win32 no cover |
| 60 | def test_adding_files_with_conflicting_deep_directories(temp_git_dir): |
| 61 | with temp_git_dir.as_cwd(): |
| 62 | temp_git_dir.mkdir('x').mkdir('y').join('z').write('foo') |
| 63 | temp_git_dir.join('X').write('foo') |
| 64 | cmd_output('git', 'add', '-A') |
| 65 | |
| 66 | assert find_conflicting_filenames([]) == 1 |
| 67 | |
| 68 | |
| 69 | @skip_win32 # pragma: win32 no cover |
| 70 | def test_adding_file_with_conflicting_directory(temp_git_dir): |
| 71 | with temp_git_dir.as_cwd(): |
| 72 | temp_git_dir.mkdir('dir').join('x').write('foo') |
| 73 | temp_git_dir.join('DIR').write('foo') |
| 74 | cmd_output('git', 'add', '-A') |
| 75 | |
| 76 | assert find_conflicting_filenames([]) == 1 |
| 77 | |
| 78 | |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 79 | def test_added_file_not_in_pre_commits_list(temp_git_dir): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 80 | with temp_git_dir.as_cwd(): |
| 81 | temp_git_dir.join('f.py').write("print('hello world')") |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 82 | cmd_output('git', 'add', 'f.py') |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 83 | |
| 84 | assert find_conflicting_filenames(['g.py']) == 0 |
| 85 | |
| 86 | |
| 87 | def test_file_conflicts_with_committed_file(temp_git_dir): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 88 | with temp_git_dir.as_cwd(): |
| 89 | temp_git_dir.join('f.py').write("print('hello world')") |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 90 | cmd_output('git', 'add', 'f.py') |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 91 | git_commit('-m', 'Add f.py') |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 92 | |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 93 | temp_git_dir.join('F.py').write("print('hello world')") |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 94 | cmd_output('git', 'add', 'F.py') |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 95 | |
| 96 | assert find_conflicting_filenames(['F.py']) == 1 |
| 97 | |
| 98 | |
Krystian Chmura | f681234 | 2021-03-19 00:59:31 +0100 | [diff] [blame] | 99 | @skip_win32 # pragma: win32 no cover |
| 100 | def test_file_conflicts_with_committed_dir(temp_git_dir): |
| 101 | with temp_git_dir.as_cwd(): |
| 102 | temp_git_dir.mkdir('dir').join('x').write('foo') |
| 103 | cmd_output('git', 'add', '-A') |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 104 | git_commit('-m', 'Add f.py') |
Krystian Chmura | f681234 | 2021-03-19 00:59:31 +0100 | [diff] [blame] | 105 | |
| 106 | temp_git_dir.join('DIR').write('foo') |
| 107 | cmd_output('git', 'add', '-A') |
| 108 | |
| 109 | assert find_conflicting_filenames([]) == 1 |
| 110 | |
| 111 | |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 112 | def test_integration(temp_git_dir): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 113 | with temp_git_dir.as_cwd(): |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 114 | assert main(argv=[]) == 0 |
| 115 | |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 116 | temp_git_dir.join('f.py').write("print('hello world')") |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 117 | cmd_output('git', 'add', 'f.py') |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 118 | |
| 119 | assert main(argv=['f.py']) == 0 |
| 120 | |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 121 | temp_git_dir.join('F.py').write("print('hello world')") |
Anthony Sottile | 713fab4 | 2015-03-20 13:52:21 -0700 | [diff] [blame] | 122 | cmd_output('git', 'add', 'F.py') |
gkisel | c682b50 | 2015-01-07 14:07:32 -0800 | [diff] [blame] | 123 | |
| 124 | assert main(argv=['F.py']) == 1 |