Skip to content

Commit 9e67166

Browse files
Sijabcardiff
authored andcommittedMar 17, 2017
GIT_REMOTE_PATTERNS regexes are being overly greedy (#4132)
* GIT_REMOTE_PATTERNS regexes are being overly greedy With given remote url: “https://github.com/foo/bar.cr.git” - Before: {repo: “bar”} - Now: {repo: “bar.cr”} * Add specs for Crystal::Doc::Generator::GIT_REMOTE_PATTERNS
1 parent c2ceee7 commit 9e67166

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed
 
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require "../../../spec_helper"
2+
3+
private def assert_matches_pattern(url, **options)
4+
match = Crystal::Doc::Generator::GIT_REMOTE_PATTERNS.each_key.compact_map(&.match(url)).first?
5+
if match
6+
options.each { |k, v| match[k.to_s].should eq(v) }
7+
end
8+
end
9+
10+
describe Crystal::Doc::Generator do
11+
describe "GIT_REMOTE_PATTERNS" do
12+
it "matches github repos" do
13+
assert_matches_pattern "https://www.github.com/foo/bar", user: "foo", repo: "bar"
14+
assert_matches_pattern "http://www.github.com/foo/bar", user: "foo", repo: "bar"
15+
assert_matches_pattern "http://github.com/foo/bar", user: "foo", repo: "bar"
16+
17+
assert_matches_pattern "https://github.com/foo/bar", user: "foo", repo: "bar"
18+
assert_matches_pattern "https://github.com/foo/bar.git", user: "foo", repo: "bar"
19+
assert_matches_pattern "https://github.com/foo/bar.cr", user: "foo", repo: "bar.cr"
20+
assert_matches_pattern "https://github.com/foo/bar.cr.git", user: "foo", repo: "bar.cr"
21+
22+
assert_matches_pattern "https://github.com/fOO-Bar/w00den-baRK.ab.cd", user: "fOO-Bar", repo: "w00den-baRK.ab.cd"
23+
assert_matches_pattern "https://github.com/fOO-Bar/w00den-baRK.ab.cd.git", user: "fOO-Bar", repo: "w00den-baRK.ab.cd"
24+
assert_matches_pattern "https://github.com/foo_bar/_baz-buzz.cx", user: "foo_bar", repo: "_baz-buzz.cx"
25+
end
26+
27+
it "matches gitlab repos" do
28+
assert_matches_pattern "https://www.gitlab.com/foo/bar", user: "foo", repo: "bar"
29+
assert_matches_pattern "http://www.gitlab.com/foo/bar", user: "foo", repo: "bar"
30+
assert_matches_pattern "http://gitlab.com/foo/bar", user: "foo", repo: "bar"
31+
32+
assert_matches_pattern "https://gitlab.com/foo/bar", user: "foo", repo: "bar"
33+
assert_matches_pattern "https://gitlab.com/foo/bar.git", user: "foo", repo: "bar"
34+
assert_matches_pattern "https://gitlab.com/foo/bar.cr", user: "foo", repo: "bar.cr"
35+
assert_matches_pattern "https://gitlab.com/foo/bar.cr.git", user: "foo", repo: "bar.cr"
36+
37+
assert_matches_pattern "https://gitlab.com/fOO-Bar/w00den-baRK.ab.cd", user: "fOO-Bar", repo: "w00den-baRK.ab.cd"
38+
assert_matches_pattern "https://gitlab.com/fOO-Bar/w00den-baRK.ab.cd.git", user: "fOO-Bar", repo: "w00den-baRK.ab.cd"
39+
assert_matches_pattern "https://gitlab.com/foo_bar/_baz-buzz.cx", user: "foo_bar", repo: "_baz-buzz.cx"
40+
end
41+
end
42+
end

‎src/compiler/crystal/tools/doc/generator.cr

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ class Crystal::Doc::Generator
1818
FLAGS = FLAG_COLORS.keys
1919

2020
GIT_REMOTE_PATTERNS = {
21-
/github\.com(?:\:|\/)(?<user>(?:\w|-|_)+)\/(?<repo>(?:\w|-|_|\.)+?)(?:.git)?\b/ => {
21+
/github\.com(?:\:|\/)(?<user>(?:\w|-|_)+)\/(?<repo>(?:\w|-|_|\.)+?)(?:\.git)?$/ => {
2222
repository: "https://github.com/%{user}/%{repo}/blob/%{rev}",
2323
repo_name: "github.com/%{user}/%{repo}",
2424
},
25-
/gitlab\.com(?:\:|\/)(?<user>(?:\w|-|_|\.)+)\/(?<repo>(?:\w|-|_|\.)+?)(?:.git)?\b/ => {
25+
/gitlab\.com(?:\:|\/)(?<user>(?:\w|-|_|\.)+)\/(?<repo>(?:\w|-|_|\.)+?)(?:\.git)?$/ => {
2626
repository: "https://gitlab.com/%{user}/%{repo}/blob/%{rev}",
2727
repo_name: "gitlab.com/%{user}/%{repo}",
2828
},

0 commit comments

Comments
 (0)
Please sign in to comment.