Skip to content

Commit 2c98f78

Browse files
authored
Merge pull request #341 from RX14/feature/vheck-versions
Detect version mismatches between shard.yml and git tags
2 parents 4091ac5 + b0958c4 commit 2c98f78

File tree

7 files changed

+60
-27
lines changed

7 files changed

+60
-27
lines changed

spec/integration/install_spec.cr

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,32 @@ describe "install" do
376376
end
377377
end
378378

379+
it "warns when shard.yml version doesn't match git tag" do
380+
metadata = {
381+
dependencies: {
382+
version_mismatch: {git: git_url(:version_mismatch), version: "0.2.0"},
383+
},
384+
}
385+
with_shard(metadata) do
386+
stdout = run "shards install --no-color"
387+
stdout.should contain("W: Shard \"version_mismatch\" version (0.1.0) doesn't match tag version (0.2.0)")
388+
assert_installed "version_mismatch"
389+
end
390+
end
391+
392+
it "doesn't warn when version mismatch is fixed" do
393+
metadata = {
394+
dependencies: {
395+
version_mismatch: {git: git_url(:version_mismatch), version: "0.2.1"},
396+
},
397+
}
398+
with_shard(metadata) do
399+
stdout = run "shards install --no-color"
400+
stdout.should_not contain("doesn't match tag version")
401+
assert_installed "version_mismatch", "0.2.1"
402+
end
403+
end
404+
379405
it "test install old with version when shard was renamed" do
380406
metadata = {
381407
dependencies: {

spec/integration/spec_helper.cr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ private def setup_repositories
6666
create_git_release "renamed", "0.2.0", "name: new_name\nversion: 0.2.0"
6767
create_git_version_commit "renamed", "0.3.0", "name: another_name\nversion: 0.3.0"
6868

69+
create_git_repository "version_mismatch"
70+
create_git_release "version_mismatch", "0.1.0", "name: version_mismatch\nversion: 0.1.0"
71+
create_git_release "version_mismatch", "0.2.0", "name: version_mismatch\nversion: 0.1.0"
72+
create_git_release "version_mismatch", "0.2.1", "name: version_mismatch\nversion: 0.2.1"
73+
6974
create_git_repository "inprogress"
7075
create_git_version_commit "inprogress", "0.1.0"
7176
create_git_version_commit "inprogress", "0.1.0"

src/molinillo_solver.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ module Shards
7070
unless dependency.name == spec.name
7171
raise Error.new("Error shard name (#{spec.name}) doesn't match dependency name (#{dependency.name})")
7272
end
73+
if spec.mismatched_version?
74+
Log.warn { "Shard \"#{spec.name}\" version (#{spec.original_version}) doesn't match tag version (#{spec.version})" }
75+
end
7376
end
7477
resolver = spec.resolver || raise "BUG: returned Spec has no resolver"
7578
version = spec.version

src/resolvers/git.cr

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,24 @@ module Shards
4141
end
4242
end
4343

44-
def specs(versions)
45-
specs = {} of String => Spec
46-
47-
versions.each do |version|
48-
refs = git_refs(version)
49-
yaml = capture("git show #{refs}:#{SPEC_FILENAME}")
50-
specs[version] = Spec.from_yaml(yaml).tap { |spec| spec.resolver = self }
51-
rescue Error
44+
def spec(version = nil)
45+
spec = Spec.from_yaml(read_spec(version))
46+
spec.resolver = self
47+
48+
if version && version =~ VERSION_REFERENCE
49+
# In this case, we know that the version was looked up by git tag, so we
50+
# validate the spec version against the tag version.
51+
version = version.lstrip('v')
52+
if spec.version != version
53+
spec.mismatched_version = true
54+
end
5255
end
5356

54-
specs
57+
spec
5558
end
5659

57-
def spec?(version)
58-
refs = git_refs(version)
59-
yaml = capture("git show #{refs}:#{SPEC_FILENAME}")
60-
Spec.from_yaml(yaml)
60+
private def spec?(version)
61+
spec(version)
6162
rescue Error
6263
end
6364

src/resolvers/path.cr

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ module Shards
2020
@dependency.path
2121
end
2222

23-
def spec?(version)
24-
spec_path = File.join(local_path, SPEC_FILENAME)
25-
26-
if File.exists?(spec_path)
27-
Spec.from_yaml(File.read(spec_path))
28-
# TODO: fail if the spec isn't the expected version!
29-
end
23+
def spec(version = nil)
24+
spec = Spec.from_yaml(read_spec(version))
25+
spec.resolver = self
26+
spec
3027
end
3128

3229
def installed_spec

src/resolvers/resolver.cr

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ module Shards
1111
def initialize(@dependency)
1212
end
1313

14-
def spec(version = nil)
15-
Spec.from_yaml(read_spec(version)).tap { |spec| spec.resolver = self }
16-
end
17-
1814
def specs(versions)
1915
specs = {} of String => Spec
20-
versions.each { |version| specs[version] = spec(version) }
16+
versions.each do |version|
17+
specs[version] = spec(version)
18+
rescue Error
19+
end
2120
specs
2221
end
2322

@@ -35,7 +34,7 @@ module Shards
3534
end
3635

3736
abstract def read_spec(version = nil)
38-
abstract def spec?(version)
37+
abstract def spec(version)
3938
abstract def available_versions
4039
abstract def install(version = nil)
4140
abstract def installed_commit_hash

src/spec.cr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ module Shards
7070

7171
getter! name : String?
7272
getter! version : String?
73+
getter! original_version : String?
7374
getter description : String?
7475
getter license : String?
7576
getter crystal : String?
7677
property resolver : Resolver?
78+
property? mismatched_version = false
7779

7880
# :nodoc:
7981
def initialize(pull : YAML::PullParser, validate = false)
@@ -84,7 +86,7 @@ module Shards
8486
when "name"
8587
@name = pull.read_scalar
8688
when "version"
87-
@version = pull.read_scalar
89+
@original_version = @version = pull.read_scalar
8890
when "description"
8991
@description = pull.read_scalar
9092
when "license"

0 commit comments

Comments
 (0)