From 2a58323bd4fd8fcdc882ac2643525b5306c3d732 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Wed, 29 Oct 2014 15:37:45 -0400 Subject: [PATCH] Another unit test for dirify --- lib/squib/input_helpers.rb | 2 +- spec/commands/new_spec.rb | 2 +- spec/input_helpers_spec.rb | 28 ++++++++++++++++++++-------- spec/spec_helper.rb | 11 +++++++++++ 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/squib/input_helpers.rb b/lib/squib/input_helpers.rb index c500d43..50dda4e 100644 --- a/lib/squib/input_helpers.rb +++ b/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 diff --git a/spec/commands/new_spec.rb b/spec/commands/new_spec.rb index 8d7a4e3..036c1e9 100644 --- a/spec/commands/new_spec.rb +++ b/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 diff --git a/spec/input_helpers_spec.rb b/spec/input_helpers_spec.rb index 55b2c2d..808629d 100644 --- a/spec/input_helpers_spec.rb +++ b/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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e81f277..c888e97 100644 --- a/spec/spec_helper.rb +++ b/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