Skip to content

Commit 791b5ca

Browse files
author
Brian J. Cardiff
authored
Update check, list, outdated commands to work against source changes and overrides (#429)
* Fix check command to handle different sources * Fix check command to handle overrides * Add list command spec to handle source change and override No need to check resolver or apply overrides in list command * Update specs description * Fix outdated command to handle different sources * Fix outdated command to detect invalid lock * Fix outdated command to handle overrides
1 parent 46308bf commit 791b5ca

File tree

5 files changed

+170
-8
lines changed

5 files changed

+170
-8
lines changed

spec/integration/check_spec.cr

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,32 @@ describe "check" do
7575
run "shards check"
7676
end
7777
end
78+
79+
it "fails when another source was installed" do
80+
with_shard({dependencies: {awesome: "0.1.0"}}) do
81+
run "shards install"
82+
end
83+
84+
with_shard({dependencies: {awesome: {git: git_url(:forked_awesome)}}}) do
85+
ex = expect_raises(FailedCommand) { run "shards check --no-color" }
86+
ex.stdout.should contain("Dependencies aren't satisfied")
87+
ex.stderr.should be_empty
88+
end
89+
end
90+
91+
it "fails when override changes version to use" do
92+
metadata = {dependencies: {awesome: "0.1.0"}}
93+
94+
with_shard(metadata) do
95+
run "shards install"
96+
end
97+
98+
override = {dependencies: {awesome: "0.2.0"}}
99+
100+
with_shard(metadata, nil, override) do
101+
ex = expect_raises(FailedCommand) { run "shards check --no-color" }
102+
ex.stdout.should contain("Dependencies aren't satisfied")
103+
ex.stderr.should be_empty
104+
end
105+
end
78106
end

spec/integration/list_spec.cr

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,32 @@ describe "list" do
6060
ex.stdout.should contain("Dependencies aren't satisfied. Install them with 'shards install'")
6161
end
6262
end
63+
64+
it "show previous installed dependency when source has changed" do
65+
with_shard({dependencies: {awesome: "0.1.0"}}) do
66+
run "shards install"
67+
end
68+
69+
with_shard({dependencies: {awesome: {version: "0.2.0", git: git_url(:forked_awesome)}}}) do
70+
stdout = run "shards list --tree"
71+
stdout.should contain(" * awesome (0.1.0)")
72+
stdout.should contain(" * d (0.2.0)")
73+
end
74+
end
75+
76+
it "show previous installed dependency when override is added" do
77+
metadata = {dependencies: {awesome: "0.1.0"}}
78+
79+
with_shard(metadata) do
80+
run "shards install"
81+
end
82+
83+
override = {dependencies: {awesome: "0.2.0"}}
84+
85+
with_shard(metadata, nil, override) do
86+
stdout = run "shards list --tree"
87+
stdout.should contain(" * awesome (0.1.0)")
88+
stdout.should contain(" * d (0.2.0)")
89+
end
90+
end
6391
end

spec/integration/outdated_spec.cr

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "./spec_helper"
22

33
describe "outdated" do
4-
it "up_to_date" do
4+
it "up to date" do
55
with_shard({dependencies: {web: "*"}}) do
66
run "shards install"
77

@@ -10,7 +10,7 @@ describe "outdated" do
1010
end
1111
end
1212

13-
it "not_latest_version" do
13+
it "not latest version" do
1414
with_shard({dependencies: {orm: "*"}}, {orm: "0.3.1"}) do
1515
run "shards install"
1616

@@ -20,7 +20,7 @@ describe "outdated" do
2020
end
2121
end
2222

23-
it "available_version_matching_pessimistic_operator" do
23+
it "available version matching pessimistic operator" do
2424
with_shard({dependencies: {orm: "~> 0.3.0"}}, {orm: "0.3.1"}) do
2525
run "shards install"
2626

@@ -30,7 +30,7 @@ describe "outdated" do
3030
end
3131
end
3232

33-
it "reports_new_prerelease" do
33+
it "reports new prerelease" do
3434
with_shard({dependencies: {unstable: "0.3.0.alpha"}}) do
3535
run "shards install"
3636
end
@@ -41,7 +41,7 @@ describe "outdated" do
4141
end
4242
end
4343

44-
it "wont_report_prereleases_by_default" do
44+
it "won't report prereleases by default" do
4545
with_shard({dependencies: {preview: "*"}}, {preview: "0.2.0"}) do
4646
run "shards install"
4747

@@ -51,7 +51,7 @@ describe "outdated" do
5151
end
5252
end
5353

54-
it "reports_prereleases_when_asked" do
54+
it "reports prereleases when asked" do
5555
with_shard({dependencies: {preview: "*"}}, {preview: "0.2.0"}) do
5656
run "shards install"
5757

@@ -60,4 +60,82 @@ describe "outdated" do
6060
stdout.should contain(" * preview (installed: 0.2.0, available: 0.4.0.a)")
6161
end
6262
end
63+
64+
it "fails when source has changed" do
65+
with_shard({dependencies: {awesome: "0.1.0"}}) do
66+
run "shards install"
67+
end
68+
69+
with_shard({dependencies: {awesome: {git: git_url(:forked_awesome)}}}) do
70+
ex = expect_raises(FailedCommand) { run "shards outdated --no-color" }
71+
ex.stdout.should contain("Outdated shard.lock (awesome source changed)")
72+
end
73+
end
74+
75+
it "fails when requirements would require an update" do
76+
with_shard({dependencies: {awesome: "0.1.0"}}) do
77+
run "shards install"
78+
end
79+
80+
with_shard({dependencies: {awesome: "0.2.0"}}) do
81+
ex = expect_raises(FailedCommand) { run "shards outdated --no-color" }
82+
ex.stdout.should contain("Outdated shard.lock (awesome requirements changed)")
83+
end
84+
end
85+
86+
it "fails when requirements would require an update due to override" do
87+
metadata = {dependencies: {awesome: "0.1.0"}}
88+
89+
with_shard(metadata) do
90+
run "shards install"
91+
end
92+
93+
override = {dependencies: {awesome: "0.2.0"}}
94+
95+
with_shard(metadata, nil, override) do
96+
ex = expect_raises(FailedCommand) { run "shards outdated --no-color" }
97+
ex.stdout.should contain("Outdated shard.lock (awesome requirements changed)")
98+
end
99+
end
100+
101+
it "not latest version in override (same source)" do
102+
metadata = {dependencies: {awesome: "0.1.0"}}
103+
lock = {awesome: "0.1.0"}
104+
override = {dependencies: {awesome: "*"}}
105+
106+
with_shard(metadata, lock, override) do
107+
run "shards install"
108+
109+
stdout = run "shards outdated --no-color"
110+
stdout.should contain("W: Outdated dependencies:")
111+
stdout.should contain(" * awesome (installed: 0.1.0, available: 0.3.0)")
112+
end
113+
end
114+
115+
it "not latest version in override (different source)" do
116+
metadata = {dependencies: {awesome: "0.1.0"}}
117+
lock = {awesome: {version: "0.1.0", git: git_url(:forked_awesome)}}
118+
override = {dependencies: {awesome: {git: git_url(:forked_awesome)}}}
119+
120+
with_shard(metadata, lock, override) do
121+
run "shards install"
122+
123+
stdout = run "shards outdated --no-color"
124+
stdout.should contain("W: Outdated dependencies:")
125+
stdout.should contain(" * awesome (installed: 0.1.0, available: 0.2.0)")
126+
end
127+
end
128+
129+
it "up to date in override" do
130+
metadata = {dependencies: {awesome: "0.1.0"}}
131+
lock = {awesome: {version: "0.2.0", git: git_url(:forked_awesome)}}
132+
override = {dependencies: {awesome: {git: git_url(:forked_awesome)}}}
133+
134+
with_shard(metadata, lock, override) do
135+
run "shards install"
136+
137+
stdout = run "shards outdated --no-color"
138+
stdout.should contain("I: Dependencies are up to date!")
139+
end
140+
end
63141
end

src/commands/check.cr

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Shards
1919
end
2020

2121
private def verify(dependencies)
22-
dependencies.each do |dependency|
22+
apply_overrides(dependencies).each do |dependency|
2323
Log.debug { "#{dependency.name}: checking..." }
2424

2525
unless installed?(dependency)
@@ -34,7 +34,10 @@ module Shards
3434
return false
3535
end
3636

37-
if !dependency.matches?(lock.version)
37+
if dependency.resolver != lock.resolver
38+
Log.debug { "#{dependency.name}: source changed" }
39+
return false
40+
elsif !dependency.matches?(lock.version)
3841
Log.debug { "#{dependency.name}: lock conflict" }
3942
return false
4043
else
@@ -43,6 +46,16 @@ module Shards
4346
return true
4447
end
4548
end
49+
50+
# FIXME: duplicates MolinilloSolver#on_override
51+
def on_override(dependency : Dependency) : Dependency?
52+
override.try(&.dependencies.find { |o| o.name == dependency.name })
53+
end
54+
55+
# FIXME: duplicates MolinilloSolver#apply_overrides
56+
def apply_overrides(deps : Array(Dependency))
57+
deps.map { |dep| on_override(dep) || dep }
58+
end
4659
end
4760
end
4861
end

src/commands/outdated.cr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@ module Shards
3434
return
3535
end
3636

37+
if installed_dep.resolver != package.resolver
38+
raise LockConflict.new("#{package.name} source changed")
39+
end
40+
3741
resolver = package.resolver
3842
installed = installed_dep.version
43+
dependency = dependency_by_name package.name
44+
45+
if dependency && !dependency.matches?(installed)
46+
raise LockConflict.new("#{package.name} requirements changed")
47+
end
3948

4049
# already the latest version?
4150
available_versions =
@@ -68,6 +77,12 @@ module Shards
6877
private def has_dependencies?
6978
spec.dependencies.any? || (!Shards.production? && spec.development_dependencies.any?)
7079
end
80+
81+
private def dependency_by_name(name : String)
82+
override.try(&.dependencies.find { |o| o.name == name }) ||
83+
spec.dependencies.find { |o| o.name == name } ||
84+
spec.development_dependencies.find { |o| o.name == name }
85+
end
7186
end
7287
end
7388
end

0 commit comments

Comments
 (0)