Convert App test to spec.

This commit is contained in:
Markus Reiter 2017-02-08 14:23:18 +01:00
parent 75609f7212
commit 20e85cc96f

View File

@ -1,4 +1,4 @@
require "test_helper" require "spec_helper"
describe Hbc::Artifact::App do describe Hbc::Artifact::App do
let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/local-caffeine.rb") } let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/local-caffeine.rb") }
@ -12,8 +12,8 @@ describe Hbc::Artifact::App do
let(:install_phase) { -> { app.install_phase } } let(:install_phase) { -> { app.install_phase } }
let(:uninstall_phase) { -> { app.uninstall_phase } } let(:uninstall_phase) { -> { app.uninstall_phase } }
before do before(:each) do
TestHelper.install_without_artifacts(cask) InstallHelper.install_without_artifacts(cask)
end end
describe "install_phase" do describe "install_phase" do
@ -22,8 +22,8 @@ describe Hbc::Artifact::App do
install_phase.call install_phase.call
end end
target_path.must_be :directory? expect(target_path).to be_a_directory
source_path.wont_be :exist? expect(source_path).not_to exist
end end
describe "when app is in a subdirectory" do describe "when app is in a subdirectory" do
@ -45,8 +45,8 @@ describe Hbc::Artifact::App do
install_phase.call install_phase.call
end end
target_path.must_be :directory? expect(target_path).to be_a_directory
appsubdir.join("Caffeine.app").wont_be :exist? expect(appsubdir.join("Caffeine.app")).not_to exist
end end
end end
@ -58,36 +58,34 @@ describe Hbc::Artifact::App do
install_phase.call install_phase.call
end end
target_path.must_be :directory? expect(target_path).to be_a_directory
source_path.wont_be :exist? expect(source_path).not_to exist
Hbc.appdir.join("Caffeine Deluxe.app").wont_be :exist? expect(Hbc.appdir.join("Caffeine Deluxe.app")).not_to exist
cask.staged_path.join("Caffeine Deluxe.app").must_be :exist? expect(cask.staged_path.join("Caffeine Deluxe.app")).to exist
end end
describe "when the target already exists" do describe "when the target already exists" do
before do before(:each) do
target_path.mkpath target_path.mkpath
end end
it "avoids clobbering an existing app" do it "avoids clobbering an existing app" do
err = install_phase.must_raise(Hbc::CaskError) expect(install_phase).to raise_error(Hbc::CaskError, "It seems there is already an App at '#{target_path}'.")
err.message.must_equal("It seems there is already an App at '#{target_path}'.") expect(source_path).to be_a_directory
expect(target_path).to be_a_directory
source_path.must_be :directory? expect(File.identical?(source_path, target_path)).to be false
target_path.must_be :directory?
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")
contents_path.wont_be :exist? expect(contents_path).not_to exist
end end
describe "given the force option" do describe "given the force option" do
let(:force) { true } let(:force) { true }
before do before(:each) do
Hbc::Utils.stubs(current_user: "fake_user") allow(Hbc::Utils).to receive(:current_user).and_return("fake_user")
end end
describe "target is both writable and user-owned" do describe "target is both writable and user-owned" do
@ -101,40 +99,31 @@ describe Hbc::Artifact::App do
Warning: It seems there is already an App at '#{target_path}'; overwriting. Warning: It seems there is already an App at '#{target_path}'; overwriting.
EOS EOS
install_phase.must_output(stdout, stderr) expect {
expect(install_phase).to output(stdout).to_stdout
}.to output(stderr).to_stderr
source_path.wont_be :exist? expect(source_path).not_to exist
target_path.must_be :directory? expect(target_path).to be_a_directory
contents_path = target_path.join("Contents/Info.plist") contents_path = target_path.join("Contents/Info.plist")
contents_path.must_be :exist? expect(contents_path).to 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 } before(:each) do
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
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 "overwrites the existing app" do it "overwrites the existing app" do
command.expect_and_pass_through(chflags_cmd) expect(command).to receive(:run).with("/bin/chmod", args: ["-R", "--", "u+rwx", target_path], must_succeed: false)
command.expect_and_pass_through(chmod_cmd) .and_call_original
command.expect_and_pass_through(chmod_n_cmd) expect(command).to receive(:run).with("/bin/chmod", args: ["-R", "-N", target_path], must_succeed: false)
.and_call_original
expect(command).to receive(:run).with("/usr/bin/chflags", args: ["-R", "--", "000", target_path], must_succeed: false)
.and_call_original
stdout = <<-EOS.undent stdout = <<-EOS.undent
==> Removing App: '#{target_path}' ==> Removing App: '#{target_path}'
@ -145,16 +134,18 @@ describe Hbc::Artifact::App do
Warning: It seems there is already an App at '#{target_path}'; overwriting. Warning: It seems there is already an App at '#{target_path}'; overwriting.
EOS EOS
install_phase.must_output(stdout, stderr) expect {
expect(install_phase).to output(stdout).to_stdout
}.to output(stderr).to_stderr
source_path.wont_be :exist? expect(source_path).not_to exist
target_path.must_be :directory? expect(target_path).to be_a_directory
contents_path = target_path.join("Contents/Info.plist") contents_path = target_path.join("Contents/Info.plist")
contents_path.must_be :exist? expect(contents_path).to exist
end end
after do after(:each) do
system "/bin/chmod", "--", "0755", target_path system "/bin/chmod", "--", "0755", target_path
end end
end end
@ -164,18 +155,15 @@ describe Hbc::Artifact::App do
describe "when the target is a broken symlink" do describe "when the target is a broken symlink" do
let(:deleted_path) { cask.staged_path.join("Deleted.app") } let(:deleted_path) { cask.staged_path.join("Deleted.app") }
before do before(:each) do
deleted_path.mkdir deleted_path.mkdir
File.symlink(deleted_path, target_path) File.symlink(deleted_path, target_path)
deleted_path.rmdir deleted_path.rmdir
end end
it "leaves the target alone" do it "leaves the target alone" do
err = install_phase.must_raise(Hbc::CaskError) expect(install_phase).to raise_error(Hbc::CaskError, "It seems there is already an App at '#{target_path}'.")
expect(target_path).to be_a_symlink
err.message.must_equal("It seems there is already an App at '#{target_path}'.")
File.symlink?(target_path).must_equal true
end end
describe "given the force option" do describe "given the force option" do
@ -191,13 +179,15 @@ describe Hbc::Artifact::App do
Warning: It seems there is already an App at '#{target_path}'; overwriting. Warning: It seems there is already an App at '#{target_path}'; overwriting.
EOS EOS
install_phase.must_output(stdout, stderr) expect {
expect(install_phase).to output(stdout).to_stdout
}.to output(stderr).to_stderr
source_path.wont_be :exist? expect(source_path).not_to exist
target_path.must_be :directory? expect(target_path).to be_a_directory
contents_path = target_path.join("Contents/Info.plist") contents_path = target_path.join("Contents/Info.plist")
contents_path.must_be :exist? expect(contents_path).to exist
end end
end end
end end
@ -207,26 +197,23 @@ describe Hbc::Artifact::App do
message = "It seems the App source is not there: '#{source_path}'" message = "It seems the App source is not there: '#{source_path}'"
error = install_phase.must_raise(Hbc::CaskError) expect(install_phase).to raise_error(Hbc::CaskError, message)
error.message.must_equal message
end end
end end
describe "uninstall_phase" do describe "uninstall_phase" do
before do it "deletes managed apps" do
shutup do shutup do
install_phase.call install_phase.call
end end
end
it "deletes managed apps" do expect(target_path).to exist
target_path.must_be :exist?
shutup do shutup do
uninstall_phase.call uninstall_phase.call
end end
target_path.wont_be :exist? expect(target_path).not_to exist
end end
end end
@ -235,25 +222,23 @@ describe Hbc::Artifact::App do
let(:contents) { app.summary[:contents] } let(:contents) { app.summary[:contents] }
it "returns the correct english_description" do it "returns the correct english_description" do
description.must_equal "Apps" expect(description).to eq("Apps")
end end
describe "app is correctly installed" do describe "app is correctly installed" do
before do it "returns the path to the app" do
shutup do shutup do
install_phase.call install_phase.call
end end
end
it "returns the path to the app" do expect(contents).to eq(["#{target_path} (#{target_path.abv})"])
contents.must_equal ["#{target_path} (#{target_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
contents.size.must_equal 1 expect(contents.size).to eq(1)
contents[0].must_match(/.*Missing App.*: #{target_path}/) expect(contents[0]).to match(/.*Missing App.*: #{target_path}/)
end end
end end
end end