Skip to content

Commit

Permalink
Move install responsibilities from Resolver to Package (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
waj authored Aug 4, 2020
1 parent e43b954 commit b96082c
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 176 deletions.
11 changes: 11 additions & 0 deletions spec/integration/list_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,15 @@ describe "list" do
stdout.should contain(" * shoulda (0.1.0)")
end
end

it "show error when dependencies are not installed" do
metadata = {
dependencies: {web: "*", orm: "*"},
development_dependencies: {mock: "*"},
}
with_shard(metadata) do
ex = expect_raises(FailedCommand) { run "shards list --no-color" }
ex.stdout.should contain("Dependencies aren't satisfied. Install them with 'shards install'")
end
end
end
14 changes: 7 additions & 7 deletions spec/unit/git_resolver_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ module Shards
it "install" do
library = resolver("library")

library.install(version "0.1.2")
library.install_sources(version("0.1.2"), install_path("library"))
File.exists?(install_path("library", "src/library.cr")).should be_true
File.exists?(install_path("library", "shard.yml")).should be_true
library.installed_spec.not_nil!.version.should eq(version "0.1.2")
Spec.from_file(install_path("library", "shard.yml")).version.should eq(version "0.1.2")

library.install(version "0.2.0")
library.installed_spec.not_nil!.version.should eq(version "0.2.0")
library.install_sources(version("0.2.0"), install_path("library"))
Spec.from_file(install_path("library", "shard.yml")).version.should eq(version "0.2.0")
end

it "install commit" do
library = resolver("library")
version = version "0.2.0+git.commit.#{git_commits(:library)[0]}"
library.install(version)
library.installed_spec.not_nil!.version.should eq(version)
library.install_sources(version, install_path("library"))
Spec.from_file(install_path("library", "shard.yml")).version.should eq(version "0.2.0")
end

it "origin changed" do
library = GitResolver.new("library", git_url("library"))
library.install(version "0.1.2")
library.install_sources(version("0.1.2"), install_path("library"))

# Change the origin in the cache repo to https://github.com/foo/bar
Dir.cd(library.local_path) do
Expand Down
85 changes: 85 additions & 0 deletions spec/unit/package_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require "./spec_helper"
require "../../src/package"

private def resolver(name)
Shards::PathResolver.new(name, git_path(name))
end

private def git_resolver(name)
Shards::GitResolver.new(name, git_path(name))
end

module Shards
describe Package do
before_each do
create_path_repository "library", "1.2.3"
create_git_repository "repo", "0.1.2", "0.1.3"
end

it "installs" do
package = Package.new("library", resolver("library"), version "1.2.3")
package.installed?.should be_false
package.install
package.installed?.should be_true
end

it "reads spec from installed dir" do
package = Package.new("repo", git_resolver("repo"), version "0.1.2")
package.install

File.open(install_path("repo", "shard.yml"), "a") do |f|
f.puts "license: FOO"
end

package.spec.license.should eq("FOO")
end

it "fallback to resolver to read spec" do
package = Package.new("repo", git_resolver("repo"), version "0.1.2")
package.install
File.delete install_path("repo", "shard.yml")
package.spec.version.should eq(version "0.1.2")
end

it "reads spec from resolver if not installed" do
package = Package.new("repo", git_resolver("repo"), version "0.1.3")
package.install

package = Package.new("repo", git_resolver("repo"), version "0.1.2")
package.spec.original_version.should eq(version "0.1.2")
end

it "different version is not installed" do
package = Package.new("library", resolver("library"), version "1.2.3")
package.install

package2 = Package.new("library", resolver("library"), version "2.0.0")
package2.installed?.should be_false
end

it "different resolver is not installed" do
package = Package.new("library", resolver("library"), version "1.2.3")
package.install

package2 = Package.new("library", resolver("foo"), version "1.2.3")
package2.installed?.should be_false
end

it "not installed if missing target" do
package = Package.new("library", resolver("library"), version "1.2.3")
package.install

run "rm -rf #{install_path("library")}"
package.installed?.should be_false
end

it "cleanups target before installing" do
Dir.mkdir_p(install_path)
File.touch(install_path("library"))
package = Package.new("library", resolver("library"), version "1.2.3")
package.install

File.symlink?(install_path("library")).should be_true
end
end
end
53 changes: 4 additions & 49 deletions spec/unit/path_resolver_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,61 +20,16 @@ module Shards

it "install" do
resolver("library").tap do |library|
library.install(version "1.2.3")
library.install_sources(version("1.2.3"), install_path("library"))
File.exists?(install_path("library", "src/library.cr")).should be_true
File.exists?(install_path("library", "shard.yml")).should be_true
library.installed_spec.not_nil!.version.should eq(version "1.2.3")
Spec.from_file(install_path("library", "shard.yml")).version.should eq(version "1.2.3")
end
end

it "install fails when path doesnt exist" do
expect_raises(Error) { resolver("unknown").install(version "1.0.0") }
end

it "installed reports library is installed" do
resolver("library").tap do |resolver|
resolver.installed?.should be_false

resolver.install(version "1.2.3")
resolver.installed?.should be_true
end
end

it "installed when target is incorrect link" do
resolver("library").tap do |resolver|
resolver.install(version "1.2.3")
resolver.installed?.should be_true
end
end

it "installed when target is incorrect broken link" do
resolver("library").tap do |resolver|
File.symlink("/does-not-exist", resolver.install_path)
resolver.installed?.should be_false

resolver.install(version "1.2.3")
resolver.installed?.should be_true
end
end

it "installed when target is dir" do
resolver("library").tap do |resolver|
Dir.mkdir_p(resolver.install_path)
File.touch(File.join(resolver.install_path, "foo"))
resolver.installed?.should be_false

resolver.install(version "1.2.3")
resolver.installed?.should be_true
end
end

it "installed when target is file" do
resolver("library").tap do |resolver|
File.touch(resolver.install_path)
resolver.installed?.should be_false

resolver.install(version "1.2.3")
resolver.installed?.should be_true
expect_raises(Error) do
resolver("unknown").install_sources(version("1.0.0"), install_path("unknown"))
end
end

Expand Down
15 changes: 0 additions & 15 deletions spec/unit/resolver_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@ module Shards
resolver.should_not eq(GitResolver.new("name", "/path"))
end

describe "#installed_spec" do
it "reports parse error location" do
create_path_repository "foo", "1.2.3"
create_file "foo", "shard.yml", "name: foo\nname: foo\n"

resolver = Shards::PathResolver.new("foo", git_path("foo"))
resolver.install Shards::Version.new("1.2.3")

error = expect_raises(ParseError, %(Error in foo:spec/unit/.lib/foo/shard.yml: duplicate attribute "name" at line 2, column 1)) do
resolver.installed_spec
end
error.resolver.should eq resolver
end
end

describe "#spec" do
it "reports parse error location" do
create_path_repository "foo", "1.2.3"
Expand Down
5 changes: 3 additions & 2 deletions spec/unit/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ end
Spec.before_each do
clear_repositories
Shards::Resolver.clear_resolver_cache
Shards.info.reload
end

private def clear_repositories
run "rm -rf #{tmp_path}/*"
run "rm -rf #{Shards.cache_path}/*"
run "rm -rf #{Shards.install_path}/*"
run "rm -rf #{Shards.cache_path}"
run "rm -rf #{Shards.install_path}"
end

def install_path(project, *path_names)
Expand Down
20 changes: 7 additions & 13 deletions src/commands/check.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,14 @@ module Shards
dependencies.each do |dependency|
Log.debug { "#{dependency.name}: checking..." }

unless _spec = dependency.resolver.installed_spec
Log.debug { "#{dependency.name}: not installed" }
unless installed?(dependency)
raise Error.new("Dependencies aren't satisfied. Install them with 'shards install'")
end

unless installed?(dependency, _spec)
raise Error.new("Dependencies aren't satisfied. Install them with 'shards install'")
end

verify(_spec.dependencies)
end
end

private def installed?(dependency, spec)
unless lock = locks.shards.find { |d| d.name == spec.name }
private def installed?(dependency)
unless lock = locks.shards.find { |d| d.name == dependency.name }
Log.debug { "#{dependency.name}: not locked" }
return false
end
Expand All @@ -46,13 +39,14 @@ module Shards
Log.debug { "#{dependency.name}: lock conflict" }
return false
else
return spec.version == version
package = Package.new(lock.name, lock.resolver, version)
return false unless package.installed?
verify(package.spec.dependencies)
return true
end
else
raise Error.new("Invalid #{LOCK_FILENAME}. Please run `shards install` to fix it.")
end

true
end
end
end
Expand Down
14 changes: 8 additions & 6 deletions src/commands/list.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ module Shards

private def list(dependencies, level = 1)
dependencies.each do |dependency|
resolver = dependency.resolver

# FIXME: duplicated from Check#verify
unless _spec = resolver.installed_spec
installed = Shards.info.installed[dependency.name]?
unless installed
Log.debug { "#{dependency.name}: not installed" }
raise Error.new("Dependencies aren't satisfied. Install them with 'shards install'")
end

version = installed.requirement.as(Shards::Version)
package = Package.new(installed.name, installed.resolver, version)
resolver = installed.resolver

indent = " " * level
puts "#{indent}* #{_spec.name} (#{resolver.report_version _spec.version})"
puts "#{indent}* #{dependency.name} (#{resolver.report_version version})"

indent_level = @tree ? level + 1 : level
list(_spec.dependencies, indent_level)
list(package.spec.dependencies, indent_level)
end
end

Expand Down
8 changes: 4 additions & 4 deletions src/commands/outdated.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ module Shards
end

private def analyze(package)
resolver = package.resolver
installed = resolver.installed_spec.try(&.version)

unless installed
unless installed_dep = Shards.info.installed[package.name]?
Log.warn { "#{package.name}: not installed" }
return
end

resolver = package.resolver
installed = installed_dep.requirement.as(Shards::Version)

# already the latest version?
available_versions =
if @prereleases
Expand Down
2 changes: 2 additions & 0 deletions src/info.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Shards::Info
path = info_path
if File.exists?(path)
@installed = Lock.from_file(path).shards.index_by &.name
else
@installed.clear
end
end

Expand Down
Loading

0 comments on commit b96082c

Please sign in to comment.