Refactor tests.

This commit is contained in:
Markus Reiter 2017-10-04 17:54:52 +02:00
parent 02362259a5
commit e9c587d751
14 changed files with 76 additions and 46 deletions

View File

@ -3,7 +3,11 @@ describe Hbc::Artifact::App, :cask do
let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-alt-target.rb") } let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-alt-target.rb") }
let(:install_phase) { let(:install_phase) {
-> { cask.artifacts.select { |a| a.is_a?(described_class) }.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } } lambda do
cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact|
artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
end
} }
let(:source_path) { cask.staged_path.join("Caffeine.app") } let(:source_path) { cask.staged_path.join("Caffeine.app") }

View File

@ -2,7 +2,7 @@ describe Hbc::Artifact::App, :cask 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") }
let(:command) { Hbc::SystemCommand } let(:command) { Hbc::SystemCommand }
let(:force) { false } let(:force) { false }
let(:app) { cask.artifacts.select { |a| a.is_a?(described_class) }.first } let(:app) { cask.artifacts.find { |a| a.is_a?(described_class) } }
let(:source_path) { cask.staged_path.join("Caffeine.app") } let(:source_path) { cask.staged_path.join("Caffeine.app") }
let(:target_path) { Hbc.appdir.join("Caffeine.app") } let(:target_path) { Hbc.appdir.join("Caffeine.app") }

View File

@ -4,6 +4,7 @@ describe Hbc::Artifact::Binary, :cask do
InstallHelper.install_without_artifacts(cask) InstallHelper.install_without_artifacts(cask)
end end
} }
let(:artifacts) { cask.artifacts.select { |a| a.is_a?(described_class) } }
let(:expected_path) { Hbc.binarydir.join("binary") } let(:expected_path) { Hbc.binarydir.join("binary") }
before(:each) do before(:each) do
@ -26,8 +27,9 @@ describe Hbc::Artifact::Binary, :cask do
end end
it "links the binary to the proper directory" do it "links the binary to the proper directory" do
cask.artifacts.select { |a| a.is_a?(described_class) } artifacts.each do |artifact|
.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
expect(expected_path).to be_a_symlink expect(expected_path).to be_a_symlink
expect(expected_path.readlink).to exist expect(expected_path.readlink).to exist
@ -46,8 +48,9 @@ describe Hbc::Artifact::Binary, :cask do
expect(FileUtils).to receive(:chmod) expect(FileUtils).to receive(:chmod)
.with("+x", cask.staged_path.join("naked_non_executable")).and_call_original .with("+x", cask.staged_path.join("naked_non_executable")).and_call_original
cask.artifacts.select { |a| a.is_a?(described_class) } artifacts.each do |artifact|
.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
expect(expected_path).to be_a_symlink expect(expected_path).to be_a_symlink
expect(expected_path.readlink).to be_executable expect(expected_path.readlink).to be_executable
@ -58,8 +61,9 @@ describe Hbc::Artifact::Binary, :cask do
FileUtils.touch expected_path FileUtils.touch expected_path
expect { expect {
cask.artifacts.select { |a| a.is_a?(described_class) } artifacts.each do |artifact|
.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
}.to raise_error(Hbc::CaskError) }.to raise_error(Hbc::CaskError)
expect(expected_path).not_to be :symlink? expect(expected_path).not_to be :symlink?
@ -68,8 +72,9 @@ describe Hbc::Artifact::Binary, :cask do
it "clobbers an existing symlink" do it "clobbers an existing symlink" do
expected_path.make_symlink("/tmp") expected_path.make_symlink("/tmp")
cask.artifacts.select { |a| a.is_a?(described_class) } artifacts.each do |artifact|
.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
expect(File.readlink(expected_path)).not_to eq("/tmp") expect(File.readlink(expected_path)).not_to eq("/tmp")
end end
@ -77,8 +82,9 @@ describe Hbc::Artifact::Binary, :cask do
it "creates parent directory if it doesn't exist" do it "creates parent directory if it doesn't exist" do
FileUtils.rmdir Hbc.binarydir FileUtils.rmdir Hbc.binarydir
cask.artifacts.select { |a| a.is_a?(described_class) } artifacts.each do |artifact|
.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
expect(expected_path.exist?).to be true expect(expected_path.exist?).to be true
end end
@ -91,10 +97,12 @@ describe Hbc::Artifact::Binary, :cask do
} }
it "links the binary to the proper directory" do it "links the binary to the proper directory" do
cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::App) } cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::App) }.each do |artifact|
.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
cask.artifacts.select { |a| a.is_a?(described_class) } end
.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifacts.each do |artifact|
artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
expect(expected_path).to be_a_symlink expect(expected_path).to be_a_symlink
expect(expected_path.readlink).to exist expect(expected_path.readlink).to exist

View File

@ -2,7 +2,11 @@ describe Hbc::Artifact::Artifact, :cask do
let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-generic-artifact.rb") } let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-generic-artifact.rb") }
let(:install_phase) { let(:install_phase) {
-> { cask.artifacts.select { |a| a.is_a?(described_class) }.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } } lambda do
cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact|
artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
end
} }
let(:source_path) { cask.staged_path.join("Caffeine.app") } let(:source_path) { cask.staged_path.join("Caffeine.app") }

View File

@ -5,8 +5,9 @@ describe Hbc::Artifact::NestedContainer, :cask do
InstallHelper.install_without_artifacts(c) InstallHelper.install_without_artifacts(c)
end end
cask.artifacts.select { |a| a.is_a?(described_class) } cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact|
.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
expect(cask.staged_path.join("MyNestedApp.app")).to be_a_directory expect(cask.staged_path.join("MyNestedApp.app")).to be_a_directory
end end

View File

@ -8,7 +8,7 @@ describe Hbc::Artifact::Pkg, :cask do
describe "install_phase" do describe "install_phase" do
it "runs the system installer on the specified pkgs" do it "runs the system installer on the specified pkgs" do
pkg = cask.artifacts.select { |a| a.is_a?(described_class) }.first pkg = cask.artifacts.find { |a| a.is_a?(described_class) }
expect(fake_system_command).to receive(:run!).with( expect(fake_system_command).to receive(:run!).with(
"/usr/sbin/installer", "/usr/sbin/installer",
@ -25,7 +25,7 @@ describe Hbc::Artifact::Pkg, :cask do
let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-choices.rb") } let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-choices.rb") }
it "passes the choice changes xml to the system installer" do it "passes the choice changes xml to the system installer" do
pkg = cask.artifacts.select { |a| a.is_a?(described_class) }.first pkg = cask.artifacts.find { |a| a.is_a?(described_class) }
file = double(path: Pathname.new("/tmp/choices.xml")) file = double(path: Pathname.new("/tmp/choices.xml"))

View File

@ -11,8 +11,9 @@ describe Hbc::Artifact::PostflightBlock, :cask do
end end
end end
cask.artifacts.select { |a| a.is_a?(described_class) } cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact|
.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
expect(called).to be true expect(called).to be true
expect(yielded_arg).to be_kind_of(Hbc::DSL::Postflight) expect(yielded_arg).to be_kind_of(Hbc::DSL::Postflight)
@ -31,8 +32,9 @@ describe Hbc::Artifact::PostflightBlock, :cask do
end end
end end
cask.artifacts.select { |a| a.is_a?(described_class) } cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact|
.each { |artifact| artifact.uninstall_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.uninstall_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
expect(called).to be true expect(called).to be true
expect(yielded_arg).to be_kind_of(Hbc::DSL::UninstallPostflight) expect(yielded_arg).to be_kind_of(Hbc::DSL::UninstallPostflight)

View File

@ -11,8 +11,9 @@ describe Hbc::Artifact::PreflightBlock, :cask do
end end
end end
cask.artifacts.select { |a| a.is_a?(described_class) } cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact|
.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
expect(called).to be true expect(called).to be true
expect(yielded_arg).to be_kind_of Hbc::DSL::Preflight expect(yielded_arg).to be_kind_of Hbc::DSL::Preflight
@ -31,8 +32,9 @@ describe Hbc::Artifact::PreflightBlock, :cask do
end end
end end
cask.artifacts.select { |a| a.is_a?(described_class) } cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact|
.each { |artifact| artifact.uninstall_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.uninstall_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
expect(called).to be true expect(called).to be true
expect(yielded_arg).to be_kind_of Hbc::DSL::UninstallPreflight expect(yielded_arg).to be_kind_of Hbc::DSL::UninstallPreflight

View File

@ -2,7 +2,11 @@ describe Hbc::Artifact::Suite, :cask do
let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-suite.rb") } let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-suite.rb") }
let(:install_phase) { let(:install_phase) {
-> { cask.artifacts.select { |a| a.is_a?(described_class) }.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } } lambda do
cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact|
artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
end
} }
let(:target_path) { Hbc.appdir.join("Caffeine") } let(:target_path) { Hbc.appdir.join("Caffeine") }

View File

@ -3,7 +3,11 @@ describe Hbc::Artifact::App, :cask do
let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-two-apps-correct.rb") } let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-two-apps-correct.rb") }
let(:install_phase) { let(:install_phase) {
-> { cask.artifacts.select { |a| a.is_a?(described_class) }.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } } lambda do
cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact|
artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
end
} }
let(:source_path_mini) { cask.staged_path.join("Caffeine Mini.app") } let(:source_path_mini) { cask.staged_path.join("Caffeine Mini.app") }

View File

@ -2,7 +2,7 @@ describe Hbc::Artifact::Zap, :cask do
let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-installable.rb") } let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-installable.rb") }
let(:zap_artifact) { let(:zap_artifact) {
cask.artifacts.select { |a| a.is_a?(described_class) }.first cask.artifacts.find { |a| a.is_a?(described_class) }
} }
before(:each) do before(:each) do

View File

@ -1,6 +1,6 @@
shared_examples "#uninstall_phase or #zap_phase" do shared_examples "#uninstall_phase or #zap_phase" do
let(:artifact_dsl_key) { described_class.dsl_key } let(:artifact_dsl_key) { described_class.dsl_key }
let(:artifact) { cask.artifacts.select { |a| a.is_a?(described_class) }.first } let(:artifact) { cask.artifacts.find { |a| a.is_a?(described_class) } }
let(:fake_system_command) { Hbc::FakeSystemCommand } let(:fake_system_command) { Hbc::FakeSystemCommand }
subject { artifact.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command) } subject { artifact.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command) }

View File

@ -72,8 +72,9 @@ describe Hbc::CLI::List, :cask do
it "lists the installed files for those Casks" do it "lists the installed files for those Casks" do
casks.each(&InstallHelper.method(:install_without_artifacts_with_caskfile)) casks.each(&InstallHelper.method(:install_without_artifacts_with_caskfile))
transmission.artifacts.select { |a| a.is_a?(Hbc::Artifact::App) } transmission.artifacts.select { |a| a.is_a?(Hbc::Artifact::App) }.each do |artifact|
.each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
expect { expect {
described_class.run("local-transmission", "local-caffeine") described_class.run("local-transmission", "local-caffeine")

View File

@ -216,12 +216,12 @@ describe Hbc::DSL, :cask do
app "Bar.app" app "Bar.app"
end end
expect(cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::App) }.map(&:to_s)).to eq(["Foo.app (App)", "Bar.app (App)"]) expect(cask.artifacts.map(&:to_s)).to eq(["Foo.app (App)", "Bar.app (App)"])
end end
it "allow app stanzas to be empty" do it "allow app stanzas to be empty" do
cask = Hbc::Cask.new("cask-with-no-apps") cask = Hbc::Cask.new("cask-with-no-apps")
expect(cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::App) }).to be_empty expect(cask.artifacts).to be_empty
end end
end end
@ -249,7 +249,7 @@ describe Hbc::DSL, :cask do
pkg "Bar.pkg" pkg "Bar.pkg"
end end
expect(cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::Pkg) }.map(&:to_s)).to eq(["Foo.pkg (Pkg)", "Bar.pkg (Pkg)"]) expect(cask.artifacts.map(&:to_s)).to eq(["Foo.pkg (Pkg)", "Bar.pkg (Pkg)"])
end end
end end
@ -501,10 +501,10 @@ describe Hbc::DSL, :cask do
let(:token) { "with-installer-script" } let(:token) { "with-installer-script" }
it "allows installer script to be specified" do it "allows installer script to be specified" do
expect(cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::Installer) }.first.path).to eq(Pathname("/usr/bin/true")) expect(cask.artifacts.to_a[0].path).to eq(Pathname("/usr/bin/true"))
expect(cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::Installer) }.first.args[:args]).to eq(["--flag"]) expect(cask.artifacts.to_a[0].args[:args]).to eq(["--flag"])
expect(cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::Installer) }.to_a[1].path).to eq(Pathname("/usr/bin/false")) expect(cask.artifacts.to_a[1].path).to eq(Pathname("/usr/bin/false"))
expect(cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::Installer) }.to_a[1].args[:args]).to eq(["--flag"]) expect(cask.artifacts.to_a[1].args[:args]).to eq(["--flag"])
end end
end end
@ -512,7 +512,7 @@ describe Hbc::DSL, :cask do
let(:token) { "with-installer-manual" } let(:token) { "with-installer-manual" }
it "allows installer manual to be specified" do it "allows installer manual to be specified" do
installer = cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::Installer) }.first installer = cask.artifacts.first
expect(installer).to be_a(Hbc::Artifact::Installer::ManualInstaller) expect(installer).to be_a(Hbc::Artifact::Installer::ManualInstaller)
expect(installer.path).to eq(cask.staged_path.join("Caffeine.app")) expect(installer.path).to eq(cask.staged_path.join("Caffeine.app"))
end end
@ -524,7 +524,7 @@ describe Hbc::DSL, :cask do
let(:token) { "stage-only" } let(:token) { "stage-only" }
it "allows stage_only stanza to be specified" do it "allows stage_only stanza to be specified" do
expect(cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::StageOnly) }).not_to be_empty expect(cask.artifacts).to contain_exactly a_kind_of Hbc::Artifact::StageOnly
end end
end end
@ -550,7 +550,7 @@ describe Hbc::DSL, :cask do
let(:token) { "appdir-interpolation" } let(:token) { "appdir-interpolation" }
it "is allowed" do it "is allowed" do
expect(cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::Binary) }.first.source).to eq(Hbc.appdir/"some/path") expect(cask.artifacts.first.source).to eq(Hbc.appdir/"some/path")
end end
end end
@ -563,7 +563,7 @@ describe Hbc::DSL, :cask do
binary "#{appdir}/some/path" binary "#{appdir}/some/path"
end end
expect(cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::Binary) }.first.source).to eq(original_appdir/"some/path") expect(cask.artifacts.first.source).to eq(original_appdir/"some/path")
ensure ensure
Hbc.appdir = original_appdir Hbc.appdir = original_appdir
end end