From b9c9f0f6872fc3e83e4987559ee3b3c5fa2509a5 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 5 Mar 2017 05:30:45 +0100 Subject: Move Cask test helpers to `test/support`. --- Library/Homebrew/cask/spec/spec_helper.rb | 6 +- .../cask/spec/support/fake_system_command.rb | 77 ---------------------- .../Homebrew/cask/spec/support/install_helper.rb | 42 ------------ .../cask/spec/support/never_sudo_system_command.rb | 9 --- .../support/helper/cask/fake_system_command.rb | 77 ++++++++++++++++++++++ .../test/support/helper/cask/install_helper.rb | 42 ++++++++++++ .../helper/cask/never_sudo_system_command.rb | 9 +++ 7 files changed, 132 insertions(+), 130 deletions(-) delete mode 100644 Library/Homebrew/cask/spec/support/fake_system_command.rb delete mode 100644 Library/Homebrew/cask/spec/support/install_helper.rb delete mode 100644 Library/Homebrew/cask/spec/support/never_sudo_system_command.rb create mode 100644 Library/Homebrew/test/support/helper/cask/fake_system_command.rb create mode 100644 Library/Homebrew/test/support/helper/cask/install_helper.rb create mode 100644 Library/Homebrew/test/support/helper/cask/never_sudo_system_command.rb (limited to 'Library') diff --git a/Library/Homebrew/cask/spec/spec_helper.rb b/Library/Homebrew/cask/spec/spec_helper.rb index ca0b69d33..0b3a120ca 100644 --- a/Library/Homebrew/cask/spec/spec_helper.rb +++ b/Library/Homebrew/cask/spec/spec_helper.rb @@ -4,10 +4,12 @@ require "test/spec_helper" # add Homebrew-Cask to load path $LOAD_PATH.push(HOMEBREW_LIBRARY_PATH.join("cask", "lib").to_s) -Pathname.glob(HOMEBREW_LIBRARY_PATH.join("cask", "spec", "support", "**", "*.rb")).each(&method(:require)) - require "hbc" +require "test/support/helper/cask/fake_system_command" +require "test/support/helper/cask/install_helper" +require "test/support/helper/cask/never_sudo_system_command" + HOMEBREW_CASK_DIRS = [ :appdir, :caskroom, diff --git a/Library/Homebrew/cask/spec/support/fake_system_command.rb b/Library/Homebrew/cask/spec/support/fake_system_command.rb deleted file mode 100644 index b9390d482..000000000 --- a/Library/Homebrew/cask/spec/support/fake_system_command.rb +++ /dev/null @@ -1,77 +0,0 @@ -def sudo(*args) - %w[/usr/bin/sudo -E --] + args.flatten -end - -module Hbc - class FakeSystemCommand - def self.responses - @responses ||= {} - end - - def self.expectations - @expectations ||= {} - end - - def self.system_calls - @system_calls ||= Hash.new(0) - end - - def self.clear - @responses = nil - @expectations = nil - @system_calls = nil - end - - def self.stubs_command(command, response = "") - responses[command] = response - end - - def self.expects_command(command, response = "", times = 1) - stubs_command(command, response) - expectations[command] = times - end - - def self.expect_and_pass_through(command, times = 1) - pass_through = ->(cmd, opts) { Hbc::SystemCommand.run(cmd, opts) } - expects_command(command, pass_through, times) - end - - def self.verify_expectations! - expectations.each do |command, times| - unless system_calls[command] == times - raise("expected #{command.inspect} to be run #{times} times, but got #{system_calls[command]}") - end - end - end - - def self.run(command_string, options = {}) - command = Hbc::SystemCommand.new(command_string, options).command - puts command - unless responses.key?(command) - raise("no response faked for #{command.inspect}, faked responses are: #{responses.inspect}") - end - system_calls[command] += 1 - - response = responses[command] - if response.respond_to?(:call) - response.call(command_string, options) - else - Hbc::SystemCommand::Result.new(command, response, "", 0) - end - end - - def self.run!(command, options = {}) - run(command, options.merge(must_succeed: true)) - end - end -end - -RSpec.configure do |config| - config.after(:each) do - begin - Hbc::FakeSystemCommand.verify_expectations! - ensure - Hbc::FakeSystemCommand.clear - end - end -end diff --git a/Library/Homebrew/cask/spec/support/install_helper.rb b/Library/Homebrew/cask/spec/support/install_helper.rb deleted file mode 100644 index d91b9ea57..000000000 --- a/Library/Homebrew/cask/spec/support/install_helper.rb +++ /dev/null @@ -1,42 +0,0 @@ -module InstallHelper - module_function - - require "test/support/helper/shutup" - extend Test::Helper::Shutup - - def self.install_without_artifacts(cask) - Hbc::Installer.new(cask).tap do |i| - shutup do - i.download - i.extract_primary_container - end - end - end - - def self.install_without_artifacts_with_caskfile(cask) - Hbc::Installer.new(cask).tap do |i| - shutup do - i.download - i.extract_primary_container - i.save_caskfile - end - end - end - - def install_without_artifacts(cask) - Hbc::Installer.new(cask).tap do |i| - shutup do - i.download - i.extract_primary_container - end - end - end - - def install_with_caskfile(cask) - Hbc::Installer.new(cask).tap do |i| - shutup do - i.save_caskfile - end - end - end -end diff --git a/Library/Homebrew/cask/spec/support/never_sudo_system_command.rb b/Library/Homebrew/cask/spec/support/never_sudo_system_command.rb deleted file mode 100644 index eb8b677f2..000000000 --- a/Library/Homebrew/cask/spec/support/never_sudo_system_command.rb +++ /dev/null @@ -1,9 +0,0 @@ -require "hbc/system_command" - -module Hbc - class NeverSudoSystemCommand < SystemCommand - def self.run(command, options = {}) - super(command, options.merge(sudo: false)) - end - end -end diff --git a/Library/Homebrew/test/support/helper/cask/fake_system_command.rb b/Library/Homebrew/test/support/helper/cask/fake_system_command.rb new file mode 100644 index 000000000..b9390d482 --- /dev/null +++ b/Library/Homebrew/test/support/helper/cask/fake_system_command.rb @@ -0,0 +1,77 @@ +def sudo(*args) + %w[/usr/bin/sudo -E --] + args.flatten +end + +module Hbc + class FakeSystemCommand + def self.responses + @responses ||= {} + end + + def self.expectations + @expectations ||= {} + end + + def self.system_calls + @system_calls ||= Hash.new(0) + end + + def self.clear + @responses = nil + @expectations = nil + @system_calls = nil + end + + def self.stubs_command(command, response = "") + responses[command] = response + end + + def self.expects_command(command, response = "", times = 1) + stubs_command(command, response) + expectations[command] = times + end + + def self.expect_and_pass_through(command, times = 1) + pass_through = ->(cmd, opts) { Hbc::SystemCommand.run(cmd, opts) } + expects_command(command, pass_through, times) + end + + def self.verify_expectations! + expectations.each do |command, times| + unless system_calls[command] == times + raise("expected #{command.inspect} to be run #{times} times, but got #{system_calls[command]}") + end + end + end + + def self.run(command_string, options = {}) + command = Hbc::SystemCommand.new(command_string, options).command + puts command + unless responses.key?(command) + raise("no response faked for #{command.inspect}, faked responses are: #{responses.inspect}") + end + system_calls[command] += 1 + + response = responses[command] + if response.respond_to?(:call) + response.call(command_string, options) + else + Hbc::SystemCommand::Result.new(command, response, "", 0) + end + end + + def self.run!(command, options = {}) + run(command, options.merge(must_succeed: true)) + end + end +end + +RSpec.configure do |config| + config.after(:each) do + begin + Hbc::FakeSystemCommand.verify_expectations! + ensure + Hbc::FakeSystemCommand.clear + end + end +end diff --git a/Library/Homebrew/test/support/helper/cask/install_helper.rb b/Library/Homebrew/test/support/helper/cask/install_helper.rb new file mode 100644 index 000000000..d91b9ea57 --- /dev/null +++ b/Library/Homebrew/test/support/helper/cask/install_helper.rb @@ -0,0 +1,42 @@ +module InstallHelper + module_function + + require "test/support/helper/shutup" + extend Test::Helper::Shutup + + def self.install_without_artifacts(cask) + Hbc::Installer.new(cask).tap do |i| + shutup do + i.download + i.extract_primary_container + end + end + end + + def self.install_without_artifacts_with_caskfile(cask) + Hbc::Installer.new(cask).tap do |i| + shutup do + i.download + i.extract_primary_container + i.save_caskfile + end + end + end + + def install_without_artifacts(cask) + Hbc::Installer.new(cask).tap do |i| + shutup do + i.download + i.extract_primary_container + end + end + end + + def install_with_caskfile(cask) + Hbc::Installer.new(cask).tap do |i| + shutup do + i.save_caskfile + end + end + end +end diff --git a/Library/Homebrew/test/support/helper/cask/never_sudo_system_command.rb b/Library/Homebrew/test/support/helper/cask/never_sudo_system_command.rb new file mode 100644 index 000000000..eb8b677f2 --- /dev/null +++ b/Library/Homebrew/test/support/helper/cask/never_sudo_system_command.rb @@ -0,0 +1,9 @@ +require "hbc/system_command" + +module Hbc + class NeverSudoSystemCommand < SystemCommand + def self.run(command, options = {}) + super(command, options.merge(sudo: false)) + end + end +end -- cgit v1.2.3