From 321e2602eb542879ced5c4735c3d81b80ae6b7fc Mon Sep 17 00:00:00 2001 From: Max Rozentsveyg Date: Mon, 4 Jun 2018 12:21:15 -0400 Subject: [PATCH] Update regex for VCS permalinks --- pre_commit_hooks/check_vcs_permalinks.py | 2 +- tests/check_vcs_permalinks_test.py | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/pre_commit_hooks/check_vcs_permalinks.py b/pre_commit_hooks/check_vcs_permalinks.py index d67d5f5..e109435 100644 --- a/pre_commit_hooks/check_vcs_permalinks.py +++ b/pre_commit_hooks/check_vcs_permalinks.py @@ -8,7 +8,7 @@ import sys GITHUB_NON_PERMALINK = re.compile( - b'https://github.com/[^/]+/[^/]+/blob/master/[^# ]+#L\d+', + b'https?://(www\.)?github.com/[^/]+/[^/]+/blob/master/[^# ]+#', ) diff --git a/tests/check_vcs_permalinks_test.py b/tests/check_vcs_permalinks_test.py index 31fd608..1b236ee 100644 --- a/tests/check_vcs_permalinks_test.py +++ b/tests/check_vcs_permalinks_test.py @@ -1,6 +1,8 @@ from __future__ import absolute_import from __future__ import unicode_literals +import pytest + from pre_commit_hooks.check_vcs_permalinks import main @@ -20,17 +22,27 @@ def test_passing(tmpdir): assert not main((f.strpath,)) -def test_failing(tmpdir, capsys): +@pytest.mark.parametrize( + 'contents', + ( + 'http://github.com/asottile/test/blob/master/foo#L1', + 'http://www.github.com/asottile/test/blob/master/foo#L1', + 'https://github.com/asottile/test/blob/master/foo#L1', + 'https://www.github.com/asottile/test/blob/master/foo#L1', + 'https://www.github.com/asottile/test/blob/master/foo#my-anchor', + ), +) +def test_failing(contents, tmpdir, capsys): with tmpdir.as_cwd(): tmpdir.join('f.txt').write_binary( - b'https://github.com/asottile/test/blob/master/foo#L1\n', + '{}\n'.format(contents).encode('utf-8'), ) assert main(('f.txt',)) out, _ = capsys.readouterr() assert out == ( - 'f.txt:1:https://github.com/asottile/test/blob/master/foo#L1\n' + 'f.txt:1:{}\n' '\n' 'Non-permanent github link detected.\n' - 'On any page on github press [y] to load a permalink.\n' + 'On any page on github press [y] to load a permalink.\n'.format(contents) )