Permalink
Browse files

enhc: detecting duplicate definitions and then warning the user

  • Loading branch information...
jaredfolkins committed Nov 30, 2012
1 parent 31806e8 commit c6db8600bfdaddcf3d3e58383f25ab5c9fb9a249
Showing with 39 additions and 0 deletions.
  1. +21 −0 lib/guard/guardfile.rb
  2. +18 −0 spec/guard/guardfile_spec.rb
View
@@ -30,6 +30,24 @@ def create_guardfile(options = {})
end
end
+ # Opens an existing guardfile and searches for redundant definitions
+ # if extraneous defintions are found, it warns the user
+ #
+ # @see Guard::CLI.init
+ #
+ # @param [String] class name of gem definition that you would like to search for in the Guardfile
+ # @param [String] contents of existing guardfile
+ #
+ def duplicate_definitions?(guard_class, guard_file)
+ matches = guard_file.to_s.scan(/guard\s[\'|\"]#{guard_class}[\'|\"]\sdo/)
+ if matches.count > 1
+ ::Guard::UI.info "There are #{matches.count.to_s} definitions in your Guardfile for '#{guard_class}', you may want to clean up your Guardfile as this could cause issues."
+ return true
+ else
+ return false
+ end
+ end
+
# Adds the Guardfile template of a Guard implementation
# to an existing Guardfile.
#
@@ -42,6 +60,9 @@ def initialize_template(guard_name)
if guard_class
guard_class.init(guard_name)
+ guardfile_name = 'Guardfile'
+ guard_file = File.read(guardfile_name) if File.exists?(guardfile_name)
+ duplicate_definitions?(guard_name, guard_file)
elsif File.exist?(File.join(HOME_TEMPLATES, guard_name))
content = File.read('Guardfile')
template = File.read(File.join(HOME_TEMPLATES, guard_name))
@@ -46,6 +46,24 @@
end
end
+ describe ".duplicate_defintions?" do
+ context "that finds an existing Guardfile" do
+ context "that has duplicate definitions" do
+ it "should return true" do
+ io = StringIO.new("guard 'rspec' do\nend\nguard 'rspec' do\nend\n")
+ Guard::Guardfile.duplicate_definitions?('rspec', io.string).should == true
+ end
+ end
+
+ context "that doesn't have duplicate definitions" do
+ it "should return false" do
+ io = StringIO.new("guard 'rspec' do\nend\n")
+ Guard::Guardfile.duplicate_definitions?('rspec', io.string).should == false
+ end
+ end
+ end
+ end
+
describe ".initialize_template" do
context 'with an installed Guard implementation' do
let(:foo_guard) { double('Guard::Foo').as_null_object }

0 comments on commit c6db860

Please sign in to comment.