Skip to content

Commit

Permalink
install rspec; move first few tests over
Browse files Browse the repository at this point in the history
  • Loading branch information
phinze committed Jun 29, 2014
1 parent c51d9b2 commit 58d4c74
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 95 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ group :test do
gem 'rake'
gem 'minitest', '5.3'
gem 'minitest-colorize'
gem 'mocha', '0.14.0'
gem 'mocha', '0.14.0', :require => false
gem 'rspec', '~> 3.0.0'
end
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
metaclass (0.0.1)
minitest (5.3)
minitest-colorize (0.0.5)
minitest (>= 2.0)
mocha (0.14.0)
metaclass (~> 0.0.1)
rake (10.0.4)
rspec (3.0.0)
rspec-core (~> 3.0.0)
rspec-expectations (~> 3.0.0)
rspec-mocks (~> 3.0.0)
rspec-core (3.0.2)
rspec-support (~> 3.0.0)
rspec-expectations (3.0.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.0.0)
rspec-mocks (3.0.2)
rspec-support (~> 3.0.0)
rspec-support (3.0.2)

PLATFORMS
ruby
Expand All @@ -17,3 +30,4 @@ DEPENDENCIES
minitest-colorize
mocha (= 0.14.0)
rake
rspec (~> 3.0.0)
5 changes: 4 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require 'rake/testtask'
require 'rspec/core/rake_task'

Rake::TestTask.new do |t|
t.pattern = "test/**/*_test.rb"
t.libs << 'test'
end

task :default => :test
RSpec::Core::RakeTask.new(:spec)

task :default => [:test, :spec]
36 changes: 18 additions & 18 deletions test/cask/audit_test.rb → spec/cask/audit_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'test_helper'
require 'spec_helper'

class CaskMissingUrl < Cask
version '1.2.3'
Expand Down Expand Up @@ -46,21 +46,21 @@ class CaskWithVersionNoChecksum < Cask
describe Cask::Audit do
describe "result" do
it "is 'failed' if there are have been any errors added" do
audit = Cask::Audit.new(TestHelper.test_cask)
audit = Cask::Audit.new(Cask.new)
audit.add_error 'bad'
audit.add_warning 'eh'
audit.result.must_match /failed/
expect(audit.result).to match(/failed/)
end

it "is 'warning' if there are no errors, but there are warnings" do
audit = Cask::Audit.new(TestHelper.test_cask)
audit = Cask::Audit.new(Cask.new)
audit.add_warning 'eh'
audit.result.must_match /warning/
expect(audit.result).to match(/warning/)
end

it "is 'passed' if there are no errors or warning" do
audit = Cask::Audit.new(TestHelper.test_cask)
audit.result.must_match /passed/
audit = Cask::Audit.new(Cask.new)
expect(audit.result).to match(/passed/)
end
end

Expand All @@ -69,31 +69,31 @@ class CaskWithVersionNoChecksum < Cask
it "adds an error if url is missing" do
audit = Cask::Audit.new(CaskMissingUrl.new)
audit.run!
audit.errors.must_include 'url is required'
expect(audit.errors).to include('url is required')
end

it "adds an error if version is missing" do
audit = Cask::Audit.new(CaskMissingVersion.new)
audit.run!
audit.errors.must_include 'version is required'
expect(audit.errors).to include('version is required')
end

it "adds an error if homepage is missing" do
audit = Cask::Audit.new(CaskMissingHomepage.new)
audit.run!
audit.errors.must_include 'homepage is required'
expect(audit.errors).to include('homepage is required')
end

it "adds an error if version is latest and using sha256" do
audit = Cask::Audit.new(CaskVersionLatestWithChecksum.new)
audit.run!
audit.errors.must_include %q{you should use sha256 :no_check when version is 'latest'}
expect(audit.errors).to include(%q{you should use sha256 :no_check when version is 'latest'})
end

it "adds an error if versioned and has no checksum" do
audit = Cask::Audit.new(CaskWithVersionNoChecksum.new)
audit.run!
audit.errors.must_include %q{you must include a sha256 when version is not 'latest'}
expect(audit.errors).to include(%q{you must include a sha256 when version is not 'latest'})
end
end

Expand All @@ -104,27 +104,27 @@ class CaskWithVersionNoChecksum < Cask

audit = Cask::Audit.new(CaskSourceForgeIncorrectURLFormat.new)
audit.run!
audit.warnings.must_include warning_msg
expect(audit.warnings).to include(warning_msg)

audit = Cask::Audit.new(CaskSourceForgeCorrectURLFormat.new)
audit.run!
audit.warnings.wont_include warning_msg
expect(audit.warnings).to_not include(warning_msg)

audit = Cask::Audit.new(CaskSourceForgeOtherCorrectURLFormat.new)
audit.run!
audit.warnings.wont_include warning_msg
expect(audit.warnings).to_not include(warning_msg)
end
end

describe "audit of downloads" do
it "creates an error if the download fails" do
error_message = "Download Failed"
download = mock()
download = double()
download.expects(:perform).raises(StandardError.new(error_message))

audit = Cask::Audit.new(TestHelper.test_cask)
audit = Cask::Audit.new(Cask.new)
audit.run!(download)
audit.errors.first.must_match(/#{error_message}/)
expect(audit.errors).to include(/#{error_message}/)
end
end
end
Expand Down
51 changes: 51 additions & 0 deletions spec/cask/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'

describe Cask::CLI do
it "lists the taps for casks that show up in two taps" do
listing = Cask::CLI.nice_listing(%w[
caskroom/cask/adium
caskroom/cask/google-chrome
passcod/homebrew-cask/adium
])

expect(listing).to eq(%w[
caskroom/cask/adium
google-chrome
passcod/cask/adium
])
end

describe ".process" do
let(:noop_command) { double('CLI::Noop') }
before {
allow(Cask::CLI).to receive(:lookup_command) { noop_command }
allow(noop_command).to receive(:run)
}

it "respects the env variable when choosing what appdir to create" do
with_env_var('HOMEBREW_CASK_OPTS', "--appdir=/custom/appdir") do
allow(Cask).to receive(:init) {
expect(Cask.appdir.to_s).to eq('/custom/appdir')
}
Cask::CLI.process('noop')
end
end

it "respects the ENV variable when choosing a non-default Caskroom location" do
with_env_var 'HOMEBREW_CASK_OPTS', "--caskroom=/custom/caskdir" do
allow(Cask).to receive(:init) {
expect(Cask.caskroom.to_s).to eq('/custom/caskdir')
}
Cask::CLI.process('noop')
end
end

it "exits with a status of 1 when something goes wrong" do
Cask::CLI.expects(:exit).with(1)
Cask::CLI.expects(:lookup_command).raises(CaskError)
shutup {
Cask::CLI.process('noop')
}
end
end
end
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require 'test_helper'
require 'spec_helper'

describe Cask::UnderscoreSupportingURI do
describe 'parse' do
it 'works like normal on normal URLs' do
uri = Cask::UnderscoreSupportingURI.parse('http://example.com/TestCask.dmg')
uri.must_equal URI('http://example.com/TestCask.dmg')
expect(uri).to eq(URI('http://example.com/TestCask.dmg'))
end

it 'works just fine on URIs with underscores' do
uri = Cask::UnderscoreSupportingURI.parse('http://dl_dir.qq.com/qqfile/qq/QQforMac/QQ_V3.0.0.dmg')
uri.host.must_include '_'
uri.to_s.must_equal 'http://dl_dir.qq.com/qqfile/qq/QQforMac/QQ_V3.0.0.dmg'
expect(uri.host).to include('_')
expect(uri.to_s).to eq('http://dl_dir.qq.com/qqfile/qq/QQforMac/QQ_V3.0.0.dmg')
end
end
end
22 changes: 22 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
project_root = Pathname(File.expand_path("../..", __FILE__))

Dir["#{project_root}/spec/support/**/*.rb"].each { |f| require f }

include HomebrewTestingEnvironment

# add cask lib to load path
$:.push(project_root.join('lib').to_s)

require 'cask'

# Look for casks in testcasks by default. It is elsewhere required that
# the string "test" appear in the directory name.
Cask.default_tap = 'caskroom/homebrew-testcasks'

# our own testy caskroom
Cask.caskroom = HOMEBREW_PREFIX.join('TestCaskroom')

RSpec.configure do |config|
config.include ShutupHelper
config.include TempEnvVarHelper
end
19 changes: 19 additions & 0 deletions spec/support/homebrew_testing_environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module HomebrewTestingEnvironment
def self.included(base)
# force some environment variables
ENV['HOMEBREW_NO_EMOJI']='1'

# set some Homebrew constants used in our code
base.const_set('HOMEBREW_BREW_FILE', '/usr/local/bin/brew')

# add homebrew to load path
homebrew_path = Pathname(`brew --prefix`.chomp)
homebrew_path = Pathname('/usr/local') unless homebrew_path.exist?
$:.push(homebrew_path.join('Library', 'Homebrew'))

# require homebrew testing env
with_disabled_at_exit do
require 'test/testing_env'
end
end
end
15 changes: 15 additions & 0 deletions spec/support/kernel_at_exit_hacks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Kernel
alias_method :real_at_exit, :at_exit

def at_exit(&block)
unless ENV['DISABLE_AT_EXIT']
real_at_exit(&block)
end
end

def with_disabled_at_exit(&block)
ENV['DISABLE_AT_EXIT'] = '1'
yield
ENV.delete('DISABLE_AT_EXIT')
end
end
18 changes: 18 additions & 0 deletions spec/support/shutup_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module ShutupHelper
def shutup
if ARGV.verbose?
yield
else
begin
tmperr = $stderr.clone
tmpout = $stdout.clone
$stderr.reopen '/dev/null', 'w'
$stdout.reopen '/dev/null', 'w'
yield
ensure
$stderr.reopen tmperr
$stdout.reopen tmpout
end
end
end
end
14 changes: 14 additions & 0 deletions spec/support/temp_env_var.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module TempEnvVarHelper
def with_env_var(key, val, &block)
was_defined = ENV.key? 'key'
old_value = ENV['key']
ENV[key] = val
block.call
ensure
if was_defined
ENV[key] = old_value
else
ENV.delete(key)
end
end
end
69 changes: 0 additions & 69 deletions test/cask/cli_test.rb

This file was deleted.

Loading

0 comments on commit 58d4c74

Please sign in to comment.