|
| 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 "unreleased-bm" |
| 26 | + create_hg_version_commit "unreleased-bm", "0.1.0" |
| 27 | + checkout_new_hg_bookmark "unreleased-bm", "branch" |
| 28 | + create_hg_commit "unreleased-bm", "testing" |
| 29 | + checkout_hg_rev "unreleased-bm", "default" |
| 30 | + |
| 31 | + create_hg_repository "library", "0.0.1", "0.1.0", "0.1.1", "0.1.2", "0.2.0" |
| 32 | + |
| 33 | + # Create a version tag not prefixed by 'v' which should be ignored |
| 34 | + create_hg_tag "library", "99.9.9" |
| 35 | + end |
| 36 | + |
| 37 | + it "normalizes github bitbucket gitlab sources" do |
| 38 | + # don't normalise other domains |
| 39 | + HgResolver.normalize_key_source("hg", "HTTPs://myhgserver.com/Repo").should eq({"hg", "HTTPs://myhgserver.com/Repo"}) |
| 40 | + |
| 41 | + # don't change protocol from ssh |
| 42 | + HgResolver.normalize_key_source( "hg", "ssh://[email protected]/Repo").should eq({ "hg", "ssh://[email protected]/Repo"}) |
| 43 | + end |
| 44 | + |
| 45 | + it "available releases" do |
| 46 | + resolver("empty").available_releases.should be_empty |
| 47 | + resolver("library").available_releases.should eq(versions ["0.0.1", "0.1.0", "0.1.1", "0.1.2", "0.2.0"]) |
| 48 | + end |
| 49 | + |
| 50 | + it "latest version for ref" do |
| 51 | + expect_raises(Shards::Error, "No shard.yml was found for shard \"empty\" at commit #{hg_commits(:empty)[0]}") do |
| 52 | + resolver("empty").latest_version_for_ref(hg_branch "default") |
| 53 | + end |
| 54 | + expect_raises(Shards::Error, "No shard.yml was found for shard \"empty\" at commit #{hg_commits(:empty)[0]}") do |
| 55 | + resolver("empty").latest_version_for_ref(nil) |
| 56 | + end |
| 57 | + resolver("unreleased").latest_version_for_ref(hg_branch "default").should eq(version "0.1.0+hg.commit.#{hg_commits(:unreleased)[0]}") |
| 58 | + resolver("unreleased").latest_version_for_ref(hg_branch "branch").should eq(version "0.1.0+hg.commit.#{hg_commits(:unreleased, "branch")[0]}") |
| 59 | + resolver("unreleased").latest_version_for_ref(nil).should eq(version "0.1.0+hg.commit.#{hg_commits(:unreleased)[0]}") |
| 60 | + resolver("unreleased-bm").latest_version_for_ref(hg_branch "default").should eq(version "0.1.0+hg.commit.#{hg_commits("unreleased-bm")[0]}") |
| 61 | + resolver("unreleased-bm").latest_version_for_ref(hg_bookmark "branch").should eq(version "0.1.0+hg.commit.#{hg_commits("unreleased-bm", "branch")[0]}") |
| 62 | + resolver("unreleased-bm").latest_version_for_ref(nil).should eq(version "0.1.0+hg.commit.#{hg_commits("unreleased-bm")[0]}") |
| 63 | + resolver("library").latest_version_for_ref(hg_branch "default").should eq(version "0.2.0+hg.commit.#{hg_commits(:library)[0]}") |
| 64 | + resolver("library").latest_version_for_ref(nil).should eq(version "0.2.0+hg.commit.#{hg_commits(:library)[0]}") |
| 65 | + expect_raises(Shards::Error, "Could not find branch foo for shard \"library\" in the repository #{hg_url(:library)}") do |
| 66 | + resolver("library").latest_version_for_ref(hg_branch "foo") |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + it "versions for" do |
| 71 | + expect_raises(Shards::Error, "No shard.yml was found for shard \"empty\" at commit #{hg_commits(:empty)[0]}") do |
| 72 | + resolver("empty").versions_for(Any) |
| 73 | + end |
| 74 | + resolver("library").versions_for(Any).should eq(versions ["0.0.1", "0.1.0", "0.1.1", "0.1.2", "0.2.0"]) |
| 75 | + resolver("library").versions_for(VersionReq.new "~> 0.1.0").should eq(versions ["0.1.0", "0.1.1", "0.1.2"]) |
| 76 | + resolver("library").versions_for(hg_branch "default").should eq(versions ["0.2.0+hg.commit.#{hg_commits(:library)[0]}"]) |
| 77 | + resolver("unreleased").versions_for(hg_branch "default").should eq(versions ["0.1.0+hg.commit.#{hg_commits(:unreleased)[0]}"]) |
| 78 | + resolver("unreleased").versions_for(Any).should eq(versions ["0.1.0+hg.commit.#{hg_commits(:unreleased)[0]}"]) |
| 79 | + resolver("unreleased-bm").versions_for(hg_branch "default").should eq(versions ["0.1.0+hg.commit.#{hg_commits("unreleased-bm")[0]}"]) |
| 80 | + resolver("unreleased-bm").versions_for(Any).should eq(versions ["0.1.0+hg.commit.#{hg_commits("unreleased-bm")[0]}"]) |
| 81 | + end |
| 82 | + |
| 83 | + it "read spec for release" do |
| 84 | + spec = resolver("library").spec(version "0.1.1") |
| 85 | + spec.original_version.should eq(version "0.1.1") |
| 86 | + spec.version.should eq(version "0.1.1") |
| 87 | + end |
| 88 | + |
| 89 | + it "read spec for commit" do |
| 90 | + version = version("0.2.0+hg.commit.#{hg_commits(:library)[0]}") |
| 91 | + spec = resolver("library").spec(version) |
| 92 | + spec.original_version.should eq(version "0.2.0") |
| 93 | + spec.version.should eq(version) |
| 94 | + end |
| 95 | + |
| 96 | + it "install" do |
| 97 | + library = resolver("library") |
| 98 | + |
| 99 | + library.install_sources(version("0.1.2"), install_path("library")) |
| 100 | + File.exists?(install_path("library", "src/library.cr")).should be_true |
| 101 | + File.exists?(install_path("library", "shard.yml")).should be_true |
| 102 | + Spec.from_file(install_path("library", "shard.yml")).version.should eq(version "0.1.2") |
| 103 | + |
| 104 | + library.install_sources(version("0.2.0"), install_path("library")) |
| 105 | + Spec.from_file(install_path("library", "shard.yml")).version.should eq(version "0.2.0") |
| 106 | + end |
| 107 | + |
| 108 | + it "install commit" do |
| 109 | + library = resolver("library") |
| 110 | + version = version "0.2.0+hg.commit.#{hg_commits(:library)[0]}" |
| 111 | + library.install_sources(version, install_path("library")) |
| 112 | + Spec.from_file(install_path("library", "shard.yml")).version.should eq(version "0.2.0") |
| 113 | + end |
| 114 | + |
| 115 | + it "origin changed" do |
| 116 | + library = HgResolver.new("library", hg_url("library")) |
| 117 | + library.install_sources(version("0.1.2"), install_path("library")) |
| 118 | + |
| 119 | + # Change the origin in the cache repo to https://foss.heptapod.net/foo/bar |
| 120 | + hgrc_path = File.join(library.local_path, ".hg", "hgrc") |
| 121 | + hgrc = File.read(hgrc_path) |
| 122 | + hgrc = hgrc.gsub(/(default\s*=\s*)([^\r\n]*)/, "\\1https://foss.heptapod.net/foo/bar") |
| 123 | + File.write(hgrc_path, hgrc) |
| 124 | + # |
| 125 | + # All of these alternatives should not trigger origin as changed |
| 126 | + same_origins = [ |
| 127 | + "https://foss.heptapod.net/foo/bar", |
| 128 | + "https://foss.heptapod.net:1234/foo/bar", |
| 129 | + "http://foss.heptapod.net/foo/bar", |
| 130 | + "ssh://foss.heptapod.net/foo/bar", |
| 131 | + "hg://foss.heptapod.net/foo/bar", |
| 132 | + "rsync://foss.heptapod.net/foo/bar", |
| 133 | + |
| 134 | + |
| 135 | + "foss.heptapod.net:foo/bar", |
| 136 | + ] |
| 137 | + |
| 138 | + same_origins.each do |origin| |
| 139 | + library.source = origin |
| 140 | + library.origin_changed?.should be_false |
| 141 | + end |
| 142 | + |
| 143 | + # These alternatives should all trigger origin as changed |
| 144 | + changed_origins = [ |
| 145 | + "https://foss.heptapod.net/foo/bar2", |
| 146 | + "https://foss.heptapod.net/foos/bar", |
| 147 | + "https://hghubz.com/foo/bar", |
| 148 | + "file:///foss.heptapod.net/foo/bar", |
| 149 | + |
| 150 | + |
| 151 | + "", |
| 152 | + ] |
| 153 | + |
| 154 | + changed_origins.each do |origin| |
| 155 | + library.source = origin |
| 156 | + library.origin_changed?.should be_true |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + it "renders report version" do |
| 161 | + resolver("library").report_version(version "1.2.3").should eq("1.2.3") |
| 162 | + resolver("library").report_version(version "1.2.3+hg.commit.654875c9dbfa8d72fba70d65fd548d51ffb85aff").should eq("1.2.3 at 654875c") |
| 163 | + end |
| 164 | + |
| 165 | + it "#matches_ref" do |
| 166 | + resolver = HgResolver.new("", "") |
| 167 | + resolver.matches_ref?(HgCommitRef.new("1234567890abcdef"), Shards::Version.new("0.1.0.+hg.commit.1234567")).should be_true |
| 168 | + resolver.matches_ref?(HgCommitRef.new("1234567890abcdef"), Shards::Version.new("0.1.0.+hg.commit.1234567890abcdef")).should be_true |
| 169 | + resolver.matches_ref?(HgCommitRef.new("1234567"), Shards::Version.new("0.1.0.+hg.commit.1234567890abcdef")).should be_true |
| 170 | + end |
| 171 | + end |
| 172 | +end |
0 commit comments