Skip to content

Commit

Permalink
Merge pull request crystal-lang#341 from RX14/feature/vheck-versions
Browse files Browse the repository at this point in the history
 Detect version mismatches between shard.yml and git tags
  • Loading branch information
waj committed Apr 3, 2020
2 parents 1492792 + 2950168 commit 322bd3d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 27 deletions.
26 changes: 26 additions & 0 deletions spec/integration/install_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,32 @@ describe "install" do
end
end

it "warns when shard.yml version doesn't match git tag" do
metadata = {
dependencies: {
version_mismatch: {git: git_url(:version_mismatch), version: "0.2.0"},
},
}
with_shard(metadata) do
stdout = run "shards install --no-color"
stdout.should contain("W: Shard \"version_mismatch\" version (0.1.0) doesn't match tag version (0.2.0)")
assert_installed "version_mismatch"
end
end

it "doesn't warn when version mismatch is fixed" do
metadata = {
dependencies: {
version_mismatch: {git: git_url(:version_mismatch), version: "0.2.1"},
},
}
with_shard(metadata) do
stdout = run "shards install --no-color"
stdout.should_not contain("doesn't match tag version")
assert_installed "version_mismatch", "0.2.1"
end
end

it "test install old with version when shard was renamed" do
metadata = {
dependencies: {
Expand Down
5 changes: 5 additions & 0 deletions spec/integration/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ private def setup_repositories
create_git_release "renamed", "0.2.0", "name: new_name\nversion: 0.2.0"
create_git_version_commit "renamed", "0.3.0", "name: another_name\nversion: 0.3.0"

create_git_repository "version_mismatch"
create_git_release "version_mismatch", "0.1.0", "name: version_mismatch\nversion: 0.1.0"
create_git_release "version_mismatch", "0.2.0", "name: version_mismatch\nversion: 0.1.0"
create_git_release "version_mismatch", "0.2.1", "name: version_mismatch\nversion: 0.2.1"

create_git_repository "inprogress"
create_git_version_commit "inprogress", "0.1.0"
create_git_version_commit "inprogress", "0.1.0"
Expand Down
3 changes: 3 additions & 0 deletions src/molinillo_solver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ module Shards
unless dependency.name == spec.name
raise Error.new("Error shard name (#{spec.name}) doesn't match dependency name (#{dependency.name})")
end
if spec.mismatched_version?
Log.warn { "Shard \"#{spec.name}\" version (#{spec.original_version}) doesn't match tag version (#{spec.version})" }
end
end
resolver = spec.resolver || raise "BUG: returned Spec has no resolver"
version = spec.version
Expand Down
27 changes: 14 additions & 13 deletions src/resolvers/git.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,24 @@ module Shards
end
end

def specs(versions)
specs = {} of String => Spec

versions.each do |version|
refs = git_refs(version)
yaml = capture("git show #{refs}:#{SPEC_FILENAME}")
specs[version] = Spec.from_yaml(yaml).tap { |spec| spec.resolver = self }
rescue Error
def spec(version = nil)
spec = Spec.from_yaml(read_spec(version))
spec.resolver = self

if version && version =~ VERSION_REFERENCE
# In this case, we know that the version was looked up by git tag, so we
# validate the spec version against the tag version.
version = version.lstrip('v')
if spec.version != version
spec.mismatched_version = true
end
end

specs
spec
end

def spec?(version)
refs = git_refs(version)
yaml = capture("git show #{refs}:#{SPEC_FILENAME}")
Spec.from_yaml(yaml)
private def spec?(version)
spec(version)
rescue Error
end

Expand Down
11 changes: 4 additions & 7 deletions src/resolvers/path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ module Shards
@dependency.path
end

def spec?(version)
spec_path = File.join(local_path, SPEC_FILENAME)

if File.exists?(spec_path)
Spec.from_yaml(File.read(spec_path))
# TODO: fail if the spec isn't the expected version!
end
def spec(version = nil)
spec = Spec.from_yaml(read_spec(version))
spec.resolver = self
spec
end

def installed_spec
Expand Down
11 changes: 5 additions & 6 deletions src/resolvers/resolver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ module Shards
def initialize(@dependency)
end

def spec(version = nil)
Spec.from_yaml(read_spec(version)).tap { |spec| spec.resolver = self }
end

def specs(versions)
specs = {} of String => Spec
versions.each { |version| specs[version] = spec(version) }
versions.each do |version|
specs[version] = spec(version)
rescue Error
end
specs
end

Expand All @@ -35,7 +34,7 @@ module Shards
end

abstract def read_spec(version = nil)
abstract def spec?(version)
abstract def spec(version)
abstract def available_versions
abstract def install(version = nil)
abstract def installed_commit_hash
Expand Down
4 changes: 3 additions & 1 deletion src/spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ module Shards

getter! name : String?
getter! version : String?
getter! original_version : String?
getter description : String?
getter license : String?
getter crystal : String?
property resolver : Resolver?
property? mismatched_version = false

# :nodoc:
def initialize(pull : YAML::PullParser, validate = false)
Expand All @@ -84,7 +86,7 @@ module Shards
when "name"
@name = pull.read_scalar
when "version"
@version = pull.read_scalar
@original_version = @version = pull.read_scalar
when "description"
@description = pull.read_scalar
when "license"
Expand Down

0 comments on commit 322bd3d

Please sign in to comment.