Skip to content

Commit c1a6c2d

Browse files
committed
Add specs for hg resolver
1 parent b11bb08 commit c1a6c2d

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-0
lines changed

spec/support/factories.cr

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,78 @@ def checkout_git_branch(project, branch)
8080
end
8181
end
8282

83+
def create_hg_repository(project, *versions)
84+
Dir.cd(tmp_path) do
85+
run "hg init #{Process.quote(project)}"
86+
end
87+
88+
Dir.mkdir(File.join(hg_path(project), "src"))
89+
File.write(File.join(hg_path(project), "src", "#{project}.cr"), "module #{project.capitalize}\nend")
90+
91+
Dir.cd(hg_path(project)) do
92+
run "hg add #{Process.quote("src/#{project}.cr")}"
93+
end
94+
95+
versions.each { |version| create_hg_release project, version }
96+
end
97+
98+
def create_fork_hg_repository(project, upstream)
99+
Dir.cd(tmp_path) do
100+
run "hg clone #{Process.quote(hg_url(upstream))} #{Process.quote(project)}"
101+
end
102+
end
103+
104+
def create_hg_version_commit(project, version, shard : Bool | NamedTuple = true)
105+
Dir.cd(hg_path(project)) do
106+
if shard
107+
contents = shard.is_a?(NamedTuple) ? shard : nil
108+
create_shard project, version, contents
109+
end
110+
Dir.cd(hg_path(project)) do
111+
name = shard[:name]? if shard.is_a?(NamedTuple)
112+
name ||= project
113+
File.touch "src/#{name}.cr"
114+
run "hg add #{Process.quote("src/#{name}.cr")}"
115+
end
116+
create_hg_commit project, "release: v#{version}"
117+
end
118+
end
119+
120+
def create_hg_release(project, version, shard : Bool | NamedTuple = true)
121+
create_hg_version_commit(project, version, shard)
122+
create_hg_tag(project, "v#{version}")
123+
end
124+
125+
def create_hg_tag(project, version)
126+
Dir.cd(hg_path(project)) do
127+
run "hg tag #{Process.quote(version)}"
128+
end
129+
end
130+
131+
def create_hg_commit(project, message = "new commit")
132+
Dir.cd(hg_path(project)) do
133+
run "hg commit -A -m #{Process.quote(message)}"
134+
end
135+
end
136+
137+
def checkout_new_hg_bookmark(project, branch)
138+
Dir.cd(hg_path(project)) do
139+
run "hg bookmark #{Process.quote(branch)}"
140+
end
141+
end
142+
143+
def checkout_new_hg_branch(project, branch)
144+
Dir.cd(hg_path(project)) do
145+
run "hg branch #{Process.quote(branch)}"
146+
end
147+
end
148+
149+
def checkout_hg_rev(project, rev)
150+
Dir.cd(hg_path(project)) do
151+
run "hg update -C #{Process.quote(rev)}"
152+
end
153+
end
154+
83155
def create_shard(project, version, contents : NamedTuple? = nil)
84156
spec = {name: project, version: version, crystal: Shards.crystal_version}
85157
spec = spec.merge(contents) if contents
@@ -116,6 +188,20 @@ def git_path(project)
116188
File.join(tmp_path, project.to_s)
117189
end
118190

191+
def hg_commits(project, rev = ".")
192+
Dir.cd(hg_path(project)) do
193+
run("hg log --template='{node}\n' -r #{Process.quote(rev)}").strip.split('\n')
194+
end
195+
end
196+
197+
def hg_url(project)
198+
"file://#{Path[hg_path(project)].to_posix}"
199+
end
200+
201+
def hg_path(project)
202+
File.join(tmp_path, project.to_s)
203+
end
204+
119205
def rel_path(project)
120206
"../../spec/.repositories/#{project}"
121207
end

spec/support/requirement.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ def commit(sha1)
66
Shards::GitCommitRef.new(sha1)
77
end
88

9+
def hg_branch(name)
10+
Shards::HgBranchRef.new(name)
11+
end
12+
913
def version(version)
1014
Shards::Version.new(version)
1115
end

spec/unit/hg_resolver_spec.cr

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
require "./spec_helper"
2+
3+
private def resolver(name)
4+
Shards::HgResolver.new(name, hg_url(name))
5+
end
6+
7+
module Shards
8+
# Allow overriding `source` for the specs
9+
class HgResolver
10+
def source=(@source)
11+
end
12+
end
13+
14+
describe HgResolver do
15+
before_each do
16+
create_hg_repository "empty"
17+
create_hg_commit "empty", "initial release"
18+
19+
create_hg_repository "unreleased"
20+
create_hg_version_commit "unreleased", "0.1.0"
21+
checkout_new_hg_branch "unreleased", "branch"
22+
create_hg_commit "unreleased", "testing"
23+
checkout_hg_rev "unreleased", "default"
24+
25+
create_hg_repository "library", "0.0.1", "0.1.0", "0.1.1", "0.1.2", "0.2.0"
26+
27+
# Create a version tag not prefixed by 'v' which should be ignored
28+
create_hg_tag "library", "99.9.9"
29+
end
30+
31+
it "available releases" do
32+
resolver("empty").available_releases.should be_empty
33+
resolver("library").available_releases.should eq(versions ["0.0.1", "0.1.0", "0.1.1", "0.1.2", "0.2.0"])
34+
end
35+
36+
it "latest version for ref" do
37+
expect_raises(Shards::Error, "No shard.yml was found for shard \"empty\" at commit #{hg_commits(:empty)[0]}") do
38+
resolver("empty").latest_version_for_ref(hg_branch "default")
39+
end
40+
expect_raises(Shards::Error, "No shard.yml was found for shard \"empty\" at commit #{hg_commits(:empty)[0]}") do
41+
resolver("empty").latest_version_for_ref(nil)
42+
end
43+
resolver("unreleased").latest_version_for_ref(hg_branch "default").should eq(version "0.1.0+hg.commit.#{hg_commits(:unreleased)[0]}")
44+
resolver("unreleased").latest_version_for_ref(hg_branch "branch").should eq(version "0.1.0+hg.commit.#{hg_commits(:unreleased, "branch")[0]}")
45+
resolver("unreleased").latest_version_for_ref(nil).should eq(version "0.1.0+hg.commit.#{hg_commits(:unreleased)[0]}")
46+
resolver("library").latest_version_for_ref(hg_branch "default").should eq(version "0.2.0+hg.commit.#{hg_commits(:library)[0]}")
47+
resolver("library").latest_version_for_ref(nil).should eq(version "0.2.0+hg.commit.#{hg_commits(:library)[0]}")
48+
expect_raises(Shards::Error, "Could not find branch foo for shard \"library\" in the repository #{hg_url(:library)}") do
49+
resolver("library").latest_version_for_ref(hg_branch "foo")
50+
end
51+
end
52+
53+
it "versions for" do
54+
expect_raises(Shards::Error, "No shard.yml was found for shard \"empty\" at commit #{hg_commits(:empty)[0]}") do
55+
resolver("empty").versions_for(Any)
56+
end
57+
resolver("library").versions_for(Any).should eq(versions ["0.0.1", "0.1.0", "0.1.1", "0.1.2", "0.2.0"])
58+
resolver("library").versions_for(VersionReq.new "~> 0.1.0").should eq(versions ["0.1.0", "0.1.1", "0.1.2"])
59+
resolver("library").versions_for(hg_branch "default").should eq(versions ["0.2.0+hg.commit.#{hg_commits(:library)[0]}"])
60+
resolver("unreleased").versions_for(hg_branch "default").should eq(versions ["0.1.0+hg.commit.#{hg_commits(:unreleased)[0]}"])
61+
resolver("unreleased").versions_for(Any).should eq(versions ["0.1.0+hg.commit.#{hg_commits(:unreleased)[0]}"])
62+
end
63+
64+
it "read spec for release" do
65+
spec = resolver("library").spec(version "0.1.1")
66+
spec.original_version.should eq(version "0.1.1")
67+
spec.version.should eq(version "0.1.1")
68+
end
69+
70+
it "read spec for commit" do
71+
version = version("0.2.0+hg.commit.#{hg_commits(:library)[0]}")
72+
spec = resolver("library").spec(version)
73+
spec.original_version.should eq(version "0.2.0")
74+
spec.version.should eq(version)
75+
end
76+
77+
it "install" do
78+
library = resolver("library")
79+
80+
library.install_sources(version("0.1.2"), install_path("library"))
81+
File.exists?(install_path("library", "src/library.cr")).should be_true
82+
File.exists?(install_path("library", "shard.yml")).should be_true
83+
Spec.from_file(install_path("library", "shard.yml")).version.should eq(version "0.1.2")
84+
85+
library.install_sources(version("0.2.0"), install_path("library"))
86+
Spec.from_file(install_path("library", "shard.yml")).version.should eq(version "0.2.0")
87+
end
88+
89+
it "install commit" do
90+
library = resolver("library")
91+
version = version "0.2.0+hg.commit.#{hg_commits(:library)[0]}"
92+
library.install_sources(version, install_path("library"))
93+
Spec.from_file(install_path("library", "shard.yml")).version.should eq(version "0.2.0")
94+
end
95+
96+
it "origin changed" do
97+
library = HgResolver.new("library", hg_url("library"))
98+
library.install_sources(version("0.1.2"), install_path("library"))
99+
100+
# Change the origin in the cache repo to https://foss.heptapod.net/foo/bar
101+
hgrc_path = File.join(library.local_path, ".hg", "hgrc")
102+
hgrc = File.read(hgrc_path)
103+
hgrc = hgrc.gsub(/(default\s*=\s*)([^\r\n]*)/, "\\1https://foss.heptapod.net/foo/bar")
104+
File.write(hgrc_path, hgrc)
105+
#
106+
# All of these alternatives should not trigger origin as changed
107+
same_origins = [
108+
"https://foss.heptapod.net/foo/bar",
109+
"https://foss.heptapod.net:1234/foo/bar",
110+
"http://foss.heptapod.net/foo/bar",
111+
"ssh://foss.heptapod.net/foo/bar",
112+
"hg://foss.heptapod.net/foo/bar",
113+
"rsync://foss.heptapod.net/foo/bar",
114+
"[email protected]:foo/bar",
115+
"[email protected]:foo/bar",
116+
"foss.heptapod.net:foo/bar",
117+
]
118+
119+
same_origins.each do |origin|
120+
library.source = origin
121+
library.origin_changed?.should be_false
122+
end
123+
124+
# These alternatives should all trigger origin as changed
125+
changed_origins = [
126+
"https://foss.heptapod.net/foo/bar2",
127+
"https://foss.heptapod.net/foos/bar",
128+
"https://hghubz.com/foo/bar",
129+
"file:///foss.heptapod.net/foo/bar",
130+
"[email protected]:foo/bar2",
131+
"[email protected]:foo/bar",
132+
"",
133+
]
134+
135+
changed_origins.each do |origin|
136+
library.source = origin
137+
library.origin_changed?.should be_true
138+
end
139+
end
140+
141+
it "renders report version" do
142+
resolver("library").report_version(version "1.2.3").should eq("1.2.3")
143+
resolver("library").report_version(version "1.2.3+hg.commit.654875c9dbfa8d72fba70d65fd548d51ffb85aff").should eq("1.2.3 at 654875c")
144+
end
145+
146+
it "#matches_ref" do
147+
resolver = HgResolver.new("", "")
148+
resolver.matches_ref?(HgCommitRef.new("1234567890abcdef"), Shards::Version.new("0.1.0.+hg.commit.1234567")).should be_true
149+
resolver.matches_ref?(HgCommitRef.new("1234567890abcdef"), Shards::Version.new("0.1.0.+hg.commit.1234567890abcdef")).should be_true
150+
resolver.matches_ref?(HgCommitRef.new("1234567"), Shards::Version.new("0.1.0.+hg.commit.1234567890abcdef")).should be_true
151+
end
152+
end
153+
end

0 commit comments

Comments
 (0)