From e7c943b561cc9c0bfc1d0923783e5dbf729bc076 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 9 Feb 2017 03:57:53 +0100 Subject: [PATCH] Convert postflight test to spec. --- .../cask/dsl/postflight_spec.rb} | 2 +- .../support/shared_examples/staged.rb | 90 +++++++++++++------ Library/Homebrew/cask/test/test_helper.rb | 1 - 3 files changed, 63 insertions(+), 30 deletions(-) rename Library/Homebrew/cask/{test/cask/dsl/postflight_test.rb => spec/cask/dsl/postflight_spec.rb} (93%) rename Library/Homebrew/cask/{test => spec}/support/shared_examples/staged.rb (50%) diff --git a/Library/Homebrew/cask/test/cask/dsl/postflight_test.rb b/Library/Homebrew/cask/spec/cask/dsl/postflight_spec.rb similarity index 93% rename from Library/Homebrew/cask/test/cask/dsl/postflight_test.rb rename to Library/Homebrew/cask/spec/cask/dsl/postflight_spec.rb index b5b38102db..116a7c8a80 100644 --- a/Library/Homebrew/cask/test/cask/dsl/postflight_test.rb +++ b/Library/Homebrew/cask/spec/cask/dsl/postflight_spec.rb @@ -1,4 +1,4 @@ -require "test_helper" +require "spec_helper" describe Hbc::DSL::Postflight do let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/basic-cask.rb") } diff --git a/Library/Homebrew/cask/test/support/shared_examples/staged.rb b/Library/Homebrew/cask/spec/support/shared_examples/staged.rb similarity index 50% rename from Library/Homebrew/cask/test/support/shared_examples/staged.rb rename to Library/Homebrew/cask/spec/support/shared_examples/staged.rb index 95e75726e8..f791696aca 100644 --- a/Library/Homebrew/cask/test/support/shared_examples/staged.rb +++ b/Library/Homebrew/cask/spec/support/shared_examples/staged.rb @@ -1,15 +1,19 @@ -require "test_helper" +require "spec_helper" -shared_examples_for Hbc::Staged do +require "hbc/staged" + +shared_examples Hbc::Staged do let(:fake_pathname_exists) { - fake_pathname = Pathname("/path/to/file/that/exists") - fake_pathname.stubs(exist?: true, expand_path: fake_pathname) + fake_pathname = Pathname.new("/path/to/file/that/exists") + allow(fake_pathname).to receive(:exist?).and_return(true) + allow(fake_pathname).to receive(:expand_path).and_return(fake_pathname) fake_pathname } let(:fake_pathname_does_not_exist) { - fake_pathname = Pathname("/path/to/file/that/does/not/exist") - fake_pathname.stubs(exist?: false, expand_path: fake_pathname) + fake_pathname = Pathname.new("/path/to/file/that/does/not/exist") + allow(fake_pathname).to receive(:exist?).and_return(false) + allow(fake_pathname).to receive(:expand_path).and_return(fake_pathname) fake_pathname } @@ -17,93 +21,123 @@ shared_examples_for Hbc::Staged do Hbc::FakeSystemCommand.expects_command( ["echo", "homebrew-cask", "rocks!"] ) - staged.system_command("echo", args: ["homebrew-cask", "rocks!"]) + + shutup do + staged.system_command("echo", args: ["homebrew-cask", "rocks!"]) + end end it "can get the Info.plist file for the primary app" do - staged.info_plist_file.to_s.must_include Hbc.appdir.join("TestCask.app/Contents/Info.plist") + expect(staged.info_plist_file.to_s).to include Hbc.appdir.join("TestCask.app/Contents/Info.plist") end it "can execute commands on the Info.plist file" do - staged.stubs(bundle_identifier: "com.example.BasicCask") + allow(staged).to receive(:bundle_identifier).and_return("com.example.BasicCask") Hbc::FakeSystemCommand.expects_command( ["/usr/libexec/PlistBuddy", "-c", "Print CFBundleIdentifier", staged.info_plist_file] ) - staged.plist_exec("Print CFBundleIdentifier") + + shutup do + staged.plist_exec("Print CFBundleIdentifier") + end end it "can set a key in the Info.plist file" do - staged.stubs(bundle_identifier: "com.example.BasicCask") + allow(staged).to receive(:bundle_identifier).and_return("com.example.BasicCask") Hbc::FakeSystemCommand.expects_command( ["/usr/libexec/PlistBuddy", "-c", "Set :JVMOptions:JVMVersion 1.6+", staged.info_plist_file] ) - staged.plist_set(":JVMOptions:JVMVersion", "1.6+") + + shutup do + staged.plist_set(":JVMOptions:JVMVersion", "1.6+") + end end it "can set the permissions of a file" do fake_pathname = fake_pathname_exists - staged.stubs(Pathname: fake_pathname) + allow(staged).to receive(:Pathname).and_return(fake_pathname) Hbc::FakeSystemCommand.expects_command( ["/bin/chmod", "-R", "--", "777", fake_pathname] ) - staged.set_permissions(fake_pathname.to_s, "777") + + shutup do + staged.set_permissions(fake_pathname.to_s, "777") + end end it "can set the permissions of multiple files" do fake_pathname = fake_pathname_exists - staged.stubs(:Pathname).returns(fake_pathname) + allow(staged).to receive(:Pathname).and_return(fake_pathname) Hbc::FakeSystemCommand.expects_command( ["/bin/chmod", "-R", "--", "777", fake_pathname, fake_pathname] ) - staged.set_permissions([fake_pathname.to_s, fake_pathname.to_s], "777") + + shutup do + staged.set_permissions([fake_pathname.to_s, fake_pathname.to_s], "777") + end end it "cannot set the permissions of a file that does not exist" do fake_pathname = fake_pathname_does_not_exist - staged.stubs(Pathname: fake_pathname) + allow(staged).to receive(:Pathname).and_return(fake_pathname) staged.set_permissions(fake_pathname.to_s, "777") end it "can set the ownership of a file" do - staged.stubs(current_user: "fake_user") fake_pathname = fake_pathname_exists - staged.stubs(Pathname: fake_pathname) + + allow(staged).to receive(:current_user).and_return("fake_user") + allow(staged).to receive(:Pathname).and_return(fake_pathname) Hbc::FakeSystemCommand.expects_command( ["/usr/bin/sudo", "-E", "--", "/usr/sbin/chown", "-R", "--", "fake_user:staff", fake_pathname] ) - staged.set_ownership(fake_pathname.to_s) + + shutup do + staged.set_ownership(fake_pathname.to_s) + end end it "can set the ownership of multiple files" do - staged.stubs(current_user: "fake_user") fake_pathname = fake_pathname_exists - staged.stubs(Pathname: fake_pathname) + + allow(staged).to receive(:current_user).and_return("fake_user") + allow(staged).to receive(:Pathname).and_return(fake_pathname) Hbc::FakeSystemCommand.expects_command( ["/usr/bin/sudo", "-E", "--", "/usr/sbin/chown", "-R", "--", "fake_user:staff", fake_pathname, fake_pathname] ) - staged.set_ownership([fake_pathname.to_s, fake_pathname.to_s]) + + shutup do + staged.set_ownership([fake_pathname.to_s, fake_pathname.to_s]) + end end it "can set the ownership of a file with a different user and group" do fake_pathname = fake_pathname_exists - staged.stubs(Pathname: fake_pathname) + + allow(staged).to receive(:Pathname).and_return(fake_pathname) Hbc::FakeSystemCommand.expects_command( ["/usr/bin/sudo", "-E", "--", "/usr/sbin/chown", "-R", "--", "other_user:other_group", fake_pathname] ) - staged.set_ownership(fake_pathname.to_s, user: "other_user", group: "other_group") + + shutup do + staged.set_ownership(fake_pathname.to_s, user: "other_user", group: "other_group") + end end it "cannot set the ownership of a file that does not exist" do - staged.stubs(current_user: "fake_user") + allow(staged).to receive(:current_user).and_return("fake_user") fake_pathname = fake_pathname_does_not_exist - staged.stubs(Pathname: fake_pathname) - staged.set_ownership(fake_pathname.to_s) + allow(staged).to receive(:Pathname).and_return(fake_pathname) + + shutup do + staged.set_ownership(fake_pathname.to_s) + end end end diff --git a/Library/Homebrew/cask/test/test_helper.rb b/Library/Homebrew/cask/test/test_helper.rb index 57b468d0f8..a4bb9af0f8 100644 --- a/Library/Homebrew/cask/test/test_helper.rb +++ b/Library/Homebrew/cask/test/test_helper.rb @@ -93,7 +93,6 @@ end # Extend MiniTest API with support for RSpec-style shared examples require "support/shared_examples" -require "support/shared_examples/staged.rb" require "support/fake_dirs" require "support/fake_system_command"