Move the template initialization to the Guard module and add specs.

This commit is contained in:
Michael Kessler 2011-10-04 09:46:14 +02:00
parent cdf0906614
commit 4451d73583
3 changed files with 70 additions and 13 deletions

View File

@ -18,6 +18,28 @@ module Guard
class << self
attr_accessor :options, :interactor, :listener
# Creates the initial Guardfile template or add a Guard implementation
# Guardfile template to an existing Guardfile.
#
# @see Guard::Guard.init
#
# @param [String] guard_name the name of the Guard to initialize
#
def initialize_template(guard_name = nil)
if guard_name
guard_class = ::Guard.get_guard_class(guard_name)
guard_class.init(guard_name)
else
if !File.exist?('Guardfile')
::Guard::UI.info "Writing new Guardfile to #{ Dir.pwd }/Guardfile"
FileUtils.cp(File.expand_path('../templates/Guardfile', __FILE__), 'Guardfile')
else
::Guard::UI.error "Guardfile already exists at #{ Dir.pwd }/Guardfile"
exit 1
end
end
end
# Initialize the Guard singleton.
#
# @option options [Boolean] clear if auto clear the UI should be done

View File

@ -3,8 +3,9 @@ require 'guard/version'
module Guard
# Guard command line interface managed by [Thor](https://github.com/wycats/thor).
# Facade for the Guard command line interface managed by [Thor](https://github.com/wycats/thor).
# This is the main interface to Guard that is called by the Guard binary `bin/guard`.
# Do not put any logic in here, create a class and delegate instead.
#
class CLI < Thor
@ -66,6 +67,8 @@ module Guard
# List the Guards that are available for use in your system and marks
# those that are currently used in your `Guardfile`.
#
# @see Guard::DslDescriber.list
#
def list
Guard::DslDescriber.list(options)
end
@ -86,21 +89,12 @@ module Guard
# Appends the Guard template to the `Guardfile`, or creates an initial
# `Guardfile` when no Guard name is passed.
#
# @see Guard.initialize_template
#
# @param [String] guard_name the name of the Guard to initialize
#
def init(guard_name = nil)
if !File.exist?('Guardfile')
puts "Writing new Guardfile to #{Dir.pwd}/Guardfile"
FileUtils.cp(File.expand_path('../templates/Guardfile', __FILE__), 'Guardfile')
elsif guard_name.nil?
Guard::UI.error "Guardfile already exists at #{ Dir.pwd }/Guardfile"
exit 1
end
if guard_name
guard_class = ::Guard.get_guard_class(guard_name)
guard_class.init(guard_name)
end
Guard.initialize_template(guard_name)
end
desc 'show', 'Show all defined Guards and their options'
@ -109,6 +103,8 @@ module Guard
# Shows all Guards and their options that are defined in
# the `Guardfile`.
#
# @see Guard::DslDescriber.show
#
def show
Guard::DslDescriber.show(options)
end

View File

@ -3,6 +3,45 @@ require 'guard/guard'
describe Guard do
describe ".initialize_template" do
context "with a Guard name" do
it "initializes a the Guard" do
class Guard::TestGuard < Guard::Guard
end
Guard::TestGuard.should_receive(:init)
Guard.initialize_template('test-guard')
end
end
context "without a Guard name" do
context "with an existing Guardfile" do
before do
File.stub(:exist?).and_return true
Dir.stub(:pwd).and_return "/home/user"
end
it "shows an error" do
Guard.should_receive(:exit).with 1
::Guard::UI.should_receive(:error).with("Guardfile already exists at /home/user/Guardfile")
Guard.initialize_template()
end
end
context "without an existing Guardfile" do
before do
File.stub(:exist?).and_return false
Dir.stub(:pwd).and_return "/home/user"
end
it "copies the Guardfile template" do
::Guard::UI.should_receive(:info).with("Writing new Guardfile to /home/user/Guardfile")
FileUtils.should_receive(:cp).with(an_instance_of(String), 'Guardfile')
Guard.initialize_template()
end
end
end
end
describe ".setup" do
subject { ::Guard.setup }