Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 3 | from pre_commit_hooks.check_vcs_permalinks import main |
| 4 | |
| 5 | |
| 6 | def test_trivial(tmpdir): |
| 7 | f = tmpdir.join('f.txt').ensure() |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 8 | assert not main((str(f),)) |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 9 | |
| 10 | |
| 11 | def test_passing(tmpdir): |
| 12 | f = tmpdir.join('f.txt') |
| 13 | f.write_binary( |
| 14 | # permalinks are ok |
| 15 | b'https://github.com/asottile/test/blob/649e6/foo%20bar#L1\n' |
jack1142 | 4729918 | 2021-04-06 20:07:08 +0200 | [diff] [blame] | 16 | # tags are ok |
| 17 | b'https://github.com/asottile/test/blob/1.0.0/foo%20bar#L1\n' |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 18 | # links to files but not line numbers are ok |
Anthony Sottile | c72ad40 | 2018-06-04 10:10:14 -0700 | [diff] [blame] | 19 | b'https://github.com/asottile/test/blob/master/foo%20bar\n' |
| 20 | # regression test for overly-greedy regex |
| 21 | b'https://github.com/ yes / no ? /blob/master/foo#L1\n', |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 22 | ) |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 23 | assert not main((str(f),)) |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def test_failing(tmpdir, capsys): |
| 27 | with tmpdir.as_cwd(): |
| 28 | tmpdir.join('f.txt').write_binary( |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 29 | b'https://github.com/asottile/test/blob/master/foo#L1\n' |
jack1142 | 4729918 | 2021-04-06 20:07:08 +0200 | [diff] [blame] | 30 | b'https://example.com/asottile/test/blob/master/foo#L1\n' |
| 31 | b'https://example.com/asottile/test/blob/main/foo#L1\n', |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 34 | assert main(('f.txt', '--additional-github-domain', 'example.com')) |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 35 | out, _ = capsys.readouterr() |
| 36 | assert out == ( |
| 37 | 'f.txt:1:https://github.com/asottile/test/blob/master/foo#L1\n' |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 38 | 'f.txt:2:https://example.com/asottile/test/blob/master/foo#L1\n' |
jack1142 | 4729918 | 2021-04-06 20:07:08 +0200 | [diff] [blame] | 39 | 'f.txt:3:https://example.com/asottile/test/blob/main/foo#L1\n' |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 40 | '\n' |
| 41 | 'Non-permanent github link detected.\n' |
| 42 | 'On any page on github press [y] to load a permalink.\n' |
| 43 | ) |