From 1845efb11ec0254d83724d3d104af5d7e2dfda66 Mon Sep 17 00:00:00 2001 From: Michael Kessler Date: Mon, 10 Oct 2011 08:05:10 +0200 Subject: [PATCH] Add missing specs for Guard implementation base class --- lib/guard/guard.rb | 4 ++- spec/guard/guard_spec.rb | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 spec/guard/guard_spec.rb diff --git a/lib/guard/guard.rb b/lib/guard/guard.rb index 97c0a17..1771015 100644 --- a/lib/guard/guard.rb +++ b/lib/guard/guard.rb @@ -55,12 +55,14 @@ module Guard else content = File.read('Guardfile') guard = File.read("#{ ::Guard.locate_guard(name) }/lib/guard/#{ name }/templates/Guardfile") + File.open('Guardfile', 'wb') do |f| f.puts(content) f.puts("") f.puts(guard) end - ::Guard::UI.info "#{name} guard added to Guardfile, feel free to edit it" + + ::Guard::UI.info "#{ name } guard added to Guardfile, feel free to edit it" end end diff --git a/spec/guard/guard_spec.rb b/spec/guard/guard_spec.rb new file mode 100644 index 0000000..49ad38a --- /dev/null +++ b/spec/guard/guard_spec.rb @@ -0,0 +1,60 @@ +require 'spec_helper' + +describe Guard::Guard do + + describe '#initialize' do + + it 'assigns the defined watchers' do + watchers = [ Guard::Watcher.new('*') ] + guard = Guard::Guard.new(watchers) + guard.watchers.should eql watchers + end + + it 'assigns the defined options' do + options = { :a => 1, :b => 2 } + guard = Guard::Guard.new([], options) + guard.options.should eql options + end + + context 'with a group in the options' do + it 'assigns the given group' do + options = { :group => :test } + guard = Guard::Guard.new([], options) + guard.group.should eql :test + end + end + + context 'without a group in the options' do + it 'assigns a default group' do + options = { } + guard = Guard::Guard.new([], options) + guard.group.should eql :default + end + end + end + + describe '#init' do + context 'when the Guard is already in the Guardfile' do + before { ::Guard::Dsl.stub(:guardfile_include?).and_return true } + + it 'shows an info message' do + ::Guard::UI.should_receive(:info).with 'Guardfile already includes myguard guard' + Guard::Guard.init('myguard') + end + end + + context 'when the Guard is not in the Guardfile' do + before { ::Guard::Dsl.stub(:guardfile_include?).and_return false } + + it 'appends the template to the Guardfile' do + File.should_receive(:read).with('Guardfile').and_return 'Guardfile content' + ::Guard.should_receive(:locate_guard).with('myguard').and_return '/Users/me/projects/guard-myguard' + File.should_receive(:read).with('/Users/me/projects/guard-myguard/lib/guard/myguard/templates/Guardfile').and_return('Template content') + io = StringIO.new + File.should_receive(:open).with('Guardfile', 'wb').and_yield io + Guard::Guard.init('myguard') + io.string.should eql "Guardfile content\n\nTemplate content\n" + end + end + end +end