Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 3 | import os |
Anthony Sottile | 69f2da6 | 2018-10-12 19:37:46 -0700 | [diff] [blame] | 4 | import subprocess |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 5 | from unittest import mock |
Anthony Sottile | 69f2da6 | 2018-10-12 19:37:46 -0700 | [diff] [blame] | 6 | |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 7 | import pytest |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 8 | |
| 9 | from pre_commit_hooks.forbid_new_submodules import main |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 10 | from testing.util import git_commit |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 11 | |
| 12 | |
Anthony Sottile | 4ab7914 | 2018-01-21 15:31:23 -0800 | [diff] [blame] | 13 | @pytest.fixture |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 14 | def git_dir_with_git_dir(tmpdir): |
| 15 | with tmpdir.as_cwd(): |
Anthony Sottile | 69f2da6 | 2018-10-12 19:37:46 -0700 | [diff] [blame] | 16 | subprocess.check_call(('git', 'init', '.')) |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 17 | git_commit('--allow-empty', '-m', 'init') |
Anthony Sottile | 69f2da6 | 2018-10-12 19:37:46 -0700 | [diff] [blame] | 18 | subprocess.check_call(('git', 'init', 'foo')) |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 19 | git_commit('--allow-empty', '-m', 'init', cwd=str(tmpdir.join('foo'))) |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 20 | yield |
| 21 | |
| 22 | |
| 23 | @pytest.mark.parametrize( |
| 24 | 'cmd', |
| 25 | ( |
| 26 | # Actually add the submodule |
| 27 | ('git', 'submodule', 'add', './foo'), |
| 28 | # Sneaky submodule add (that doesn't show up in .gitmodules) |
| 29 | ('git', 'add', 'foo'), |
| 30 | ), |
| 31 | ) |
| 32 | def test_main_new_submodule(git_dir_with_git_dir, capsys, cmd): |
Anthony Sottile | 69f2da6 | 2018-10-12 19:37:46 -0700 | [diff] [blame] | 33 | subprocess.check_call(cmd) |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 34 | assert main(('random_non-related_file',)) == 0 |
| 35 | assert main(('foo',)) == 1 |
| 36 | out, _ = capsys.readouterr() |
| 37 | assert out.startswith('foo: new submodule introduced\n') |
| 38 | |
| 39 | |
| 40 | def test_main_new_submodule_committed(git_dir_with_git_dir, capsys): |
| 41 | rev_parse_cmd = ('git', 'rev-parse', 'HEAD') |
| 42 | from_ref = subprocess.check_output(rev_parse_cmd).decode().strip() |
| 43 | subprocess.check_call(('git', 'submodule', 'add', './foo')) |
| 44 | git_commit('-m', 'new submodule') |
| 45 | to_ref = subprocess.check_output(rev_parse_cmd).decode().strip() |
| 46 | with mock.patch.dict( |
| 47 | os.environ, |
| 48 | {'PRE_COMMIT_FROM_REF': from_ref, 'PRE_COMMIT_TO_REF': to_ref}, |
| 49 | ): |
| 50 | assert main(('random_non-related_file',)) == 0 |
| 51 | assert main(('foo',)) == 1 |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 52 | out, _ = capsys.readouterr() |
| 53 | assert out.startswith('foo: new submodule introduced\n') |
| 54 | |
| 55 | |
| 56 | def test_main_no_new_submodule(git_dir_with_git_dir): |
| 57 | open('test.py', 'a+').close() |
Anthony Sottile | 69f2da6 | 2018-10-12 19:37:46 -0700 | [diff] [blame] | 58 | subprocess.check_call(('git', 'add', 'test.py')) |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 59 | assert main(('test.py',)) == 0 |