Skip to content

Commit 456e46a

Browse files
Fix error message for invalid shard.yml (#516)
1 parent 97ce009 commit 456e46a

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

spec/integration/install_spec.cr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,14 @@ describe "install" do
954954
end
955955
end
956956

957+
it "shows error when installing by ref and spec is invalid" do
958+
metadata = {dependencies: {invalidspec: {git: git_url(:invalidspec), tag: "v0.1.0"}}}
959+
with_shard(metadata) do
960+
ex = expect_raises(FailedCommand) { run "shards install --no-color" }
961+
ex.stdout.should contain(%(E: Invalid shard.yml for shard "invalidspec" at commit #{git_commits(:invalidspec)[0]}: Expected SCALAR but was SEQUENCE_START at line 5, column 1))
962+
end
963+
end
964+
957965
it "install latest version despite current crystal being older version, but warn" do
958966
metadata = {dependencies: {incompatible: "*"}}
959967
with_shard(metadata) do

spec/integration/spec_helper.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ private def setup_repositories
6262
create_git_release "noshardyml", "0.1.0", false
6363
create_git_release "noshardyml", "0.2.0"
6464

65+
create_git_repository "invalidspec"
66+
create_git_release "invalidspec", "0.1.0", {crystal: [""]}
67+
6568
# dependencies with postinstall scripts:
6669
create_git_repository "post"
6770
{% if flag?(:win32) %}

src/resolvers/git.cr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,18 @@ module Shards
152152
end
153153
end
154154

155-
private def spec_at_ref(ref : GitRef) : Spec?
155+
private def spec_at_ref(ref : GitRef, commit) : Spec
156156
update_local_cache
157+
158+
unless file_exists?(ref, SPEC_FILENAME)
159+
raise Error.new "No #{SPEC_FILENAME} was found for shard #{name.inspect} at commit #{commit}"
160+
end
161+
162+
spec_yaml = capture("git show #{Process.quote("#{ref.to_git_ref}:#{SPEC_FILENAME}")}")
157163
begin
158-
if file_exists?(ref, SPEC_FILENAME)
159-
spec_yaml = capture("git show #{Process.quote("#{ref.to_git_ref}:#{SPEC_FILENAME}")}")
160-
Spec.from_yaml(spec_yaml)
161-
end
162-
rescue Error
163-
nil
164+
Spec.from_yaml(spec_yaml)
165+
rescue error : Error
166+
raise Error.new "Invalid #{SPEC_FILENAME} for shard #{name.inspect} at commit #{commit}: #{error.message}"
164167
end
165168
end
166169

@@ -183,11 +186,8 @@ module Shards
183186
raise Error.new "Could not find #{ref.full_info} for shard #{name.inspect} in the repository #{source}"
184187
end
185188

186-
if spec = spec_at_ref(ref)
187-
Version.new "#{spec.version.value}+git.commit.#{commit}"
188-
else
189-
raise Error.new "No #{SPEC_FILENAME} was found for shard #{name.inspect} at commit #{commit}"
190-
end
189+
spec = spec_at_ref(ref, commit)
190+
Version.new "#{spec.version.value}+git.commit.#{commit}"
191191
end
192192

193193
def matches_ref?(ref : GitRef, version : Version)

0 commit comments

Comments
 (0)