From e9c587d7510ca6fe820bbc03db5bb6d0b9672c22 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 4 Oct 2017 17:54:52 +0200 Subject: [PATCH] Refactor tests. --- .../test/cask/artifact/alt_target_spec.rb | 6 +++- .../Homebrew/test/cask/artifact/app_spec.rb | 2 +- .../test/cask/artifact/binary_spec.rb | 36 +++++++++++-------- .../cask/artifact/generic_artifact_spec.rb | 6 +++- .../cask/artifact/nested_container_spec.rb | 5 +-- .../Homebrew/test/cask/artifact/pkg_spec.rb | 4 +-- .../cask/artifact/postflight_block_spec.rb | 10 +++--- .../cask/artifact/preflight_block_spec.rb | 10 +++--- .../Homebrew/test/cask/artifact/suite_spec.rb | 6 +++- .../cask/artifact/two_apps_correct_spec.rb | 6 +++- .../cask/artifact/uninstall_no_zap_spec.rb | 2 +- .../artifact/uninstall_zap_shared_examples.rb | 2 +- Library/Homebrew/test/cask/cli/list_spec.rb | 5 +-- Library/Homebrew/test/cask/dsl_spec.rb | 22 ++++++------ 14 files changed, 76 insertions(+), 46 deletions(-) diff --git a/Library/Homebrew/test/cask/artifact/alt_target_spec.rb b/Library/Homebrew/test/cask/artifact/alt_target_spec.rb index fa5d7e6508..f7543c05d5 100644 --- a/Library/Homebrew/test/cask/artifact/alt_target_spec.rb +++ b/Library/Homebrew/test/cask/artifact/alt_target_spec.rb @@ -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(: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") } diff --git a/Library/Homebrew/test/cask/artifact/app_spec.rb b/Library/Homebrew/test/cask/artifact/app_spec.rb index 5ecaaef22e..e61fefaa0b 100644 --- a/Library/Homebrew/test/cask/artifact/app_spec.rb +++ b/Library/Homebrew/test/cask/artifact/app_spec.rb @@ -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(:command) { Hbc::SystemCommand } 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(:target_path) { Hbc.appdir.join("Caffeine.app") } diff --git a/Library/Homebrew/test/cask/artifact/binary_spec.rb b/Library/Homebrew/test/cask/artifact/binary_spec.rb index 08b0af1985..bf11ba2379 100644 --- a/Library/Homebrew/test/cask/artifact/binary_spec.rb +++ b/Library/Homebrew/test/cask/artifact/binary_spec.rb @@ -4,6 +4,7 @@ describe Hbc::Artifact::Binary, :cask do InstallHelper.install_without_artifacts(cask) end } + let(:artifacts) { cask.artifacts.select { |a| a.is_a?(described_class) } } let(:expected_path) { Hbc.binarydir.join("binary") } before(:each) do @@ -26,8 +27,9 @@ describe Hbc::Artifact::Binary, :cask do end it "links the binary to the proper directory" do - cask.artifacts.select { |a| a.is_a?(described_class) } - .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.readlink).to exist @@ -46,8 +48,9 @@ describe Hbc::Artifact::Binary, :cask do expect(FileUtils).to receive(:chmod) .with("+x", cask.staged_path.join("naked_non_executable")).and_call_original - cask.artifacts.select { |a| a.is_a?(described_class) } - .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.readlink).to be_executable @@ -58,8 +61,9 @@ describe Hbc::Artifact::Binary, :cask do FileUtils.touch expected_path expect { - cask.artifacts.select { |a| a.is_a?(described_class) } - .each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } + artifacts.each do |artifact| + artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) + end }.to raise_error(Hbc::CaskError) expect(expected_path).not_to be :symlink? @@ -68,8 +72,9 @@ describe Hbc::Artifact::Binary, :cask do it "clobbers an existing symlink" do expected_path.make_symlink("/tmp") - cask.artifacts.select { |a| a.is_a?(described_class) } - .each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } + artifacts.each do |artifact| + artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) + end expect(File.readlink(expected_path)).not_to eq("/tmp") end @@ -77,8 +82,9 @@ describe Hbc::Artifact::Binary, :cask do it "creates parent directory if it doesn't exist" do FileUtils.rmdir Hbc.binarydir - cask.artifacts.select { |a| a.is_a?(described_class) } - .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.exist?).to be true end @@ -91,10 +97,12 @@ describe Hbc::Artifact::Binary, :cask do } it "links the binary to the proper directory" do - cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::App) } - .each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } - cask.artifacts.select { |a| a.is_a?(described_class) } - .each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } + cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::App) }.each do |artifact| + artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) + end + artifacts.each do |artifact| + artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) + end expect(expected_path).to be_a_symlink expect(expected_path.readlink).to exist diff --git a/Library/Homebrew/test/cask/artifact/generic_artifact_spec.rb b/Library/Homebrew/test/cask/artifact/generic_artifact_spec.rb index 8dc30439dd..1bf3bbbdb7 100644 --- a/Library/Homebrew/test/cask/artifact/generic_artifact_spec.rb +++ b/Library/Homebrew/test/cask/artifact/generic_artifact_spec.rb @@ -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(: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") } diff --git a/Library/Homebrew/test/cask/artifact/nested_container_spec.rb b/Library/Homebrew/test/cask/artifact/nested_container_spec.rb index 65759b5b72..d7bd6f59ae 100644 --- a/Library/Homebrew/test/cask/artifact/nested_container_spec.rb +++ b/Library/Homebrew/test/cask/artifact/nested_container_spec.rb @@ -5,8 +5,9 @@ describe Hbc::Artifact::NestedContainer, :cask do InstallHelper.install_without_artifacts(c) end - cask.artifacts.select { |a| a.is_a?(described_class) } - .each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } + cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact| + artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) + end expect(cask.staged_path.join("MyNestedApp.app")).to be_a_directory end diff --git a/Library/Homebrew/test/cask/artifact/pkg_spec.rb b/Library/Homebrew/test/cask/artifact/pkg_spec.rb index d3028be9cb..8ea5a1bb30 100644 --- a/Library/Homebrew/test/cask/artifact/pkg_spec.rb +++ b/Library/Homebrew/test/cask/artifact/pkg_spec.rb @@ -8,7 +8,7 @@ describe Hbc::Artifact::Pkg, :cask do describe "install_phase" 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( "/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") } 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")) diff --git a/Library/Homebrew/test/cask/artifact/postflight_block_spec.rb b/Library/Homebrew/test/cask/artifact/postflight_block_spec.rb index acd63671aa..18cc4ca91a 100644 --- a/Library/Homebrew/test/cask/artifact/postflight_block_spec.rb +++ b/Library/Homebrew/test/cask/artifact/postflight_block_spec.rb @@ -11,8 +11,9 @@ describe Hbc::Artifact::PostflightBlock, :cask do end end - cask.artifacts.select { |a| a.is_a?(described_class) } - .each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } + cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact| + artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) + end expect(called).to be true expect(yielded_arg).to be_kind_of(Hbc::DSL::Postflight) @@ -31,8 +32,9 @@ describe Hbc::Artifact::PostflightBlock, :cask do end end - cask.artifacts.select { |a| a.is_a?(described_class) } - .each { |artifact| artifact.uninstall_phase(command: Hbc::NeverSudoSystemCommand, force: false) } + cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact| + artifact.uninstall_phase(command: Hbc::NeverSudoSystemCommand, force: false) + end expect(called).to be true expect(yielded_arg).to be_kind_of(Hbc::DSL::UninstallPostflight) diff --git a/Library/Homebrew/test/cask/artifact/preflight_block_spec.rb b/Library/Homebrew/test/cask/artifact/preflight_block_spec.rb index 285a93ffba..405cdbd6f8 100644 --- a/Library/Homebrew/test/cask/artifact/preflight_block_spec.rb +++ b/Library/Homebrew/test/cask/artifact/preflight_block_spec.rb @@ -11,8 +11,9 @@ describe Hbc::Artifact::PreflightBlock, :cask do end end - cask.artifacts.select { |a| a.is_a?(described_class) } - .each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } + cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact| + artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) + end expect(called).to be true expect(yielded_arg).to be_kind_of Hbc::DSL::Preflight @@ -31,8 +32,9 @@ describe Hbc::Artifact::PreflightBlock, :cask do end end - cask.artifacts.select { |a| a.is_a?(described_class) } - .each { |artifact| artifact.uninstall_phase(command: Hbc::NeverSudoSystemCommand, force: false) } + cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact| + artifact.uninstall_phase(command: Hbc::NeverSudoSystemCommand, force: false) + end expect(called).to be true expect(yielded_arg).to be_kind_of Hbc::DSL::UninstallPreflight diff --git a/Library/Homebrew/test/cask/artifact/suite_spec.rb b/Library/Homebrew/test/cask/artifact/suite_spec.rb index 1826dffbbb..01d25c4cc2 100644 --- a/Library/Homebrew/test/cask/artifact/suite_spec.rb +++ b/Library/Homebrew/test/cask/artifact/suite_spec.rb @@ -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(: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") } diff --git a/Library/Homebrew/test/cask/artifact/two_apps_correct_spec.rb b/Library/Homebrew/test/cask/artifact/two_apps_correct_spec.rb index ca8c44903e..84072fd1a7 100644 --- a/Library/Homebrew/test/cask/artifact/two_apps_correct_spec.rb +++ b/Library/Homebrew/test/cask/artifact/two_apps_correct_spec.rb @@ -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(: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") } diff --git a/Library/Homebrew/test/cask/artifact/uninstall_no_zap_spec.rb b/Library/Homebrew/test/cask/artifact/uninstall_no_zap_spec.rb index da0bfbb787..a91e934050 100644 --- a/Library/Homebrew/test/cask/artifact/uninstall_no_zap_spec.rb +++ b/Library/Homebrew/test/cask/artifact/uninstall_no_zap_spec.rb @@ -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(:zap_artifact) { - cask.artifacts.select { |a| a.is_a?(described_class) }.first + cask.artifacts.find { |a| a.is_a?(described_class) } } before(:each) do diff --git a/Library/Homebrew/test/cask/artifact/uninstall_zap_shared_examples.rb b/Library/Homebrew/test/cask/artifact/uninstall_zap_shared_examples.rb index f1eb1b913e..5e302d35a1 100644 --- a/Library/Homebrew/test/cask/artifact/uninstall_zap_shared_examples.rb +++ b/Library/Homebrew/test/cask/artifact/uninstall_zap_shared_examples.rb @@ -1,6 +1,6 @@ shared_examples "#uninstall_phase or #zap_phase" do 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 } subject { artifact.public_send(:"#{artifact_dsl_key}_phase", command: fake_system_command) } diff --git a/Library/Homebrew/test/cask/cli/list_spec.rb b/Library/Homebrew/test/cask/cli/list_spec.rb index 4f59aafe92..6a92289aaf 100644 --- a/Library/Homebrew/test/cask/cli/list_spec.rb +++ b/Library/Homebrew/test/cask/cli/list_spec.rb @@ -72,8 +72,9 @@ describe Hbc::CLI::List, :cask do it "lists the installed files for those Casks" do casks.each(&InstallHelper.method(:install_without_artifacts_with_caskfile)) - transmission.artifacts.select { |a| a.is_a?(Hbc::Artifact::App) } - .each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } + transmission.artifacts.select { |a| a.is_a?(Hbc::Artifact::App) }.each do |artifact| + artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) + end expect { described_class.run("local-transmission", "local-caffeine") diff --git a/Library/Homebrew/test/cask/dsl_spec.rb b/Library/Homebrew/test/cask/dsl_spec.rb index 390e56b186..e4149398e9 100644 --- a/Library/Homebrew/test/cask/dsl_spec.rb +++ b/Library/Homebrew/test/cask/dsl_spec.rb @@ -216,12 +216,12 @@ describe Hbc::DSL, :cask do app "Bar.app" 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 it "allow app stanzas to be empty" do 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 @@ -249,7 +249,7 @@ describe Hbc::DSL, :cask do pkg "Bar.pkg" 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 @@ -501,10 +501,10 @@ describe Hbc::DSL, :cask do let(:token) { "with-installer-script" } 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.select { |a| a.is_a?(Hbc::Artifact::Installer) }.first.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.select { |a| a.is_a?(Hbc::Artifact::Installer) }.to_a[1].args[:args]).to eq(["--flag"]) + expect(cask.artifacts.to_a[0].path).to eq(Pathname("/usr/bin/true")) + expect(cask.artifacts.to_a[0].args[:args]).to eq(["--flag"]) + expect(cask.artifacts.to_a[1].path).to eq(Pathname("/usr/bin/false")) + expect(cask.artifacts.to_a[1].args[:args]).to eq(["--flag"]) end end @@ -512,7 +512,7 @@ describe Hbc::DSL, :cask do let(:token) { "with-installer-manual" } 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.path).to eq(cask.staged_path.join("Caffeine.app")) end @@ -524,7 +524,7 @@ describe Hbc::DSL, :cask do let(:token) { "stage-only" } 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 @@ -550,7 +550,7 @@ describe Hbc::DSL, :cask do let(:token) { "appdir-interpolation" } 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 @@ -563,7 +563,7 @@ describe Hbc::DSL, :cask do binary "#{appdir}/some/path" 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 Hbc.appdir = original_appdir end