Browse Source

Another unit test for dirify

dev
Andy Meneely 11 years ago
parent
commit
2a58323bd4
  1. 2
      lib/squib/input_helpers.rb
  2. 2
      spec/commands/new_spec.rb
  3. 28
      spec/input_helpers_spec.rb
  4. 11
      spec/spec_helper.rb

2
lib/squib/input_helpers.rb

@ -112,7 +112,7 @@ module Squib
def dirify(opts, key, allow_create=false)
return opts if Dir.exists?(opts[key])
if allow_create
Squib.logger.warn {"Dir #{opts[key]} does not exist, creating it."}
Squib.logger.warn("Dir '#{opts[key]}' does not exist, creating it.")
Dir.mkdir opts[key]
return opts
else

2
spec/commands/new_spec.rb

@ -8,7 +8,7 @@ describe Squib::Commands::New do
@old_stderr = $stderr
$stderr = StringIO.new
@oldpwd = Dir.pwd
Dir.chdir(File.expand_path('../../samples/_output', File.dirname(__FILE__)))
Dir.chdir(output_dir)
end
before(:each) do

28
spec/input_helpers_spec.rb

@ -1,4 +1,5 @@
require 'spec_helper'
require 'squib'
require 'squib/input_helpers'
class DummyDeck
@ -15,7 +16,6 @@ end
describe Squib::InputHelpers do
before(:each) do
@deck = DummyDeck.new
@deck.layout = {
@ -29,12 +29,11 @@ describe Squib::InputHelpers do
context '#layoutify' do
it 'warns on the logger when the layout does not exist' do
@old_logger = Squib.logger
Squib.logger = instance_double(Logger)
expect(Squib.logger).to receive(:warn).with("Layout entry 'foo' does not exist.").twice
expect(Squib.logger).to receive(:debug)
expect(@deck.send(:layoutify, {layout: :foo})).to eq({layout: [:foo,:foo]})
Squib.logger = @old_logger
mock_squib_logger(@old_logger) do
expect(Squib.logger).to receive(:warn).with("Layout entry 'foo' does not exist.").twice
expect(Squib.logger).to receive(:debug)
expect(@deck.send(:layoutify, {layout: :foo})).to eq({layout: [:foo,:foo]})
end
end
it 'applies the layout in a normal situation' do
@ -83,11 +82,24 @@ describe Squib::InputHelpers do
end
end
context '#dir' do
context '#dirify' do
it 'should raise an error if the directory does not exist' do
expect{@deck.send(:dirify, {dir: 'nonexist'}, :dir, false)}.to \
raise_error(RuntimeError,"'nonexist' does not exist!")
end
it 'should warn and make a directory creation is allowed' do
opts = {dir: 'tocreate'}
Dir.chdir(output_dir) do
FileUtils.rm_rf('tocreate', secure: true)
mock_squib_logger(@old_logger) do
expect(Squib.logger).to receive(:warn).with("Dir 'tocreate' does not exist, creating it.").once
expect(@deck.send(:dirify, opts, :dir, true)).to eq(opts)
expect(Dir.exists? 'tocreate').to be true
end
end
end
end
context '#colorify' do

11
spec/spec_helper.rb

@ -10,3 +10,14 @@ SimpleCov.start
def test_file(str)
"#{File.expand_path(File.dirname(__FILE__))}/data/#{str}"
end
def mock_squib_logger(old_logger)
old_logger = Squib.logger
Squib.logger = instance_double(Logger)
yield
Squib.logger = old_logger
end
def output_dir
File.expand_path('../samples/_output', File.dirname(__FILE__))
end

Loading…
Cancel
Save