Skip to content

Commit

Permalink
GIT_REMOTE_PATTERNS regexes are being overly greedy (#4132)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Sija authored and bcardiff committed Mar 17, 2017
1 parent c2ceee7 commit 9e67166
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
42 changes: 42 additions & 0 deletions spec/compiler/crystal/tools/doc_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "../../../spec_helper"

private def assert_matches_pattern(url, **options)
match = Crystal::Doc::Generator::GIT_REMOTE_PATTERNS.each_key.compact_map(&.match(url)).first?
if match
options.each { |k, v| match[k.to_s].should eq(v) }
end
end

describe Crystal::Doc::Generator do
describe "GIT_REMOTE_PATTERNS" do
it "matches github repos" do
assert_matches_pattern "https://www.github.com/foo/bar", user: "foo", repo: "bar"
assert_matches_pattern "http://www.github.com/foo/bar", user: "foo", repo: "bar"
assert_matches_pattern "http://github.com/foo/bar", user: "foo", repo: "bar"

assert_matches_pattern "https://github.com/foo/bar", user: "foo", repo: "bar"
assert_matches_pattern "https://github.com/foo/bar.git", user: "foo", repo: "bar"
assert_matches_pattern "https://github.com/foo/bar.cr", user: "foo", repo: "bar.cr"
assert_matches_pattern "https://github.com/foo/bar.cr.git", user: "foo", repo: "bar.cr"

assert_matches_pattern "https://github.com/fOO-Bar/w00den-baRK.ab.cd", user: "fOO-Bar", repo: "w00den-baRK.ab.cd"
assert_matches_pattern "https://github.com/fOO-Bar/w00den-baRK.ab.cd.git", user: "fOO-Bar", repo: "w00den-baRK.ab.cd"
assert_matches_pattern "https://github.com/foo_bar/_baz-buzz.cx", user: "foo_bar", repo: "_baz-buzz.cx"
end

it "matches gitlab repos" do
assert_matches_pattern "https://www.gitlab.com/foo/bar", user: "foo", repo: "bar"
assert_matches_pattern "http://www.gitlab.com/foo/bar", user: "foo", repo: "bar"
assert_matches_pattern "http://gitlab.com/foo/bar", user: "foo", repo: "bar"

assert_matches_pattern "https://gitlab.com/foo/bar", user: "foo", repo: "bar"
assert_matches_pattern "https://gitlab.com/foo/bar.git", user: "foo", repo: "bar"
assert_matches_pattern "https://gitlab.com/foo/bar.cr", user: "foo", repo: "bar.cr"
assert_matches_pattern "https://gitlab.com/foo/bar.cr.git", user: "foo", repo: "bar.cr"

assert_matches_pattern "https://gitlab.com/fOO-Bar/w00den-baRK.ab.cd", user: "fOO-Bar", repo: "w00den-baRK.ab.cd"
assert_matches_pattern "https://gitlab.com/fOO-Bar/w00den-baRK.ab.cd.git", user: "fOO-Bar", repo: "w00den-baRK.ab.cd"
assert_matches_pattern "https://gitlab.com/foo_bar/_baz-buzz.cx", user: "foo_bar", repo: "_baz-buzz.cx"
end
end
end
4 changes: 2 additions & 2 deletions src/compiler/crystal/tools/doc/generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class Crystal::Doc::Generator
FLAGS = FLAG_COLORS.keys

GIT_REMOTE_PATTERNS = {
/github\.com(?:\:|\/)(?<user>(?:\w|-|_)+)\/(?<repo>(?:\w|-|_|\.)+?)(?:.git)?\b/ => {
/github\.com(?:\:|\/)(?<user>(?:\w|-|_)+)\/(?<repo>(?:\w|-|_|\.)+?)(?:\.git)?$/ => {
repository: "https://github.com/%{user}/%{repo}/blob/%{rev}",
repo_name: "github.com/%{user}/%{repo}",
},
/gitlab\.com(?:\:|\/)(?<user>(?:\w|-|_|\.)+)\/(?<repo>(?:\w|-|_|\.)+?)(?:.git)?\b/ => {
/gitlab\.com(?:\:|\/)(?<user>(?:\w|-|_|\.)+)\/(?<repo>(?:\w|-|_|\.)+?)(?:\.git)?$/ => {
repository: "https://gitlab.com/%{user}/%{repo}/blob/%{rev}",
repo_name: "gitlab.com/%{user}/%{repo}",
},
Expand Down

0 comments on commit 9e67166

Please sign in to comment.