forked from crystal-lang/crystal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Docs::Generator source link generation (crystal-lang#9119)
* Replace Docs::Generator#repository_name with ProjectInfo#name * Add ProjectInfo#refname * Add ProjectInfo#source_url_pattern * Refactor docs generator to use ProjectInfo#source_url * Add CLI options for refname and url pattern
- Loading branch information
1 parent
32a6282
commit 2898564
Showing
11 changed files
with
342 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,8 +24,8 @@ describe Crystal::Doc::ProjectInfo do | |
|
||
describe "#fill_with_defaults" do | ||
it "empty folder" do | ||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new(nil, nil)) | ||
assert_with_defaults(ProjectInfo.new("foo", "1.0"), ProjectInfo.new("foo", "1.0")) | ||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new(nil, nil, refname: nil)) | ||
assert_with_defaults(ProjectInfo.new("foo", "1.0"), ProjectInfo.new("foo", "1.0", refname: nil)) | ||
end | ||
|
||
context "with shard.yml" do | ||
|
@@ -34,17 +34,17 @@ describe Crystal::Doc::ProjectInfo do | |
end | ||
|
||
it "no git" do | ||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", "1.0")) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0")) | ||
assert_with_defaults(ProjectInfo.new(nil, "2.0"), ProjectInfo.new("foo", "2.0")) | ||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", "1.0", refname: nil)) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0", refname: nil)) | ||
assert_with_defaults(ProjectInfo.new(nil, "2.0"), ProjectInfo.new("foo", "2.0", refname: nil)) | ||
end | ||
|
||
it "git but no commit" do | ||
run_git "init" | ||
|
||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", nil)) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0")) | ||
assert_with_defaults(ProjectInfo.new(nil, "2.0"), ProjectInfo.new("foo", "2.0")) | ||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", nil, refname: nil)) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0", refname: nil)) | ||
assert_with_defaults(ProjectInfo.new(nil, "2.0"), ProjectInfo.new("foo", "2.0", refname: nil)) | ||
end | ||
|
||
it "git tagged version" do | ||
|
@@ -53,8 +53,9 @@ describe Crystal::Doc::ProjectInfo do | |
run_git "commit -m 'Initial commit' --no-gpg-sign" | ||
run_git "tag v3.0" | ||
|
||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", "3.0")) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0")) | ||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", "3.0", refname: "v3.0")) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0", refname: "v3.0")) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0", refname: "12345"), ProjectInfo.new("bar", "2.0", refname: "12345")) | ||
end | ||
|
||
it "git tagged version dirty" do | ||
|
@@ -64,19 +65,21 @@ describe Crystal::Doc::ProjectInfo do | |
run_git "tag v3.0" | ||
File.write("foo.txt", "bar") | ||
|
||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", "3.0-dev")) | ||
assert_with_defaults(ProjectInfo.new(nil, "1.1"), ProjectInfo.new("foo", "1.1")) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0")) | ||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", "3.0-dev", refname: nil)) | ||
assert_with_defaults(ProjectInfo.new(nil, "1.1"), ProjectInfo.new("foo", "1.1", refname: nil)) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0", refname: nil)) | ||
end | ||
|
||
it "git non-tagged commit" do | ||
run_git "init" | ||
run_git "add shard.yml" | ||
run_git "commit -m 'Initial commit' --no-gpg-sign" | ||
commit_sha = `git rev-parse HEAD`.chomp | ||
|
||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", "master")) | ||
assert_with_defaults(ProjectInfo.new(nil, "1.1"), ProjectInfo.new("foo", "1.1")) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0")) | ||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", "master", refname: commit_sha)) | ||
assert_with_defaults(ProjectInfo.new(nil, "1.1"), ProjectInfo.new("foo", "1.1", refname: commit_sha)) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0", refname: commit_sha)) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0", refname: "12345"), ProjectInfo.new("bar", "2.0", refname: "12345")) | ||
end | ||
|
||
it "git non-tagged commit dirty" do | ||
|
@@ -85,9 +88,20 @@ describe Crystal::Doc::ProjectInfo do | |
run_git "commit -m 'Initial commit' --no-gpg-sign" | ||
File.write("foo.txt", "bar") | ||
|
||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", "master-dev")) | ||
assert_with_defaults(ProjectInfo.new(nil, "1.1"), ProjectInfo.new("foo", "1.1")) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0")) | ||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", "master-dev", refname: nil)) | ||
assert_with_defaults(ProjectInfo.new(nil, "1.1"), ProjectInfo.new("foo", "1.1", refname: nil)) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0", refname: nil)) | ||
end | ||
|
||
it "git with remote" do | ||
run_git "init" | ||
run_git "remote add origin [email protected]:foo/bar" | ||
|
||
url_pattern = "https://github.com/foo/bar/blob/%{refname}/%{path}#L%{line}" | ||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new("foo", nil, refname: nil, source_url_pattern: url_pattern)) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0", refname: nil, source_url_pattern: url_pattern)) | ||
assert_with_defaults(ProjectInfo.new(nil, "2.0"), ProjectInfo.new("foo", "2.0", refname: nil, source_url_pattern: url_pattern)) | ||
assert_with_defaults(ProjectInfo.new(nil, "2.0", source_url_pattern: "foo_bar"), ProjectInfo.new("foo", "2.0", refname: nil, source_url_pattern: "foo_bar")) | ||
end | ||
end | ||
|
||
|
@@ -98,9 +112,10 @@ describe Crystal::Doc::ProjectInfo do | |
run_git "commit -m 'Remove shard.yml' --no-gpg-sign" | ||
run_git "tag v4.0" | ||
|
||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new(nil, "4.0")) | ||
assert_with_defaults(ProjectInfo.new("foo", nil), ProjectInfo.new("foo", "4.0")) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0")) | ||
assert_with_defaults(ProjectInfo.new(nil, nil), ProjectInfo.new(nil, "4.0", refname: "v4.0")) | ||
assert_with_defaults(ProjectInfo.new("foo", nil), ProjectInfo.new("foo", "4.0", refname: "v4.0")) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0"), ProjectInfo.new("bar", "2.0", refname: "v4.0")) | ||
assert_with_defaults(ProjectInfo.new("bar", "2.0", refname: "12345"), ProjectInfo.new("bar", "2.0", refname: "12345")) | ||
end | ||
end | ||
|
||
|
@@ -149,6 +164,40 @@ describe Crystal::Doc::ProjectInfo do | |
ProjectInfo.find_git_version.should eq "0.1.0" | ||
end | ||
|
||
describe ".git_remote" do | ||
it "no git workdir" do | ||
ProjectInfo.git_remote.should be_nil | ||
end | ||
|
||
it "no remote" do | ||
run_git "init" | ||
ProjectInfo.git_remote.should be_nil | ||
end | ||
|
||
it "simple origin" do | ||
run_git "init" | ||
run_git "remote add origin https://example.com/foo.git" | ||
ProjectInfo.git_remote.should eq "https://example.com/foo.git" | ||
end | ||
|
||
it "origin plus other" do | ||
run_git "init" | ||
run_git "remote add bar https://example.com/bar.git" | ||
run_git "remote add origin https://example.com/foo.git" | ||
run_git "remote add baz https://example.com/baz.git" | ||
`git remote -v` | ||
ProjectInfo.git_remote.should eq "https://example.com/foo.git" | ||
end | ||
|
||
it "no origin remote" do | ||
run_git "init" | ||
run_git "remote add bar https://example.com/bar.git" | ||
run_git "remote add baz https://example.com/baz.git" | ||
`git remote -v` | ||
ProjectInfo.git_remote.should eq "https://example.com/bar.git" | ||
end | ||
end | ||
|
||
describe ".read_shard_properties" do | ||
it "no shard.yml" do | ||
ProjectInfo.read_shard_properties.should eq({nil, nil}) | ||
|
@@ -202,4 +251,73 @@ describe Crystal::Doc::ProjectInfo do | |
ProjectInfo.read_shard_properties.should eq({nil, nil}) | ||
end | ||
end | ||
|
||
it ".find_source_url_pattern" do | ||
ProjectInfo.find_source_url_pattern("no a uri").should be_nil | ||
ProjectInfo.find_source_url_pattern("[email protected]:foo/bar").should be_nil | ||
ProjectInfo.find_source_url_pattern("http://example.com/foo/bar").should be_nil | ||
|
||
ProjectInfo.find_source_url_pattern("[email protected]:foo/bar/").should eq "https://github.com/foo/bar/blob/%{refname}/%{path}#L%{line}" | ||
ProjectInfo.find_source_url_pattern("[email protected]:foo/bar.git").should eq "https://github.com/foo/bar.git/blob/%{refname}/%{path}#L%{line}" | ||
|
||
ProjectInfo.find_source_url_pattern("[email protected]:foo/bar").should eq "https://github.com/foo/bar/blob/%{refname}/%{path}#L%{line}" | ||
ProjectInfo.find_source_url_pattern("http://github.com/foo/bar").should eq "https://github.com/foo/bar/blob/%{refname}/%{path}#L%{line}" | ||
ProjectInfo.find_source_url_pattern("https://github.com/foo/bar").should eq "https://github.com/foo/bar/blob/%{refname}/%{path}#L%{line}" | ||
ProjectInfo.find_source_url_pattern("http://www.github.com/foo/bar").should eq "https://github.com/foo/bar/blob/%{refname}/%{path}#L%{line}" | ||
ProjectInfo.find_source_url_pattern("https://www.github.com/foo/bar").should eq "https://github.com/foo/bar/blob/%{refname}/%{path}#L%{line}" | ||
|
||
ProjectInfo.find_source_url_pattern("https://github.com/foo/bar.git").should eq "https://github.com/foo/bar.git/blob/%{refname}/%{path}#L%{line}" | ||
ProjectInfo.find_source_url_pattern("https://github.com/foo/bar.cr").should eq "https://github.com/foo/bar.cr/blob/%{refname}/%{path}#L%{line}" | ||
ProjectInfo.find_source_url_pattern("https://github.com/foo/bar.cr.git").should eq "https://github.com/foo/bar.cr.git/blob/%{refname}/%{path}#L%{line}" | ||
|
||
ProjectInfo.find_source_url_pattern("[email protected]:foo/bar").should eq "https://gitlab.com/foo/bar/blob/%{refname}/%{path}#L%{line}" | ||
ProjectInfo.find_source_url_pattern("http://gitlab.com/foo/bar").should eq "https://gitlab.com/foo/bar/blob/%{refname}/%{path}#L%{line}" | ||
|
||
ProjectInfo.find_source_url_pattern("[email protected]:foo/bar").should eq "https://bitbucket.com/foo/bar/src/%{refname}/%{path}#%{filename}-%{line}" | ||
ProjectInfo.find_source_url_pattern("http://bitbucket.com/foo/bar").should eq "https://bitbucket.com/foo/bar/src/%{refname}/%{path}#%{filename}-%{line}" | ||
|
||
ProjectInfo.find_source_url_pattern("[email protected]:~foo/bar").should eq "https://git.sr.ht/~foo/bar/tree/%{refname}/%{path}#L%{line}" | ||
ProjectInfo.find_source_url_pattern("http://git.sr.ht/~foo/bar").should eq "https://git.sr.ht/~foo/bar/tree/%{refname}/%{path}#L%{line}" | ||
end | ||
|
||
describe "#source_url" do | ||
it "fails if refname is missing" do | ||
location = Crystal::Doc::RelativeLocation.new("foo/bar.baz", 42) | ||
info = ProjectInfo.new("test", "v1.0", refname: nil, source_url_pattern: "http://git.example.com/test.git/src/%{refname}/%{path}#L%{line}") | ||
info.source_url(location).should be_nil | ||
end | ||
|
||
it "fails if pattern is missing" do | ||
location = Crystal::Doc::RelativeLocation.new("foo/bar.baz", 42) | ||
info = ProjectInfo.new("test", "v1.0", refname: "master") | ||
info.source_url(location).should be_nil | ||
end | ||
|
||
it "builds url" do | ||
info = ProjectInfo.new("test", "v1.0", refname: "master", source_url_pattern: "http://git.example.com/test.git/src/%{refname}/%{path}#L%{line}") | ||
location = Crystal::Doc::RelativeLocation.new("foo/bar.baz", 42) | ||
info.source_url(location).should eq "http://git.example.com/test.git/src/master/foo/bar.baz#L42" | ||
end | ||
|
||
it "returns nil for empty pattern" do | ||
info = ProjectInfo.new("test", "v1.0", refname: "master", source_url_pattern: "") | ||
location = Crystal::Doc::RelativeLocation.new("foo/bar.baz", 42) | ||
info.source_url(location).should be_nil | ||
end | ||
|
||
it "fails if pattern is missing" do | ||
location = Crystal::Doc::RelativeLocation.new("foo/bar.baz", 42) | ||
info = ProjectInfo.new("test", "v1.0") | ||
info.refname = "master" | ||
info.source_url(location).should be_nil | ||
end | ||
|
||
it "builds url" do | ||
info = ProjectInfo.new("test", "v1.0") | ||
info.refname = "master" | ||
info.source_url_pattern = "http://git.example.com/test.git/src/%{refname}/%{path}#L%{line}" | ||
location = Crystal::Doc::RelativeLocation.new("foo/bar.baz", 42) | ||
info.source_url(location).should eq "http://git.example.com/test.git/src/master/foo/bar.baz#L42" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,6 @@ | ||
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 "origin\thttps://github.com/foo/bar.cr.git (fetch)\n", user: "foo", repo: "bar.cr" | ||
assert_matches_pattern "origin\t[email protected]/foo/bar.cr.git (fetch)\n", 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 "origin\thttps://gitlab.com/foo/bar.cr.git (fetch)\n", user: "foo", repo: "bar.cr" | ||
assert_matches_pattern "origin\t[email protected]/foo/bar.cr.git (fetch)\n", 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 | ||
|
||
describe ".anchor_link" do | ||
it "generates the correct anchor link" do | ||
Crystal::Doc.anchor_link("anchor").should eq( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.