Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bundle exec guard init is adding duplicate definitions to the Guardfile #368

Merged
merged 1 commit into from Nov 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/guard/guardfile.rb
Expand Up @@ -30,6 +30,24 @@ def create_guardfile(options = {})
end end
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 # Adds the Guardfile template of a Guard implementation
# to an existing Guardfile. # to an existing Guardfile.
# #
Expand All @@ -42,6 +60,9 @@ def initialize_template(guard_name)


if guard_class if guard_class
guard_class.init(guard_name) 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)) elsif File.exist?(File.join(HOME_TEMPLATES, guard_name))
content = File.read('Guardfile') content = File.read('Guardfile')
template = File.read(File.join(HOME_TEMPLATES, guard_name)) template = File.read(File.join(HOME_TEMPLATES, guard_name))
Expand Down
18 changes: 18 additions & 0 deletions spec/guard/guardfile_spec.rb
Expand Up @@ -46,6 +46,24 @@
end end
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 describe ".initialize_template" do
context 'with an installed Guard implementation' do context 'with an installed Guard implementation' do
let(:foo_guard) { double('Guard::Foo').as_null_object } let(:foo_guard) { double('Guard::Foo').as_null_object }
Expand Down