Merge pull request #2258 from reitermarkus/cask-spec-helper

Update Cask `spec_helper`.
This commit is contained in:
Markus Reiter 2017-03-05 05:12:24 +01:00 committed by GitHub
commit 8772b021a2
7 changed files with 104 additions and 107 deletions

View File

@ -7,19 +7,32 @@ describe Hbc::Artifact::Uninstall do
Hbc::Artifact::Uninstall.new(cask, command: Hbc::FakeSystemCommand) Hbc::Artifact::Uninstall.new(cask, command: Hbc::FakeSystemCommand)
} }
let(:absolute_path) { Pathname.new("#{TEST_TMPDIR}/absolute_path") } let(:dir) { TEST_TMPDIR }
let(:path_with_tilde) { Pathname.new("#{TEST_TMPDIR}/path_with_tilde") } let(:absolute_path) { Pathname.new("#{dir}/absolute_path") }
let(:glob_path1) { Pathname.new("#{TEST_TMPDIR}/glob_path1") } let(:path_with_tilde) { Pathname.new("#{dir}/path_with_tilde") }
let(:glob_path2) { Pathname.new("#{TEST_TMPDIR}/glob_path2") } let(:glob_path1) { Pathname.new("#{dir}/glob_path1") }
let(:glob_path2) { Pathname.new("#{dir}/glob_path2") }
before(:each) do around(:each) do |example|
FileUtils.touch(absolute_path) begin
FileUtils.touch(path_with_tilde) ENV["HOME"] = dir
FileUtils.touch(glob_path1)
FileUtils.touch(glob_path2) paths = [
ENV["HOME"] = TEST_TMPDIR absolute_path,
shutup do path_with_tilde,
InstallHelper.install_without_artifacts(cask) glob_path1,
glob_path2,
]
FileUtils.touch paths
shutup do
InstallHelper.install_without_artifacts(cask)
end
example.run
ensure
FileUtils.rm_f paths
end end
end end

View File

@ -8,19 +8,32 @@ describe Hbc::Artifact::Zap do
Hbc::Artifact::Zap.new(cask, command: Hbc::FakeSystemCommand) Hbc::Artifact::Zap.new(cask, command: Hbc::FakeSystemCommand)
} }
let(:absolute_path) { Pathname.new("#{TEST_TMPDIR}/absolute_path") } let(:dir) { TEST_TMPDIR }
let(:path_with_tilde) { Pathname.new("#{TEST_TMPDIR}/path_with_tilde") } let(:absolute_path) { Pathname.new("#{dir}/absolute_path") }
let(:glob_path1) { Pathname.new("#{TEST_TMPDIR}/glob_path1") } let(:path_with_tilde) { Pathname.new("#{dir}/path_with_tilde") }
let(:glob_path2) { Pathname.new("#{TEST_TMPDIR}/glob_path2") } let(:glob_path1) { Pathname.new("#{dir}/glob_path1") }
let(:glob_path2) { Pathname.new("#{dir}/glob_path2") }
before(:each) do around(:each) do |example|
FileUtils.touch(absolute_path) begin
FileUtils.touch(path_with_tilde) ENV["HOME"] = dir
FileUtils.touch(glob_path1)
FileUtils.touch(glob_path2) paths = [
ENV["HOME"] = TEST_TMPDIR absolute_path,
shutup do path_with_tilde,
InstallHelper.install_without_artifacts(cask) glob_path1,
glob_path2,
]
FileUtils.touch paths
shutup do
InstallHelper.install_without_artifacts(cask)
end
example.run
ensure
FileUtils.rm_f paths
end end
end end

View File

@ -1,5 +1,39 @@
describe Hbc::Audit do describe Hbc::Audit do
include AuditMatchers def include_msg?(messages, msg)
if msg.is_a?(Regexp)
Array(messages).any? { |m| m =~ msg }
else
Array(messages).include?(msg)
end
end
matcher :pass do
match do |audit|
!audit.errors? && !audit.warnings?
end
end
matcher :fail do
match(&:errors?)
end
matcher :warn do
match do |audit|
audit.warnings? && !audit.errors?
end
end
matcher :fail_with do |error_msg|
match do |audit|
include_msg?(audit.errors, error_msg)
end
end
matcher :warn_with do |warning_msg|
match do |audit|
include_msg?(audit.warnings, warning_msg)
end
end
let(:cask) { instance_double(Hbc::Cask) } let(:cask) { instance_double(Hbc::Cask) }
let(:download) { false } let(:download) { false }

View File

@ -1,7 +1,12 @@
describe Hbc::DSL::Version do describe Hbc::DSL::Version do
include ExpectationsHashHelper shared_examples "expectations hash" do |input_name, expectations|
expectations.each do |input_value, expected_output|
let(:version) { described_class.new(raw_version) } context "when #{input_name} is #{input_value.inspect}" do
let(input_name.to_sym) { input_value }
it { is_expected.to eq expected_output }
end
end
end
shared_examples "version equality" do shared_examples "version equality" do
let(:raw_version) { "1.2.3" } let(:raw_version) { "1.2.3" }
@ -36,6 +41,8 @@ describe Hbc::DSL::Version do
end end
end end
let(:version) { described_class.new(raw_version) }
describe "#==" do describe "#==" do
subject { version == other } subject { version == other }
include_examples "version equality" include_examples "version equality"

View File

@ -1,32 +1,13 @@
require "rspec/its"
require "rspec/wait"
if ENV["HOMEBREW_TESTS_COVERAGE"]
require "simplecov"
end
# add Homebrew to load path
$LOAD_PATH.unshift(File.expand_path("#{ENV["HOMEBREW_REPOSITORY"]}/Library/Homebrew")) $LOAD_PATH.unshift(File.expand_path("#{ENV["HOMEBREW_REPOSITORY"]}/Library/Homebrew"))
$LOAD_PATH.unshift(File.expand_path("#{ENV["HOMEBREW_REPOSITORY"]}/Library/Homebrew/test/support/lib")) require "test/spec_helper"
require "global"
# add Homebrew-Cask to load path # add Homebrew-Cask to load path
$LOAD_PATH.push(HOMEBREW_LIBRARY_PATH.join("cask", "lib").to_s) $LOAD_PATH.push(HOMEBREW_LIBRARY_PATH.join("cask", "lib").to_s)
require "test/support/helper/shutup"
Pathname.glob(HOMEBREW_LIBRARY_PATH.join("cask", "spec", "support", "**", "*.rb")).each(&method(:require)) Pathname.glob(HOMEBREW_LIBRARY_PATH.join("cask", "spec", "support", "**", "*.rb")).each(&method(:require))
require "hbc" require "hbc"
# create and override default directories
Hbc.default_tap = Tap.fetch("caskroom", "spec").tap do |tap|
# link test casks
FileUtils.mkdir_p tap.path.dirname
FileUtils.ln_s TEST_FIXTURE_DIR.join("cask"), tap.path
end
HOMEBREW_CASK_DIRS = [ HOMEBREW_CASK_DIRS = [
:appdir, :appdir,
:caskroom, :caskroom,
@ -38,26 +19,24 @@ HOMEBREW_CASK_DIRS = [
].freeze ].freeze
RSpec.configure do |config| RSpec.configure do |config|
config.order = :random
config.include(Test::Helper::Shutup)
config.around(:each) do |example| config.around(:each) do |example|
begin begin
@__dirs = HOMEBREW_CASK_DIRS.map { |dir| dirs = HOMEBREW_CASK_DIRS.map { |dir|
Pathname.new(TEST_TMPDIR).join(dir.to_s).tap { |path| Pathname.new(TEST_TMPDIR).join("cask-#{dir}").tap { |path|
path.mkpath path.mkpath
Hbc.public_send("#{dir}=", path) Hbc.public_send("#{dir}=", path)
} }
} }
@__argv = ARGV.dup Hbc.default_tap = Tap.fetch("caskroom", "spec").tap do |tap|
@__env = ENV.to_hash # dup doesn't work on ENV # link test casks
FileUtils.mkdir_p tap.path.dirname
FileUtils.ln_sf TEST_FIXTURE_DIR.join("cask"), tap.path
end
example.run example.run
ensure ensure
ARGV.replace(@__argv) FileUtils.rm_rf dirs
ENV.replace(@__env)
FileUtils.rm_rf @__dirs.map(&:children)
end end
end end
end end

View File

@ -1,39 +0,0 @@
module AuditMatchers
extend RSpec::Matchers::DSL
matcher :pass do
match do |audit|
!audit.errors? && !audit.warnings?
end
end
matcher :fail do
match(&:errors?)
end
matcher :warn do
match do |audit|
audit.warnings? && !audit.errors?
end
end
matcher :fail_with do |error_msg|
match do |audit|
include_msg?(audit.errors, error_msg)
end
end
matcher :warn_with do |warning_msg|
match do |audit|
include_msg?(audit.warnings, warning_msg)
end
end
def include_msg?(messages, msg)
if msg.is_a?(Regexp)
Array(messages).any? { |m| m =~ msg }
else
Array(messages).include?(msg)
end
end
end

View File

@ -1,10 +0,0 @@
module ExpectationsHashHelper
shared_examples "expectations hash" do |input_name, expectations|
expectations.each do |input_value, expected_output|
context "when #{input_name} is #{input_value.inspect}" do
let(input_name.to_sym) { input_value }
it { is_expected.to eq expected_output }
end
end
end
end