diff options
| author | Markus Reiter | 2017-03-05 05:30:45 +0100 |
|---|---|---|
| committer | Markus Reiter | 2017-03-05 19:13:06 +0100 |
| commit | b9c9f0f6872fc3e83e4987559ee3b3c5fa2509a5 (patch) | |
| tree | 84afa4f0d54e6841632a52dcd216bc02ccd0be0f /Library/Homebrew/test | |
| parent | 5fbccc03a7d496e75e83742d0c7e606a4db131ab (diff) | |
| download | brew-b9c9f0f6872fc3e83e4987559ee3b3c5fa2509a5.tar.bz2 | |
Move Cask test helpers to `test/support`.
Diffstat (limited to 'Library/Homebrew/test')
3 files changed, 128 insertions, 0 deletions
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 |
