Skip to content

Commit

Permalink
add AnnotateModels.#remove_annotation_of_file spec
Browse files Browse the repository at this point in the history
  • Loading branch information
miyucy committed Jun 12, 2010
1 parent 9c68c8c commit ace84ab
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/annotate/annotate_models.rb
Expand Up @@ -127,7 +127,7 @@ def annotate_one_file(file_name, info_block, options={})
false
else
# Remove old schema info
old_content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
old_content.sub!(/^\n?# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')

# Write it back
new_content = options[:position] == 'before' ? (info_block + old_content) : (old_content + "\n" + info_block)
Expand All @@ -142,7 +142,7 @@ def remove_annotation_of_file(file_name)
if File.exist?(file_name)
content = File.read(file_name)

content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
content.sub!(/^\n?# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')

File.open(file_name, "wb") { |f| f.puts content }
end
Expand Down
58 changes: 58 additions & 0 deletions spec/annotate/annotate_models_spec.rb
Expand Up @@ -80,4 +80,62 @@ class FooWithMacro < ActiveRecord::Base
end
end

describe "#remove_annotation_of_file" do
def create(file, body="hi")
File.open(@dir + '/' + file, "w") do |f|
f.puts(body)
end
end
def content(file)
File.read(@dir + '/' + file)
end

before :all do
require "tmpdir"
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/annotate_models"
FileUtils.mkdir_p(@dir)
create("before.rb", <<-EOS)
# == Schema Information
#
# Table name: foo
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
#
class Foo < ActiveRecord::Base
end
EOS
create("after.rb", <<-EOS)
class Foo < ActiveRecord::Base
end
# == Schema Information
#
# Table name: foo
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
#
EOS
end
it "should remove before annotate" do
AnnotateModels.remove_annotation_of_file(@dir + '/' + "before.rb")
content("before.rb").should == <<-EOS
class Foo < ActiveRecord::Base
end
EOS
end
it "should remove after annotate" do
AnnotateModels.remove_annotation_of_file(@dir + '/' + "after.rb")
content("after.rb").should == <<-EOS
class Foo < ActiveRecord::Base
end
EOS
end
end

end

0 comments on commit ace84ab

Please sign in to comment.