Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic unlock on install and update #337

Merged
merged 4 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions spec/integration/install_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -188,26 +188,19 @@ describe "install" do
end
end

it "fails to install when dependency requirement changed" do
it "updates when dependency requirement changed" do
waj marked this conversation as resolved.
Show resolved Hide resolved
metadata = {dependencies: {web: "2.0.0"}}
lock = {web: "1.0.0"}

with_shard(metadata, lock) do
ex = expect_raises(FailedCommand) { run "shards install --no-color" }
ex.stdout.should contain("Outdated shard.lock")
ex.stderr.should be_empty
refute_installed "web"
run "shards install"

assert_installed "web", "2.0.0"
assert_locked "web", "2.0.0"
end
end

it "install subdependency of new dependency respecting lock" do
create_git_repository "c"
create_git_release "c", "0.1.0", "name: c\nversion: 0.1.0\ndependencies:\n d:\n git: #{git_path("d")}\n version: 0.1.0\n"
create_git_release "c", "0.2.0", "name: c\nversion: 0.2.0\ndependencies:\n d:\n git: #{git_path("d")}\n version: 0.2.0\n"
create_git_repository "d"
create_git_release "d", "0.1.0", "name: d\nversion: 0.1.0\n"
create_git_release "d", "0.2.0", "name: d\nversion: 0.2.0\n"

metadata = {dependencies: {c: "*", d: "*"}}
lock = {d: "0.1.0"}

Expand Down
7 changes: 7 additions & 0 deletions spec/integration/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ private def setup_repositories
create_git_release "binary", "0.1.0", "name: binary\nversion: 0.1.0\nexecutables:\n - foobar\n - baz\n"
create_file "binary", "bin/foo", "echo 'FOO'", perm: 0o755
create_git_release "binary", "0.2.0", "name: binary\nversion: 0.2.0\nexecutables:\n - foobar\n - baz\n - foo"

create_git_repository "c"
create_git_release "c", "0.1.0", "name: c\nversion: 0.1.0\ndependencies:\n d:\n git: #{git_path("d")}\n version: 0.1.0\n"
create_git_release "c", "0.2.0", "name: c\nversion: 0.2.0\ndependencies:\n d:\n git: #{git_path("d")}\n version: 0.2.0\n"
create_git_repository "d"
create_git_release "d", "0.1.0", "name: d\nversion: 0.1.0\n"
create_git_release "d", "0.2.0", "name: d\nversion: 0.2.0\n"
end

private def assert(value, message, file, line)
Expand Down
12 changes: 12 additions & 0 deletions spec/integration/update_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ describe "update" do
end
end

it "unlocks subdependency" do
metadata = {dependencies: {c: "*"}}
lock = {c: "0.1.0", d: "0.1.0"}

with_shard(metadata, lock) do
run "shards update c"

assert_installed "c", "0.2.0"
assert_installed "d", "0.2.0"
end
end

it "updates specified dependencies" do
metadata = {dependencies: {web: "*", orm: "*", optional: "*"}}
lock = {web: "1.0.0", orm: "0.4.0", optional: "0.2.0"}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/install.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Shards
packages = handle_resolver_errors { solver.solve }
return if packages.empty?

if lockfile?
if lockfile? && Shards.production?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if Shards.production? checks can be removed down below now right?

validate(packages)
end

Expand Down
42 changes: 31 additions & 11 deletions src/molinillo_solver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ module Shards
def prepare(@development = true)
end

private def add_lock(base, lock_index, name)
if lock = lock_index.delete(name)
resolver = Shards.find_resolver(lock)

if version = lock["version"]?
spec = resolver.spec(version)
elsif commit = lock["commit"]?
spec = resolver.spec(commit)
lock["version"] = "#{spec.version}+git.commit.#{commit}"
else
return
end

base.add_vertex(lock.name, lock, true)

spec.dependencies.each do |dep|
add_lock(base, lock_index, dep.name)
end
end
end

def solve : Array(Package)
deps = if @development
@spec.dependencies + @spec.development_dependencies
Expand All @@ -23,18 +44,16 @@ module Shards

base = Molinillo::DependencyGraph(Dependency, Dependency).new
if locks = @locks
locks.each do |lock|
if version = lock["version"]?
dep = deps.find { |d| d.name == lock.name }
next if dep && !Versions.matches?(version, dep.version)
end
lock_index = locks.to_h { |d| {d.name, d} }

deps.each do |dep|
if lock = lock_index[dep.name]?
if version = lock["version"]?
next if !Versions.matches?(version, dep.version)
waj marked this conversation as resolved.
Show resolved Hide resolved
end

if commit = lock["commit"]?
resolver = Shards.find_resolver(lock)
spec = resolver.spec(commit)
lock["version"] = "#{spec.version}+git.commit.#{commit}"
add_lock(base, lock_index, dep.name)
end
base.add_vertex(lock.name, lock, true)
end
end

Expand All @@ -45,6 +64,7 @@ module Shards

packages = [] of Package
result.each do |v|
next unless v.payload
spec = v.payload.as?(Spec) || raise "BUG: returned graph payload was not a Spec"
v.requirements.each do |dependency|
unless dependency.name == spec.name
Expand Down Expand Up @@ -140,7 +160,7 @@ module Shards
# to [HEAD], we must resolve the refs to an actual version:
versions_for_refs("HEAD", dependency, resolver)
else
matching
matching.uniq
end
end

Expand Down