Merge pull request #1339 from reitermarkus/test-helper

Use `test/lib/config` for tests.
This commit is contained in:
Markus Reiter 2016-10-23 13:34:41 +02:00 committed by GitHub
commit f56e410ebf
3 changed files with 24 additions and 58 deletions

View File

@ -31,11 +31,11 @@ Hbc.appdir = Pathname.new(TEST_TMPDIR).join("Applications").tap(&:mkpath)
Hbc.cache.mkpath
Hbc.caskroom.mkpath
Hbc.default_tap = Tap.fetch("caskroom", "spec").tap do |tap|
tap.path.dirname.mkpath
# link test casks
FileUtils.mkdir_p tap.path.dirname
FileUtils.ln_s Pathname.new(__FILE__).dirname.join("support"), tap.path
end
FileUtils.ln_s Pathname.new(__FILE__).dirname.join("support"), Hbc.default_tap.path
RSpec.configure do |config|
config.order = :random
config.include(Test::Helper::Env)

View File

@ -3,15 +3,17 @@ require "test_helper"
describe Hbc::UrlChecker do
describe "request processing" do
it "adds an error if response is empty" do
cask = TestHelper.test_cask
TestHelper.fake_response_for(cask.url, "")
checker = Hbc::UrlChecker.new(cask, TestHelper.fake_fetcher)
cask = Hbc.load("basic-cask")
Hbc::FakeFetcher.fake_response_for(cask.url, "")
checker = Hbc::UrlChecker.new(cask, Hbc::FakeFetcher)
checker.run
checker.errors.must_include "timeout while requesting #{cask.url}"
end
it "properly populates the response code and headers from an http response" do
TestHelper.fake_response_for(TestHelper.test_cask.url, <<-EOS.undent)
cask = Hbc.load("basic-cask")
Hbc::FakeFetcher.fake_response_for(cask.url, <<-EOS.undent)
HTTP/1.1 200 OK
Content-Type: application/x-apple-diskimage
ETag: "b4208f3e84967be4b078ecaa03fba941"
@ -19,7 +21,7 @@ describe Hbc::UrlChecker do
Last-Modified: Sun, 12 Aug 2012 21:17:21 GMT
EOS
checker = Hbc::UrlChecker.new(TestHelper.test_cask, TestHelper.fake_fetcher)
checker = Hbc::UrlChecker.new(cask, Hbc::FakeFetcher)
checker.run
checker.response_status.must_equal "HTTP/1.1 200 OK"
checker.headers.must_equal("Content-Type" => "application/x-apple-diskimage",

View File

@ -2,20 +2,16 @@ require "bundler"
require "bundler/setup"
require "pathname"
if ENV["HOMEBREW_TESTS_COVERAGE"]
require "simplecov"
end
project_root = Pathname.new(File.expand_path("../..", __FILE__))
tap_root = Pathname.new(ENV["HOMEBREW_LIBRARY"]).join("Taps", "caskroom", "homebrew-cask")
require "simplecov" if ENV["HOMEBREW_TESTS_COVERAGE"]
# 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/test/lib"))
require "global"
# add Homebrew-Cask to load path
$LOAD_PATH.push(project_root.join("lib").to_s)
$LOAD_PATH.push(HOMEBREW_LIBRARY_PATH.join("cask", "lib").to_s)
require "test/helper/env"
require "test/helper/shutup"
@ -23,12 +19,7 @@ include Test::Helper::Env
include Test::Helper::Shutup
def sudo(*args)
%w[/usr/bin/sudo -E --] + Array(args).flatten
end
TEST_TMPDIR = Dir.mktmpdir("homebrew_cask_tests")
at_exit do
FileUtils.remove_entry(TEST_TMPDIR)
%w[/usr/bin/sudo -E --] + args.flatten
end
# must be called after testing_env so at_exit hooks are in proper order
@ -46,30 +37,20 @@ Mocha::Integration::MiniTest.activate
# our baby
require "hbc"
# override Homebrew locations
Hbc.homebrew_prefix = Pathname.new(TEST_TMPDIR).join("prefix")
Hbc.homebrew_repository = Hbc.homebrew_prefix
# Override Tap::TAP_DIRECTORY to use our test Tap directory.
class Tap
send(:remove_const, :TAP_DIRECTORY)
TAP_DIRECTORY = Hbc.homebrew_prefix.join("Library", "Taps")
module Hbc
class TestCask < Cask; end
end
Hbc.default_tap = Tap.fetch("caskroom", "testcasks")
# also jack in some test Casks
FileUtils.ln_s project_root.join("test", "support"), Tap::TAP_DIRECTORY.join("caskroom").tap(&:mkpath).join("homebrew-testcasks")
Hbc.cache.mkpath
Hbc.caskroom.mkpath
Hbc.default_tap = Tap.fetch("caskroom", "test").tap do |tap|
# link test casks
FileUtils.mkdir_p tap.path.dirname
FileUtils.ln_s Pathname.new(__FILE__).dirname.join("support"), tap.path
end
# pretend that the caskroom/cask Tap is installed
FileUtils.ln_s tap_root, Tap::TAP_DIRECTORY.join("caskroom").tap(&:mkpath).join("homebrew-cask")
# create cache directory
Hbc.homebrew_cache = Pathname.new(TEST_TMPDIR).join("cache")
Hbc.cache.mkpath
# our own testy caskroom
Hbc.caskroom = Hbc.homebrew_prefix.join("TestCaskroom")
FileUtils.ln_s HOMEBREW_LIBRARY.join("Taps", "caskroom", "homebrew-cask"), Tap.fetch("caskroom", "cask").path
class TestHelper
# helpers for test Casks to reference local files easily
@ -81,18 +62,6 @@ class TestHelper
"file://" + local_binary_path(name)
end
def self.test_cask
@test_cask ||= Hbc.load("basic-cask")
end
def self.fake_fetcher
Hbc::FakeFetcher
end
def self.fake_response_for(*args)
Hbc::FakeFetcher.fake_response_for(*args)
end
def self.valid_alias?(candidate)
return false unless candidate.symlink?
candidate.readlink.exist?
@ -138,8 +107,3 @@ require "support/cleanup"
require "support/never_sudo_system_command"
require "tmpdir"
require "tempfile"
# Common superclass for test Casks for when we need to filter them out
module Hbc
class TestCask < Cask; end
end