Browse Source

halfway through ripping out mocha

phinze 12 years ago
parent
commit
3ad61a5d56
  1. 1
      Gemfile
  2. 4
      Gemfile.lock
  3. 8
      lib/cask/audit.rb
  4. 2
      lib/cask/dsl.rb
  5. 5
      lib/cask/fetcher.rb
  6. 44
      test/cask/audit_test.rb
  7. 15
      test/cli/list_test.rb
  8. 11
      test/cli/search_test.rb
  9. 26
      test/cli/uninstall_test.rb
  10. 21
      test/support/fake_fetcher.rb
  11. 33
      test/test_helper.rb

1
Gemfile

@ -3,5 +3,4 @@ source :rubygems
group :test do
gem 'rake'
gem 'purdytest'
gem 'mocha'
end

4
Gemfile.lock

@ -1,10 +1,7 @@
GEM
remote: http://rubygems.org/
specs:
metaclass (0.0.1)
minitest (2.12.1)
mocha (0.12.7)
metaclass (~> 0.0.1)
purdytest (1.0.0)
minitest (~> 2.2)
rake (10.0.1)
@ -13,6 +10,5 @@ PLATFORMS
ruby
DEPENDENCIES
mocha
purdytest
rake

8
lib/cask/audit.rb

@ -1,11 +1,12 @@
class Cask::Audit
attr_reader :cask, :errors, :warnings, :headers, :response_status
def initialize(cask)
def initialize(cask, fetcher=Cask::Fetcher)
@cask = cask
@errors = []
@warnings = []
@headers = {}
@fetcher = fetcher
end
def run!
@ -94,7 +95,7 @@ class Cask::Audit
end
def _get_data_from_request
response = _curl(cask.url)
response = @fetcher.head(cask.url)
if response.empty?
add_error "timeout while requesting #{cask.url}"
@ -122,7 +123,4 @@ class Cask::Audit
end
end
def _curl(url)
`curl --max-time 5 --silent --location --head '#{url}'`
end
end

2
lib/cask/dsl.rb

@ -21,7 +21,7 @@ module Cask::DSL
end
def url(url=nil)
@url ||= URI.parse(url)
@url ||= (url && URI.parse(url))
end
def version(version=nil)

5
lib/cask/fetcher.rb

@ -0,0 +1,5 @@
class Cask::Fetcher
def self.head(url)
`curl --max-time 5 --silent --location --head '#{url}'`
end
end

44
test/cask/audit_test.rb

@ -1,22 +1,37 @@
require 'test_helper'
class CaskMissingUrl < Cask
version '1.2.3'
homepage 'http://example.com'
end
class CaskMissingVersion < Cask
url 'http://localhost/something.dmg'
homepage 'http://example.com'
end
class CaskMissingHomepage < Cask
url 'http://localhost/something.dmg'
version '1.2.3'
end
describe Cask::Audit do
describe "result" do
it "is 'failed' if there are have been any errors added" do
audit = Cask::Audit.new(mock())
audit = Cask::Audit.new(TestHelper.test_cask)
audit.add_error 'bad'
audit.add_warning 'eh'
audit.result.must_match /failed/
end
it "is 'warning' if there are no errors, but there are warnings" do
audit = Cask::Audit.new(mock())
audit = Cask::Audit.new(TestHelper.test_cask)
audit.add_warning 'eh'
audit.result.must_match /warning/
end
it "is 'passed' if there are no errors or warning" do
audit = Cask::Audit.new(mock())
audit = Cask::Audit.new(TestHelper.test_cask)
audit.result.must_match /passed/
end
end
@ -24,19 +39,19 @@ describe Cask::Audit do
describe "run!" do
describe "required fields" do
it "adds an error if url is missing" do
audit = Cask::Audit.new(stub(:url => nil, :version => 'something', :homepage => 'something'))
audit = Cask::Audit.new(CaskMissingUrl.new)
audit.run!
audit.errors.must_include 'url is required'
end
it "adds an error if version is missing" do
audit = Cask::Audit.new(stub(:url => 'something', :version => nil, :homepage => 'something'))
audit = Cask::Audit.new(CaskMissingVersion.new)
audit.run!
audit.errors.must_include 'version is required'
end
it "adds an error if homepage is missing" do
audit = Cask::Audit.new(stub(:url => 'something', :version => 'something', :homepage => nil))
audit = Cask::Audit.new(CaskMissingHomepage.new)
audit.run!
audit.errors.must_include 'homepage is required'
end
@ -44,26 +59,23 @@ describe Cask::Audit do
describe "request processing" do
it "adds an error if response is empty" do
audit = Cask::Audit.new(stub(:url => 'something', :version => 'something', :homepage => 'something'))
audit.stubs(:_curl).returns('')
cask = TestHelper.test_cask
TestHelper.fake_response_for(cask.url, "")
audit = Cask::Audit.new(cask, TestHelper.fake_fetcher)
audit.run!
audit.errors.must_include 'timeout while requesting something'
audit.errors.must_include "timeout while requesting #{cask.url}"
end
it "properly populates the response code and headers from an http response" do
audit = Cask::Audit.new(stub(
:url => URI('http://something/file.zip'),
:version => 'something',
:homepage => 'something',
:content_length => '123'
))
audit.stubs(:_curl).returns(<<-RESPONSE.gsub(/^ /, ''))
TestHelper.fake_response_for(TestHelper.test_cask.url, <<-RESPONSE.gsub(/^ /, ''))
HTTP/1.1 200 OK
Content-Type: application/x-apple-diskimage
ETag: "b4208f3e84967be4b078ecaa03fba941"
Content-Length: 23726161
Last-Modified: Sun, 12 Aug 2012 21:17:21 GMT
RESPONSE
audit = Cask::Audit.new(TestHelper.test_cask, TestHelper.fake_fetcher)
audit.run!
audit.response_status.must_equal 'HTTP/1.1 200 OK'
audit.headers.must_equal({

15
test/cli/list_test.rb

@ -2,19 +2,20 @@ require 'test_helper'
describe Cask::CLI::List do
it "lists the installed casks in a pretty fashion" do
Cask.stubs(:installed).returns(%w[
phinze-cask/adium
phinze-cask/google-chrome
])
shutup do
Cask::CLI::Install.run('local-transmission', 'local-caffeine')
end
lambda {
Cask::CLI::List.run
}.must_output <<-OUTPUT.gsub(/^ */, '')
adium
google-chrome
local-caffeine
local-transmission
OUTPUT
end
it "lists the taps for casks that show up in two" do
it "lists the taps for casks that show up in two taps" do
skip("need to move this implementation to an easier to test location")
Cask.stubs(:installed).returns(%w[
phinze-cask/adium
phinze-cask/google-chrome

11
test/cli/search_test.rb

@ -2,16 +2,11 @@ require 'test_helper'
describe Cask::CLI::Search do
it "lists the available casks that match the search term" do
Cask.stubs(:all_titles).returns(%w[
phinze-cask/foo
phinze-cask/bar
phinze-cask/baz
])
lambda {
Cask::CLI::Search.run('ba')
Cask::CLI::Search.run('intellij')
}.must_output <<-OUTPUT.gsub(/^ */, '')
bar
baz
intellij-community
intellij-ultimate
OUTPUT
end
end

26
test/cli/uninstall_test.rb

@ -17,21 +17,21 @@ describe Cask::CLI::Uninstall do
OUTPUT
end
it "delegates to the installer to properly uninstall" do
fake_cask = stub('fake-cask')
Cask.stubs(:load).with('fake-cask').returns(fake_cask)
Cask::Installer.expects(:uninstall).with(fake_cask)
Cask::CLI::Uninstall.run('fake-cask')
end
it "can uninstall multiple casks at once" do
Cask::Installer.expects(:uninstall).with do |cask|
cask.title == 'caffeine'
end
Cask::Installer.expects(:uninstall).with do |cask|
cask.title == 'anvil'
caffeine = Cask.load('local-caffeine')
transmission = Cask.load('local-transmission')
shutup do
Cask::Installer.install caffeine
Cask::Installer.install transmission
end
Cask::CLI::Uninstall.run('caffeine', 'anvil')
caffeine.must_be :installed?
transmission.must_be :installed?
Cask::CLI::Uninstall.run('local-caffeine', 'local-transmission')
caffeine.wont_be :installed?
transmission.wont_be :installed?
end
end

21
test/support/fake_fetcher.rb

@ -0,0 +1,21 @@
class Cask::FakeFetcher
def self.fake_response_for(url, response)
@responses[url] = response
end
def self.head(url)
@responses ||= {}
unless @responses.key?(url)
fail("no response faked for #{url.inspect}")
end
@responses[url]
end
def self.init
@responses = {}
end
def self.clear
@responses = {}
end
end

33
test/test_helper.rb

@ -27,9 +27,6 @@ require 'minitest/spec'
require 'minitest/autorun'
require 'purdytest'
# sometimes you need to mock
require 'mocha'
# our baby
require 'cask'
@ -39,6 +36,36 @@ class TestHelper
path = File.join(File.dirname(__FILE__), 'support', 'binaries', name)
"file://#{path}"
end
def self.test_cask
Cask.load('test-cask')
end
def self.fake_fetcher
Cask::FakeFetcher
end
def self.fake_response_for(*args)
Cask::FakeFetcher.fake_response_for(*args)
end
end
require 'support/fake_fetcher'
module FakeFetcherHooks
def before_setup
super
Cask::FakeFetcher.init
end
def after_teardown
super
Cask::FakeFetcher.clear
end
end
class MiniTest::Spec
include FakeFetcherHooks
end

Loading…
Cancel
Save