cask/test: DRY up tests and remove redundant helper

This commit is contained in:
Josh Hagins 2016-10-19 16:42:31 -04:00
parent f79cb2ba30
commit cba0d069cc
13 changed files with 310 additions and 381 deletions

View File

@ -2,13 +2,15 @@ require 'test_helper'
describe "Casks" do describe "Casks" do
with_environment "HOMEBREW_DEVELOPER" => nil do with_environment "HOMEBREW_DEVELOPER" => nil do
Hbc.all.reject {|c| c.is_a?(Hbc::TestCask) }.each do |cask| shutup do
describe "#{cask}" do Hbc.all.reject { |c| c.is_a?(Hbc::TestCask) }.each do |cask|
it "passes audit" do describe "#{cask}" do
audit = Hbc::Audit.new(cask) it "passes audit" do
audit.run! audit = Hbc::Audit.new(cask)
audit.errors.must_equal [], "[#{cask}] Cask audit must be error free" audit.run!
audit.warnings.must_equal [], "[#{cask}] Cask audit must be warning free" audit.errors.must_equal [], "[#{cask}] Cask audit must be error free"
audit.warnings.must_equal [], "[#{cask}] Cask audit must be warning free"
end
end end
end end
end end

View File

@ -1,85 +1,81 @@
require "test_helper" require "test_helper"
describe Hbc::Artifact::App do describe Hbc::Artifact::App do
let(:local_alt_caffeine) { describe "activate to alternate target" do
Hbc.load("with-alt-target").tap do |cask| let(:cask) { Hbc.load("with-alt-target") }
let(:install_phase) {
lambda { Hbc::Artifact::App.new(cask).install_phase }
}
let(:source_path) { cask.staged_path.join("Caffeine.app") }
let(:target_path) { Hbc.appdir.join("AnotherName.app") }
before do
TestHelper.install_without_artifacts(cask) TestHelper.install_without_artifacts(cask)
end end
}
describe "activate to alternate target" do
it "installs the given apps using the proper target directory" do it "installs the given apps using the proper target directory" do
cask = local_alt_caffeine source_path.must_be :directory?
target_path.wont_be :exist?
shutup do shutup do
Hbc::Artifact::App.new(cask).install_phase install_phase.call
end end
File.ftype(Hbc.appdir.join("AnotherName.app")).must_equal "directory" target_path.must_be :directory?
File.exist?(cask.staged_path.join("AnotherName.app")).must_equal false source_path.wont_be :exist?
end end
it "works with an application in a subdir" do describe "when app is in a subdirectory" do
subdir_cask = Hbc::Cask.new("subdir") do let(:cask) {
url TestHelper.local_binary_url("caffeine.zip") Hbc::Cask.new("subdir") do
homepage "http://example.com/local-caffeine" url TestHelper.local_binary_url("caffeine.zip")
version "1.2.3" homepage "http://example.com/local-caffeine"
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94" version "1.2.3"
app "subdir/Caffeine.app", target: "AnotherName.app" sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
end app "subdir/Caffeine.app", target: "AnotherName.app"
end
}
begin it "installs the given apps using the proper target directory" do
TestHelper.install_without_artifacts(subdir_cask) appsubdir = cask.staged_path.join("subdir").tap(&:mkpath)
FileUtils.mv(source_path, appsubdir)
appsubdir = subdir_cask.staged_path.join("subdir").tap(&:mkpath)
FileUtils.mv(subdir_cask.staged_path.join("Caffeine.app"), appsubdir)
shutup do shutup do
Hbc::Artifact::App.new(subdir_cask).install_phase install_phase.call
end end
File.ftype(Hbc.appdir.join("AnotherName.app")).must_equal "directory" target_path.must_be :directory?
File.exist?(appsubdir.join("AnotherName.app")).must_equal false appsubdir.join("Caffeine.app").wont_be :exist?
ensure
if defined?(subdir_cask)
shutup do
Hbc::Installer.new(subdir_cask).uninstall
end
end
end end
end end
it "only uses apps when they are specified" do it "only uses apps when they are specified" do
cask = local_alt_caffeine staged_app_copy = source_path.sub("Caffeine.app", "Caffeine Deluxe.app")
FileUtils.cp_r source_path, staged_app_copy
staged_app_path = cask.staged_path.join("Caffeine.app")
staged_app_copy = staged_app_path.sub("Caffeine.app", "Caffeine Deluxe.app")
FileUtils.cp_r staged_app_path, staged_app_copy
shutup do shutup do
Hbc::Artifact::App.new(cask).install_phase install_phase.call
end end
File.ftype(Hbc.appdir.join("AnotherName.app")).must_equal "directory" target_path.must_be :directory?
File.exist?(staged_app_path).must_equal false source_path.wont_be :exist?
File.exist?(Hbc.appdir.join("AnotherNameAgain.app")).must_equal false Hbc.appdir.join("Caffeine Deluxe.app").wont_be :exist?
File.exist?(cask.staged_path.join("Caffeine Deluxe.app")).must_equal true cask.staged_path.join("Caffeine Deluxe.app").must_be :directory?
end end
it "avoids clobbering an existing app by moving over it" do it "avoids clobbering an existing app by moving over it" do
cask = local_alt_caffeine target_path.mkpath
existing_app_path = Hbc.appdir.join("AnotherName.app") install_phase.must_output <<-EOS.undent
existing_app_path.mkpath ==> It seems there is already an App at '#{target_path}'; not moving.
EOS
TestHelper.must_output(self, lambda { source_path.must_be :directory?
Hbc::Artifact::App.new(cask).install_phase target_path.must_be :directory?
}, "==> It seems there is already an App at '#{existing_app_path}'; not moving.") File.identical?(source_path, target_path).must_equal false
source_path = cask.staged_path.join("Caffeine.app")
File.identical?(source_path, existing_app_path).must_equal false
end end
end end
end end

View File

@ -1,113 +1,95 @@
require "test_helper" require "test_helper"
describe Hbc::Artifact::App do describe Hbc::Artifact::App do
let(:local_caffeine) { let(:cask) { Hbc.load("local-caffeine") }
Hbc.load("local-caffeine").tap do |cask| let(:command) { Hbc::SystemCommand }
TestHelper.install_without_artifacts(cask) let(:force) { false }
end let(:app) { Hbc::Artifact::App.new(cask, command: command, force: force) }
let(:source_path) { cask.staged_path.join("Caffeine.app") }
let(:target_path) { Hbc.appdir.join("Caffeine.app") }
let(:install_phase) {
lambda { app.install_phase }
} }
describe "install_phase" do let(:uninstall_phase) {
it "installs the given apps using the proper target directory" do lambda { app.uninstall_phase }
cask = local_caffeine }
before do
TestHelper.install_without_artifacts(cask)
end
describe "install_phase" do
it "installs the given app using the proper target directory" do
shutup do shutup do
Hbc::Artifact::App.new(cask).install_phase install_phase.call
end end
File.ftype(Hbc.appdir.join("Caffeine.app")).must_equal "directory" target_path.must_be :directory?
File.exist?(cask.staged_path.join("Caffeine.app")).must_equal false source_path.wont_be :exist?
end end
it "works with an application in a subdir" do describe "when app is in a subdirectory" do
subdir_cask = Hbc::Cask.new("subdir") do let(:cask) {
url TestHelper.local_binary_url("caffeine.zip") Hbc::Cask.new("subdir") do
homepage "http://example.com/local-caffeine" url TestHelper.local_binary_url("caffeine.zip")
version "1.2.3" homepage "http://example.com/local-caffeine"
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94" version "1.2.3"
app "subdir/Caffeine.app" sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
end app "subdir/Caffeine.app"
end
}
begin it "installs the given app using the proper target directory" do
TestHelper.install_without_artifacts(subdir_cask) appsubdir = cask.staged_path.join("subdir").tap(&:mkpath)
FileUtils.mv(source_path, appsubdir)
appsubdir = subdir_cask.staged_path.join("subdir").tap(&:mkpath)
FileUtils.mv(subdir_cask.staged_path.join("Caffeine.app"), appsubdir)
shutup do shutup do
Hbc::Artifact::App.new(subdir_cask).install_phase install_phase.call
end end
File.ftype(Hbc.appdir.join("Caffeine.app")).must_equal "directory" target_path.must_be :directory?
File.exist?(appsubdir.join("Caffeine.app")).must_equal false appsubdir.join("Caffeine.app").wont_be :exist?
ensure
if defined?(subdir_cask)
shutup do
Hbc::Installer.new(subdir_cask).uninstall
end
end
end end
end end
it "only uses apps when they are specified" do it "only uses apps when they are specified" do
cask = local_caffeine staged_app_copy = source_path.sub("Caffeine.app", "Caffeine Deluxe.app")
FileUtils.cp_r source_path, staged_app_copy
staged_app_path = cask.staged_path.join("Caffeine.app")
staged_app_copy = staged_app_path.sub("Caffeine.app", "Caffeine Deluxe.app")
FileUtils.cp_r staged_app_path, staged_app_copy
shutup do shutup do
Hbc::Artifact::App.new(cask).install_phase install_phase.call
end end
File.ftype(Hbc.appdir.join("Caffeine.app")).must_equal "directory" target_path.must_be :directory?
File.exist?(staged_app_path).must_equal false source_path.wont_be :exist?
File.exist?(Hbc.appdir.join("Caffeine Deluxe.app")).must_equal false Hbc.appdir.join("Caffeine Deluxe.app").wont_be :exist?
File.exist?(cask.staged_path.join("Caffeine Deluxe.app")).must_equal true cask.staged_path.join("Caffeine Deluxe.app").must_be :exist?
end end
describe "when the target already exists" do describe "when the target already exists" do
let(:target_path) { before do
target_path = Hbc.appdir.join("Caffeine.app")
target_path.mkpath target_path.mkpath
target_path end
}
it "avoids clobbering an existing app" do it "avoids clobbering an existing app" do
cask = local_caffeine install_phase.must_output <<-EOS.undent
==> It seems there is already an App at '#{target_path}'; not moving.
TestHelper.must_output(self, lambda { EOS
Hbc::Artifact::App.new(cask).install_phase
}, "==> It seems there is already an App at '#{target_path}'; not moving.")
source_path = cask.staged_path.join("Caffeine.app")
source_path.must_be :directory?
target_path.must_be :directory?
File.identical?(source_path, target_path).must_equal false File.identical?(source_path, target_path).must_equal false
contents_path = target_path.join("Contents/Info.plist") contents_path = target_path.join("Contents/Info.plist")
File.exist?(contents_path).must_equal false contents_path.wont_be :exist?
end end
describe "given the force option" do describe "given the force option" do
let(:install_phase) { let(:force) { true }
lambda do |given_options = {}|
options = { force: true }.merge(given_options)
Hbc::Artifact::App.new(local_caffeine, options).install_phase
end
}
let(:chmod_cmd) {
["/bin/chmod", "-R", "--", "u+rwx", target_path]
}
let(:chmod_n_cmd) {
["/bin/chmod", "-R", "-N", target_path]
}
let(:chflags_cmd) {
["/usr/bin/chflags", "-R", "--", "000", target_path]
}
before do before do
Hbc::Utils.stubs(current_user: "fake_user") Hbc::Utils.stubs(current_user: "fake_user")
@ -115,60 +97,56 @@ describe Hbc::Artifact::App do
describe "target is both writable and user-owned" do describe "target is both writable and user-owned" do
it "overwrites the existing app" do it "overwrites the existing app" do
cask = local_caffeine install_phase.must_output <<-EOS.undent
==> It seems there is already an App at '#{target_path}'; overwriting.
==> Removing App: '#{target_path}'
==> Moving App 'Caffeine.app' to '#{target_path}'
EOS
expected = [ source_path.wont_be :exist?
"==> It seems there is already an App at '#{target_path}'; overwriting.", target_path.must_be :directory?
"==> Removing App: '#{target_path}'",
"==> Moving App 'Caffeine.app' to '#{target_path}'",
]
TestHelper.must_output(self, install_phase,
expected.join("\n"))
source_path = cask.staged_path.join("Caffeine.app")
File.exist?(source_path).must_equal false
File.ftype(target_path).must_equal "directory"
contents_path = target_path.join("Contents/Info.plist") contents_path = target_path.join("Contents/Info.plist")
File.exist?(contents_path).must_equal true contents_path.must_be :exist?
end end
end end
describe "target is user-owned but contains read-only files" do describe "target is user-owned but contains read-only files" do
let(:command) { Hbc::FakeSystemCommand }
let(:chmod_cmd) {
["/bin/chmod", "-R", "--", "u+rwx", target_path]
}
let(:chmod_n_cmd) {
["/bin/chmod", "-R", "-N", target_path]
}
let(:chflags_cmd) {
["/usr/bin/chflags", "-R", "--", "000", target_path]
}
before do before do
system "/usr/bin/touch", "--", "#{target_path}/foo" system "/usr/bin/touch", "--", "#{target_path}/foo"
system "/bin/chmod", "--", "0555", target_path system "/bin/chmod", "--", "0555", target_path
end end
it "tries to make the target world-writable" do
Hbc::FakeSystemCommand.expect_and_pass_through(chflags_cmd)
Hbc::FakeSystemCommand.expect_and_pass_through(chmod_cmd)
Hbc::FakeSystemCommand.expect_and_pass_through(chmod_n_cmd)
shutup do
install_phase.call(command: Hbc::FakeSystemCommand)
end
end
it "overwrites the existing app" do it "overwrites the existing app" do
cask = local_caffeine command.expect_and_pass_through(chflags_cmd)
command.expect_and_pass_through(chmod_cmd)
command.expect_and_pass_through(chmod_n_cmd)
expected = [ install_phase.must_output <<-EOS.undent
"==> It seems there is already an App at '#{target_path}'; overwriting.", ==> It seems there is already an App at '#{target_path}'; overwriting.
"==> Removing App: '#{target_path}'", ==> Removing App: '#{target_path}'
"==> Moving App 'Caffeine.app' to '#{target_path}'", ==> Moving App 'Caffeine.app' to '#{target_path}'
] EOS
TestHelper.must_output(self, install_phase,
expected.join("\n"))
source_path = cask.staged_path.join("Caffeine.app") source_path.wont_be :exist?
target_path.must_be :directory?
File.exist?(source_path).must_equal false
File.ftype(target_path).must_equal "directory"
contents_path = target_path.join("Contents/Info.plist") contents_path = target_path.join("Contents/Info.plist")
File.exist?(contents_path).must_equal true contents_path.must_be :exist?
end end
after do after do
@ -179,15 +157,7 @@ describe Hbc::Artifact::App do
end end
describe "when the target is a broken symlink" do describe "when the target is a broken symlink" do
let(:target_path) { let(:deleted_path) { cask.staged_path.join("Deleted.app") }
Hbc.appdir.join("Caffeine.app")
}
let(:deleted_path) {
local_caffeine.staged_path.join(
"Deleted.app"
)
}
before do before do
deleted_path.mkdir deleted_path.mkdir
@ -196,105 +166,84 @@ describe Hbc::Artifact::App do
end end
it "leaves the target alone" do it "leaves the target alone" do
cask = local_caffeine install_phase.must_output <<-EOS.undent
TestHelper.must_output(self, lambda { ==> It seems there is already an App at '#{target_path}'; not moving.
Hbc::Artifact::App.new(cask).install_phase EOS
}, "==> It seems there is already an App at '#{target_path}'; not moving.")
File.symlink?(target_path).must_equal true File.symlink?(target_path).must_equal true
end end
describe "given the force option" do describe "given the force option" do
let(:install_phase) { let(:force) { true }
lambda do
Hbc::Artifact::App.new(
local_caffeine, force: true
).install_phase
end
}
it "overwrites the existing app" do it "overwrites the existing app" do
cask = local_caffeine install_phase.must_output <<-EOS.undent
==> It seems there is already an App at '#{target_path}'; overwriting.
==> Removing App: '#{target_path}'
==> Moving App 'Caffeine.app' to '#{target_path}'
EOS
expected = [ source_path.wont_be :exist?
"==> It seems there is already an App at '#{target_path}'; overwriting.", target_path.must_be :directory?
"==> Removing App: '#{target_path}'",
"==> Moving App 'Caffeine.app' to '#{target_path}'",
]
TestHelper.must_output(self, install_phase,
expected.join("\n"))
source_path = cask.staged_path.join("Caffeine.app")
File.exist?(source_path).must_equal false
File.ftype(target_path).must_equal "directory"
contents_path = target_path.join("Contents/Info.plist") contents_path = target_path.join("Contents/Info.plist")
File.exist?(contents_path).must_equal true contents_path.must_be :exist?
end end
end end
end end
it "gives a warning if the source doesn't exist" do it "gives a warning if the source doesn't exist" do
cask = local_caffeine source_path.rmtree
staged_app_path = cask.staged_path.join("Caffeine.app")
staged_app_path.rmtree
installation = -> { Hbc::Artifact::App.new(cask).install_phase } message = "It seems the App source is not there: '#{source_path}'"
message = "It seems the App source is not there: '#{staged_app_path}'"
error = installation.must_raise(Hbc::CaskError) error = install_phase.must_raise(Hbc::CaskError)
error.message.must_equal message error.message.must_equal message
end end
end end
describe "uninstall_phase" do describe "uninstall_phase" do
before do
shutup do
install_phase.call
end
end
it "deletes managed apps" do it "deletes managed apps" do
cask = local_caffeine target_path.must_be :exist?
shutup do shutup do
Hbc::Artifact::App.new(cask).install_phase uninstall_phase.call
Hbc::Artifact::App.new(cask).uninstall_phase
end end
app_path = Hbc.appdir.join("Caffeine.app") target_path.wont_be :exist?
File.exist?(app_path).must_equal false
end end
end end
describe "summary" do describe "summary" do
let(:description) { app.summary[:english_description] }
let(:contents) { app.summary[:contents] }
it "returns the correct english_description" do it "returns the correct english_description" do
cask = local_caffeine
description = Hbc::Artifact::App.new(cask).summary[:english_description]
description.must_equal "Apps" description.must_equal "Apps"
end end
describe "app is correctly installed" do describe "app is correctly installed" do
it "returns the path to the app" do before do
cask = local_caffeine
shutup do shutup do
Hbc::Artifact::App.new(cask).install_phase install_phase.call
end end
end
contents = Hbc::Artifact::App.new(cask).summary[:contents] it "returns the path to the app" do
app_path = Hbc.appdir.join("Caffeine.app") contents.must_equal ["#{target_path} (#{target_path.abv})"]
contents.must_equal ["#{app_path} (#{app_path.abv})"]
end end
end end
describe "app is missing" do describe "app is missing" do
it "returns a warning and the supposed path to the app" do it "returns a warning and the supposed path to the app" do
cask = local_caffeine
contents = Hbc::Artifact::App.new(cask).summary[:contents]
app_path = Hbc.appdir.join("Caffeine.app")
contents.size.must_equal 1 contents.size.must_equal 1
contents[0].must_match(%r{.*Missing App.*: #{app_path}}) contents[0].must_match(%r{.*Missing App.*: #{target_path}})
end end
end end
end end

View File

@ -1,44 +1,45 @@
require "test_helper" require "test_helper"
describe Hbc::Artifact::Artifact do describe Hbc::Artifact::Artifact do
let(:cask) { let(:cask) { Hbc.load("with-generic-artifact") }
Hbc.load("with-generic-artifact").tap do |cask|
TestHelper.install_without_artifacts(cask) let(:install_phase) {
lambda { Hbc::Artifact::Artifact.new(cask).install_phase }
}
let(:source_path) { cask.staged_path.join("Caffeine.app") }
let(:target_path) { Hbc.appdir.join("Caffeine.app") }
before do
TestHelper.install_without_artifacts(cask)
end
describe "with no target" do
let(:cask) { Hbc.load("with-generic-artifact-no-target") }
it "fails to install with no target" do
install_phase.must_raise Hbc::CaskInvalidError
end end
}
let(:expected_path) {
Hbc.appdir.join("Caffeine.app")
}
it "fails to install with no target" do
no_target = Hbc.load("with-generic-artifact-no-target")
TestHelper.install_without_artifacts(no_target)
lambda {
shutup do
Hbc::Artifact::Artifact.new(no_target).install_phase
end
}.must_raise(Hbc::CaskInvalidError)
end end
it "moves the artifact to the proper directory" do it "moves the artifact to the proper directory" do
shutup do shutup do
Hbc::Artifact::Artifact.new(cask).install_phase install_phase.call
end end
File.ftype(Hbc.appdir.join("Caffeine.app")).must_equal "directory" target_path.must_be :directory?
File.exist?(cask.staged_path.join("Caffeine.app")).must_equal false source_path.wont_be :exist?
end end
it "avoids clobbering an existing artifact" do it "avoids clobbering an existing artifact" do
FileUtils.touch expected_path target_path.mkpath
shutup do shutup do
Hbc::Artifact::Artifact.new(cask).install_phase install_phase.call
end end
source_path = cask.staged_path.join("Caffeine.app") source_path.must_be :directory?
target_path.must_be :directory?
File.identical?(source_path, expected_path).must_equal false File.identical?(source_path, target_path).must_equal false
end end
end end

View File

@ -1,41 +1,46 @@
require "test_helper" require "test_helper"
describe Hbc::Artifact::Suite do describe Hbc::Artifact::Suite do
let(:cask) { let(:cask) { Hbc.load("with-suite") }
Hbc.load("with-suite").tap do |cask|
TestHelper.install_without_artifacts(cask) let(:install_phase) {
end lambda { Hbc::Artifact::Suite.new(cask).install_phase }
}
let(:expected_path) {
Hbc.appdir.join("Caffeine")
} }
let(:target_path) { Hbc.appdir.join("Caffeine") }
let(:source_path) { cask.staged_path.join("Caffeine") } let(:source_path) { cask.staged_path.join("Caffeine") }
before do
TestHelper.install_without_artifacts(cask)
end
it "moves the suite to the proper directory" do it "moves the suite to the proper directory" do
shutup do shutup do
Hbc::Artifact::Suite.new(cask).install_phase install_phase.call
end end
expected_path.must_be :directory? target_path.must_be :directory?
TestHelper.valid_alias?(expected_path).must_equal false TestHelper.valid_alias?(target_path).must_equal false
File.exist?(source_path).must_equal false source_path.wont_be :exist?
end end
it "creates a suite containing the expected app" do it "creates a suite containing the expected app" do
shutup do shutup do
Hbc::Artifact::Suite.new(cask).install_phase install_phase.call
end end
expected_path.join("Caffeine.app").must_be :exist? target_path.join("Caffeine.app").must_be :exist?
end end
it "avoids clobbering an existing suite by moving over it" do it "avoids clobbering an existing suite by moving over it" do
FileUtils.touch expected_path target_path.mkpath
shutup do shutup do
Hbc::Artifact::Suite.new(cask).install_phase install_phase.call
end end
File.identical?(source_path, expected_path).must_equal false source_path.must_be :directory?
target_path.must_be :directory?
File.identical?(source_path, target_path).must_equal false
end end
end end

View File

@ -1,96 +1,90 @@
require "test_helper" require "test_helper"
describe Hbc::Artifact::App do describe Hbc::Artifact::App do
let(:local_two_apps_caffeine) {
Hbc.load("with-two-apps-correct").tap do |cask|
TestHelper.install_without_artifacts(cask)
end
}
let(:local_two_apps_subdir) {
Hbc.load("with-two-apps-subdir").tap do |cask|
TestHelper.install_without_artifacts(cask)
end
}
describe "multiple apps" do describe "multiple apps" do
it "installs both apps using the proper target directory" do let(:cask) { Hbc.load("with-two-apps-correct") }
cask = local_two_apps_caffeine
shutup do let(:install_phase) {
Hbc::Artifact::App.new(cask).install_phase lambda { Hbc::Artifact::App.new(cask).install_phase }
end }
File.ftype(Hbc.appdir.join("Caffeine Mini.app")).must_equal "directory" let(:source_path_mini) { cask.staged_path.join("Caffeine Mini.app") }
File.exist?(cask.staged_path.join("Caffeine Mini.app")).must_equal false let(:target_path_mini) { Hbc.appdir.join("Caffeine Mini.app") }
File.ftype(Hbc.appdir.join("Caffeine Pro.app")).must_equal "directory" let(:source_path_pro) { cask.staged_path.join("Caffeine Pro.app") }
File.exist?(cask.staged_path.join("Caffeine Pro.app")).must_equal false let(:target_path_pro) { Hbc.appdir.join("Caffeine Pro.app") }
before do
TestHelper.install_without_artifacts(cask)
end end
it "works with an application in a subdir" do it "installs both apps using the proper target directory" do
cask = local_two_apps_subdir
TestHelper.install_without_artifacts(cask)
shutup do shutup do
Hbc::Artifact::App.new(cask).install_phase install_phase.call
end end
File.ftype(Hbc.appdir.join("Caffeine Mini.app")).must_equal "directory" target_path_mini.must_be :directory?
File.exist?(cask.staged_path.join("Caffeine Mini.app")).must_equal false source_path_mini.wont_be :exist?
File.ftype(Hbc.appdir.join("Caffeine Pro.app")).must_equal "directory" target_path_pro.must_be :directory?
File.exist?(cask.staged_path.join("Caffeine Pro.app")).must_equal false source_path_pro.wont_be :exist?
end
describe "when apps are in a subdirectory" do
let(:cask) { Hbc.load("with-two-apps-subdir") }
it "installs both apps using the proper target directory" do
shutup do
install_phase.call
end
target_path_mini.must_be :directory?
source_path_mini.wont_be :exist?
target_path_pro.must_be :directory?
source_path_pro.wont_be :exist?
end
end end
it "only uses apps when they are specified" do it "only uses apps when they are specified" do
cask = local_two_apps_caffeine FileUtils.cp_r source_path_mini, source_path_mini.sub("Caffeine Mini.app", "Caffeine Deluxe.app")
app_path = cask.staged_path.join("Caffeine Mini.app")
FileUtils.cp_r app_path, app_path.sub("Caffeine Mini.app", "Caffeine Deluxe.app")
shutup do shutup do
Hbc::Artifact::App.new(cask).install_phase install_phase.call
end end
File.ftype(Hbc.appdir.join("Caffeine Mini.app")).must_equal "directory" target_path_mini.must_be :directory?
File.exist?(cask.staged_path.join("Caffeine Mini.app")).must_equal false source_path_mini.wont_be :exist?
File.exist?(Hbc.appdir.join("Caffeine Deluxe.app")).must_equal false Hbc.appdir.join("Caffeine Deluxe.app").wont_be :exist?
File.exist?(cask.staged_path.join("Caffeine Deluxe.app")).must_equal true cask.staged_path.join("Caffeine Deluxe.app").must_be :exist?
end end
describe "avoids clobbering an existing app" do describe "avoids clobbering an existing app" do
let(:cask) { local_two_apps_caffeine }
it "when the first app of two already exists" do it "when the first app of two already exists" do
Hbc.appdir.join("Caffeine Mini.app").mkpath target_path_mini.mkpath
TestHelper.must_output(self, lambda { install_phase.must_output <<-EOS.undent
Hbc::Artifact::App.new(cask).install_phase ==> It seems there is already an App at '#{target_path_mini}'; not moving.
}, <<-EOS.undent.chomp) ==> Moving App 'Caffeine Pro.app' to '#{target_path_pro}'
==> It seems there is already an App at '#{Hbc.appdir.join("Caffeine Mini.app")}'; not moving. EOS
==> Moving App 'Caffeine Pro.app' to '#{Hbc.appdir.join("Caffeine Pro.app")}'
EOS
source_path = cask.staged_path.join("Caffeine Mini.app") source_path_mini.must_be :directory?
target_path_mini.must_be :directory?
File.identical?(source_path, Hbc.appdir.join("Caffeine Mini.app")).must_equal false File.identical?(source_path_mini, target_path_mini).must_equal false
end end
it "when the second app of two already exists" do it "when the second app of two already exists" do
Hbc.appdir.join("Caffeine Pro.app").mkpath target_path_pro.mkpath
TestHelper.must_output(self, lambda { install_phase.must_output <<-EOS.undent
Hbc::Artifact::App.new(cask).install_phase ==> Moving App 'Caffeine Mini.app' to '#{target_path_mini}'
}, <<-EOS.undent.chomp) ==> It seems there is already an App at '#{target_path_pro}'; not moving.
==> Moving App 'Caffeine Mini.app' to '#{Hbc.appdir.join("Caffeine Mini.app")}' EOS
==> It seems there is already an App at '#{Hbc.appdir.join("Caffeine Pro.app")}'; not moving.
EOS
source_path = cask.staged_path.join("Caffeine Pro.app") source_path_pro.must_be :directory?
target_path_pro.must_be :directory?
File.identical?(source_path, Hbc.appdir.join("Caffeine Pro.app")).must_equal false File.identical?(source_path_pro, target_path_pro).must_equal false
end end
end end
end end

View File

@ -27,9 +27,9 @@ describe Hbc::CLI::Install do
Hbc::CLI::Install.run("local-transmission") Hbc::CLI::Install.run("local-transmission")
end end
TestHelper.must_output(self, lambda { lambda {
Hbc::CLI::Install.run("local-transmission", "") Hbc::CLI::Install.run("local-transmission", "")
}, %r{Warning: A Cask for local-transmission is already installed.}) }.must_output nil, %r{Warning: A Cask for local-transmission is already installed.}
end end
it "allows double install with --force" do it "allows double install with --force" do
@ -37,9 +37,9 @@ describe Hbc::CLI::Install do
Hbc::CLI::Install.run("local-transmission") Hbc::CLI::Install.run("local-transmission")
end end
TestHelper.must_output(self, lambda { lambda {
Hbc::CLI::Install.run("local-transmission", "--force") Hbc::CLI::Install.run("local-transmission", "--force")
}, %r{==> Success! local-transmission was successfully installed!}) }.must_output %r{==> Success! local-transmission was successfully installed!}
end end
it "skips dependencies with --skip-cask-deps" do it "skips dependencies with --skip-cask-deps" do
@ -60,25 +60,19 @@ describe Hbc::CLI::Install do
end end
it "returns a suggestion for a misspelled Cask" do it "returns a suggestion for a misspelled Cask" do
_, err = capture_io do lambda {
begin begin
Hbc::CLI::Install.run("googlechrome") Hbc::CLI::Install.run("googlechrome")
rescue Hbc::CaskError rescue Hbc::CaskError; end
return }.must_output nil, %r{No available Cask for googlechrome\. Did you mean:\ngoogle-chrome}
end
end
err.must_match %r{No available Cask for googlechrome\. Did you mean:\ngoogle-chrome}
end end
it "returns multiple suggestions for a Cask fragment" do it "returns multiple suggestions for a Cask fragment" do
_, err = capture_io do lambda {
begin begin
Hbc::CLI::Install.run("google") Hbc::CLI::Install.run("google")
rescue Hbc::CaskError rescue Hbc::CaskError; end
return }.must_output nil, %r{No available Cask for google\. Did you mean one of:\ngoogle}
end
end
err.must_match %r{No available Cask for google\. Did you mean one of:\ngoogle}
end end
describe "when no Cask is specified" do describe "when no Cask is specified" do

View File

@ -78,11 +78,11 @@ describe Hbc::CLI::List do
lambda { lambda {
Hbc::CLI::List.run("local-transmission", "local-caffeine") Hbc::CLI::List.run("local-transmission", "local-caffeine")
}.must_output <<-EOS.undent }.must_output <<-EOS.undent
==> Apps ==> Apps
#{Hbc.appdir.join("Transmission.app")} (#{Hbc.appdir.join("Transmission.app").abv}) #{Hbc.appdir.join("Transmission.app")} (#{Hbc.appdir.join("Transmission.app").abv})
==> Apps ==> Apps
Missing App: #{Hbc.appdir.join("Caffeine.app")} Missing App: #{Hbc.appdir.join("Caffeine.app")}
EOS EOS
end end
end end
end end

View File

@ -36,9 +36,9 @@ describe Hbc::CLI::Uninstall do
end end
caffeine.wont_be :installed? caffeine.wont_be :installed?
File.exist?(Hbc.appdir.join("Transmission.app")).must_equal false Hbc.appdir.join("Transmission.app").wont_be :exist?
transmission.wont_be :installed? transmission.wont_be :installed?
File.exist?(Hbc.appdir.join("Caffeine.app")).must_equal false Hbc.appdir.join("Caffeine.app").wont_be :exist?
end end
describe "when multiple versions of a cask are installed" do describe "when multiple versions of a cask are installed" do

View File

@ -4,6 +4,6 @@ describe "brew cask --version" do
it "respects the --version argument" do it "respects the --version argument" do
lambda { lambda {
Hbc::CLI::NullCommand.new("--version").run Hbc::CLI::NullCommand.new("--version").run
}.must_output "#{Hbc.full_version}\n" }.must_output Hbc.full_version
end end
end end

View File

@ -31,12 +31,12 @@ describe Hbc::DSL do
https://github.com/caskroom/homebrew-cask#reporting-bugs https://github.com/caskroom/homebrew-cask#reporting-bugs
EOS EOS
TestHelper.must_output(self, attempt_unknown_method, expected) attempt_unknown_method.must_output nil, expected
end end
it "will simply warn, not throw an exception" do it "will simply warn, not throw an exception" do
begin begin
capture_subprocess_io do shutup do
attempt_unknown_method.call attempt_unknown_method.call
end end
rescue StandardError => e rescue StandardError => e
@ -70,11 +70,13 @@ describe Hbc::DSL do
it "may use deprecated DSL version hash syntax" do it "may use deprecated DSL version hash syntax" do
with_environment "HOMEBREW_DEVELOPER" => nil do with_environment "HOMEBREW_DEVELOPER" => nil do
test_cask = Hbc.load("with-dsl-version") shutup do
test_cask.token.must_equal "with-dsl-version" test_cask = Hbc.load("with-dsl-version")
test_cask.url.to_s.must_equal "http://example.com/TestCask.dmg" test_cask.token.must_equal "with-dsl-version"
test_cask.homepage.must_equal "http://example.com/" test_cask.url.to_s.must_equal "http://example.com/TestCask.dmg"
test_cask.version.to_s.must_equal "1.2.3" test_cask.homepage.must_equal "http://example.com/"
test_cask.version.to_s.must_equal "1.2.3"
end
end end
end end
end end

View File

@ -237,17 +237,17 @@ describe Hbc::Installer do
it "prints caveats if they're present" do it "prints caveats if they're present" do
with_caveats = Hbc.load("with-caveats") with_caveats = Hbc.load("with-caveats")
TestHelper.must_output(self, lambda { lambda {
Hbc::Installer.new(with_caveats).install Hbc::Installer.new(with_caveats).install
}, %r{Here are some things you might want to know}) }.must_output %r{Here are some things you might want to know}
with_caveats.must_be :installed? with_caveats.must_be :installed?
end end
it "prints installer :manual instructions when present" do it "prints installer :manual instructions when present" do
with_installer_manual = Hbc.load("with-installer-manual") with_installer_manual = Hbc.load("with-installer-manual")
TestHelper.must_output(self, lambda { lambda {
Hbc::Installer.new(with_installer_manual).install Hbc::Installer.new(with_installer_manual).install
}, %r{To complete the installation of Cask with-installer-manual, you must also\nrun the installer at\n\n '#{with_installer_manual.staged_path.join('Caffeine.app')}'}) }.must_output %r{To complete the installation of Cask with-installer-manual, you must also\nrun the installer at\n\n '#{with_installer_manual.staged_path.join('Caffeine.app')}'}
with_installer_manual.must_be :installed? with_installer_manual.must_be :installed?
end end
@ -348,7 +348,7 @@ describe Hbc::Installer do
end end
dest_path = Hbc.appdir.join("MyNestedApp.app") dest_path = Hbc.appdir.join("MyNestedApp.app")
File.ftype(dest_path).must_equal "directory" dest_path.must_be :directory?
end end
it "generates and finds a timestamped metadata directory for an installed Cask" do it "generates and finds a timestamped metadata directory for an installed Cask" do

View File

@ -93,20 +93,6 @@ class TestHelper
Hbc::FakeFetcher.fake_response_for(*args) Hbc::FakeFetcher.fake_response_for(*args)
end end
def self.must_output(test, lambda, expected = nil)
out, err = test.capture_subprocess_io do
lambda.call
end
if block_given?
yield (out + err).chomp
elsif expected.is_a?(Regexp)
(out + err).chomp.must_match expected
else
(out + err).chomp.must_equal expected.gsub(%r{^ *}, "")
end
end
def self.valid_alias?(candidate) def self.valid_alias?(candidate)
return false unless candidate.symlink? return false unless candidate.symlink?
candidate.readlink.exist? candidate.readlink.exist?