blob: 01ce94decd52f45c82269f3d13a5e09df96977d0 [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Anthony Sottile9db0a742017-10-06 23:32:11 -07003from pre_commit_hooks.check_vcs_permalinks import main
4
5
6def test_trivial(tmpdir):
7 f = tmpdir.join('f.txt').ensure()
Max Rozentsveygf35bfed2020-05-20 12:07:45 -04008 assert not main((str(f),))
Anthony Sottile9db0a742017-10-06 23:32:11 -07009
10
11def 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'
jack114247299182021-04-06 20:07:08 +020016 # tags are ok
17 b'https://github.com/asottile/test/blob/1.0.0/foo%20bar#L1\n'
Anthony Sottile9db0a742017-10-06 23:32:11 -070018 # links to files but not line numbers are ok
Anthony Sottilec72ad402018-06-04 10:10:14 -070019 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 Sottile9db0a742017-10-06 23:32:11 -070022 )
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040023 assert not main((str(f),))
Anthony Sottile9db0a742017-10-06 23:32:11 -070024
25
26def test_failing(tmpdir, capsys):
27 with tmpdir.as_cwd():
28 tmpdir.join('f.txt').write_binary(
Youngmin Koo1f8151a2020-11-17 02:33:47 +090029 b'https://github.com/asottile/test/blob/master/foo#L1\n'
jack114247299182021-04-06 20:07:08 +020030 b'https://example.com/asottile/test/blob/master/foo#L1\n'
31 b'https://example.com/asottile/test/blob/main/foo#L1\n',
Anthony Sottile9db0a742017-10-06 23:32:11 -070032 )
33
Youngmin Koo1f8151a2020-11-17 02:33:47 +090034 assert main(('f.txt', '--additional-github-domain', 'example.com'))
Anthony Sottile9db0a742017-10-06 23:32:11 -070035 out, _ = capsys.readouterr()
36 assert out == (
37 'f.txt:1:https://github.com/asottile/test/blob/master/foo#L1\n'
Youngmin Koo1f8151a2020-11-17 02:33:47 +090038 'f.txt:2:https://example.com/asottile/test/blob/master/foo#L1\n'
jack114247299182021-04-06 20:07:08 +020039 'f.txt:3:https://example.com/asottile/test/blob/main/foo#L1\n'
Anthony Sottile9db0a742017-10-06 23:32:11 -070040 '\n'
41 'Non-permanent github link detected.\n'
42 'On any page on github press [y] to load a permalink.\n'
43 )