Commit 10f896ee by fistfvck

Merge remote branch 'smtlaissezfaire/master' into restart

Conflicts: lib/annotate/annotate_models.rb spec/annotate/annotate_models_spec.rb
parents a153002b 1dff6966
require 'rubygems'
require 'rake' require 'rake'
require 'lib/annotate' require 'lib/annotate'
...@@ -17,15 +16,15 @@ begin ...@@ -17,15 +16,15 @@ begin
gem.authors = ['Cuong Tran', "Alex Chaffee", "Marcos Piccinini"] gem.authors = ['Cuong Tran', "Alex Chaffee", "Marcos Piccinini"]
gem.files = FileList["[A-Z]*.*", "{bin,lib,tasks,spec}/**/*"] gem.files = FileList["[A-Z]*.*", "{bin,lib,tasks,spec}/**/*"]
gem.rubyforge_project = "annotate" gem.rubyforge_project = "annotate"
# note that Jeweler automatically reads the version from VERSION.yml # note that Jeweler automatically reads the version from VERSION.yml
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end end
Jeweler::RubyforgeTasks.new do |rubyforge| Jeweler::RubyforgeTasks.new do |rubyforge|
rubyforge.doc_task = "rdoc" rubyforge.doc_task = "rdoc"
end end
rescue LoadError rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler" puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end end
......
...@@ -58,7 +58,7 @@ module AnnotateModels ...@@ -58,7 +58,7 @@ module AnnotateModels
attrs = [] attrs = []
attrs << "default(#{quote(col.default)})" unless col.default.nil? attrs << "default(#{quote(col.default)})" unless col.default.nil?
attrs << "not null" unless col.null attrs << "not null" unless col.null
attrs << "primary key" if col.name == klass.primary_key attrs << "primary key" if col.name.to_sym == klass.primary_key.to_sym
col_type = col.type.to_s col_type = col.type.to_s
if col_type == "decimal" if col_type == "decimal"
...@@ -173,12 +173,12 @@ module AnnotateModels ...@@ -173,12 +173,12 @@ module AnnotateModels
if annotate_one_file(model_file_name, info, options_with_position(options, :position_in_class)) if annotate_one_file(model_file_name, info, options_with_position(options, :position_in_class))
annotated = true annotated = true
end end
unless ENV['exclude_tests'] unless ENV['exclude_tests']
[ [
File.join(UNIT_TEST_DIR, "#{model_name}_test.rb"), # test File.join(UNIT_TEST_DIR, "#{model_name}_test.rb"), # test
File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec
].each do |file| ].each do |file|
# todo: add an option "position_in_test" -- or maybe just ask if anyone ever wants different positions for model vs. test vs. fixture # todo: add an option "position_in_test" -- or maybe just ask if anyone ever wants different positions for model vs. test vs. fixture
annotate_one_file(file, info, options_with_position(options, :position_in_fixture)) annotate_one_file(file, info, options_with_position(options, :position_in_fixture))
end end
...@@ -198,7 +198,7 @@ module AnnotateModels ...@@ -198,7 +198,7 @@ module AnnotateModels
FIXTURE_DIRS.each do |dir| FIXTURE_DIRS.each do |dir|
fixture_file_name = File.join(dir,(klass.table_name || "") + ".yml") fixture_file_name = File.join(dir,(klass.table_name || "") + ".yml")
if File.exist?(fixture_file_name) if File.exist?(fixture_file_name)
annotate_one_file(fixture_file_name, info, options_with_position(options, :position_in_fixture)) annotate_one_file(fixture_file_name, info, options_with_position(options, :position_in_fixture))
end end
end end
end end
...@@ -214,7 +214,7 @@ module AnnotateModels ...@@ -214,7 +214,7 @@ module AnnotateModels
annotated annotated
end end
# position = :position_in_fixture or :position_in_class # position = :position_in_fixture or :position_in_class
def options_with_position(options, position_in) def options_with_position(options, position_in)
options.merge(:position=>(options[position_in] || options[:position])) options.merge(:position=>(options[position_in] || options[:position]))
...@@ -321,7 +321,7 @@ module AnnotateModels ...@@ -321,7 +321,7 @@ module AnnotateModels
fixture_file_name = File.join(dir,(klass.table_name || "") + ".yml") fixture_file_name = File.join(dir,(klass.table_name || "") + ".yml")
remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name) remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name)
end end
[ File.join(UNIT_TEST_DIR, "#{klass.name.underscore}_test.rb"), [ File.join(UNIT_TEST_DIR, "#{klass.name.underscore}_test.rb"),
File.join(SPEC_MODEL_DIR,"#{klass.name.underscore}_spec.rb")].each do |file| File.join(SPEC_MODEL_DIR,"#{klass.name.underscore}_spec.rb")].each do |file|
remove_annotation_of_file(file) if File.exist?(file) remove_annotation_of_file(file) if File.exist?(file)
......
#encoding: utf-8 #encoding: utf-8
require File.dirname(__FILE__) + '/../spec_helper.rb' require File.dirname(__FILE__) + '/../spec_helper.rb'
require 'annotate/annotate_models' require 'annotate/annotate_models'
require 'rubygems'
require 'active_support' require 'active_support'
require 'fakefs/spec_helpers'
describe AnnotateModels do describe AnnotateModels do
include FakeFS::SpecHelpers
before(:all) do before(:all) do
require "tmpdir" require "tmpdir"
...@@ -14,23 +15,41 @@ describe AnnotateModels do ...@@ -14,23 +15,41 @@ describe AnnotateModels do
end end
module ::ActiveRecord module ::ActiveRecord
class Base class Base
end end
end
def create(file, body="hi")
File.open(@dir + '/' + file, "w") do |f|
f.puts(body)
end
@dir + '/' + file
end
def mock_klass(stubs={})
mock("Klass", stubs)
end end
def create(file, body="hi")
File.open(@dir + '/' + file, "w") do |f|
f.puts(body)
end
@dir + '/' + file
end
def mock_class(table_name, primary_key, columns)
options = {
:connection => mock("Conn", :indexes => []),
:table_name => table_name,
:primary_key => primary_key.to_s,
:column_names => columns.map { |col| col.name.to_s },
:columns => columns
}
mock("An ActiveRecord class", options)
end
def mock_column(name, type, options={})
default_options = {
:limit => nil,
:null => false,
:default => nil
}
def mock_column(stubs={}) stubs = default_options.dup
mock("Column", stubs) stubs.merge!(options)
stubs.merge!(:name => name, :type => type)
mock("Column", stubs)
end end
it { AnnotateModels.quote(nil).should eql("NULL") } it { AnnotateModels.quote(nil).should eql("NULL") }
...@@ -40,10 +59,13 @@ describe AnnotateModels do ...@@ -40,10 +59,13 @@ describe AnnotateModels do
it { AnnotateModels.quote(25.6).should eql("25.6") } it { AnnotateModels.quote(25.6).should eql("25.6") }
it { AnnotateModels.quote(1e-20).should eql("1.0e-20") } it { AnnotateModels.quote(1e-20).should eql("1.0e-20") }
describe "schema info" do it "should get schema info" do
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:name, :string, :limit => 50)
])
before(:each) do AnnotateModels.get_schema_info(klass, "Schema Info").should eql(<<-EOS)
@schema_info = <<-EOS
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -53,69 +75,11 @@ describe AnnotateModels do ...@@ -53,69 +75,11 @@ describe AnnotateModels do
# #
EOS EOS
end
@user_file = create('user.rb', <<-EOS)
class User < ActiveRecord::Base
end
EOS
@mock = mock_klass(
:connection => mock("Conn", :indexes => []),
:table_name => "users",
:primary_key => "id",
:column_names => ["id","name"],
:columns => [
mock_column(:type => "integer", :default => nil, :null => false, :name => "id", :limit => nil),
mock_column(:type => "string", :default => nil, :null => false, :name => "name", :limit => 50)
])
end
it "should get schema info" do
AnnotateModels.get_schema_info(@mock , "Schema Info").should eql(@schema_info)
end
it "should write the schema before (default)" do
ARGV.stub!(:dup).and_return []
AnnotateModels.stub!(:get_schema_info).and_return @schema_info
AnnotateModels.do_annotations
File.read(@user_file).should eql(<<-EOF)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
#
class User < ActiveRecord::Base
end
EOF
end
it "should write the schema after" do
ARGV.stub!(:dup).and_return []
AnnotateModels.stub!(:get_schema_info).and_return @schema_info
AnnotateModels.do_annotations(:position => :after)
File.read(@user_file).should eql(<<-EOF)
class User < ActiveRecord::Base
end
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
#
EOF
end
end
describe "#get_model_class" do describe "#get_model_class" do
before :all do before :all do
create('foo.rb', <<-EOS) create('foo.rb', <<-EOS)
class Foo < ActiveRecord::Base class Foo < ActiveRecord::Base
end end
...@@ -151,18 +115,16 @@ EOF ...@@ -151,18 +115,16 @@ EOF
describe "#remove_annotation_of_file" do describe "#remove_annotation_of_file" do
def create(file, body="hi") def create(file, body="hi")
File.open(@dir + '/' + file, "w") do |f| File.open(file, "w") do |f|
f.puts(body) f.puts(body)
end end
end end
def content(file) def content(file)
File.read(@dir + '/' + file) File.read(file)
end end
before :all do it "should remove before annotate" do
require "tmpdir"
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/annotate_models"
FileUtils.mkdir_p(@dir)
create("before.rb", <<-EOS) create("before.rb", <<-EOS)
# == Schema Information # == Schema Information
# #
...@@ -176,6 +138,16 @@ EOF ...@@ -176,6 +138,16 @@ EOF
class Foo < ActiveRecord::Base class Foo < ActiveRecord::Base
end end
EOS EOS
AnnotateModels.remove_annotation_of_file("before.rb")
content("before.rb").should == <<-EOS
class Foo < ActiveRecord::Base
end
EOS
end
it "should remove after annotate" do
create("after.rb", <<-EOS) create("after.rb", <<-EOS)
class Foo < ActiveRecord::Base class Foo < ActiveRecord::Base
end end
...@@ -190,16 +162,9 @@ end ...@@ -190,16 +162,9 @@ end
# #
EOS EOS
end
it "should remove before annotate" do AnnotateModels.remove_annotation_of_file("after.rb")
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 content("after.rb").should == <<-EOS
class Foo < ActiveRecord::Base class Foo < ActiveRecord::Base
end end
...@@ -207,4 +172,38 @@ end ...@@ -207,4 +172,38 @@ end
end end
end end
describe "annotating a file" do
before do
@file_name = "user.rb"
@file_content = "class User < ActiveRecord::Base; end"
@klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:name, :string, :limit => 50)
])
end
def write_file
File.open("user.rb", "w") do |f|
f << @file_content
end
end
it "should annotate the file before the model if position == 'before'" do
write_file
schema_info = AnnotateModels.get_schema_info(@klass, "Schema Info")
AnnotateModels.annotate_one_file("user.rb", schema_info, :position => "before")
File.read("user.rb").should == "#{schema_info}#{@file_content}\n"
end
it "should annotate before if given :position => :before" do
write_file
schema_info = AnnotateModels.get_schema_info(@klass, "Schema Info")
AnnotateModels.annotate_one_file(@file_name, schema_info, :position => :before)
File.read("user.rb").should == "#{schema_info}#{@file_content}\n"
end
end
end end
begin require 'rubygems'
require 'spec' require 'spec'
rescue LoadError
require 'rubygems'
gem 'rspec'
require 'spec'
end
$:.unshift(File.dirname(__FILE__) + '/../lib') $:.unshift(File.dirname(__FILE__) + '/../lib')
require 'annotate' require 'annotate'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment