From 42175ca94a4fa41e03cf1566c858565f3617b32f Mon Sep 17 00:00:00 2001 From: Victor Costan Date: Tue, 6 Jul 2010 12:40:02 -0400 Subject: [PATCH] Initial work for re-packaging as gem. --- .gitignore | 23 ++++++++++++++++++- Gemfile | 18 ++++++++++++--- Rakefile | 48 ++++++++++++++++++++++++++++++++++++++-- test/import_test.rb | 2 +- test/support/generate.rb | 4 ++-- test/test_helper.rb | 40 +++++++++++++++++++-------------- 6 files changed, 109 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 6047f3f..8a62f7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,23 @@ +## MAC OS +.DS_Store + +## TEXTMATE +*.tmproj +tmtags + +## EMACS +*~ +\#* +.\#* + +## VIM +*.swp + +## PROJECT::GENERAL +coverage +rdoc +pkg + +## PROJECT::SPECIFIC log/*.log -test.db \ No newline at end of file +test.db diff --git a/Gemfile b/Gemfile index 4c724c8..3cf4703 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,17 @@ source :gemcutter -gem "rails", "= 3.0.0rc" -gem "factory_girl", "= 1.3.2" -gem "delorean", :version => "= 0.2.0" +gem "rails", ">= 3.0.0.rc" + +group :test do + # Database Adapters + gem "mysql", ">= 2.8.1" + gem "pg", ">= 0.9.0" + gem "sqlite3-ruby", ">= 1.3.1" + + # Support libs + gem "factory_girl", ">= 1.3.2" + gem "delorean", ">= 0.2.0" + + # Debugging + gem "ruby-debug", ">= 0.9.3" +end diff --git a/Rakefile b/Rakefile index 17d0ecc..4be6cb4 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,26 @@ +require 'rubygems' +require 'rake' require 'rake/testtask' -task :default => ["display:notice"] +begin + require 'jeweler' + require 'bundler' + Jeweler::Tasks.new do |gem| + gem.name = "activerecord-import" + gem.summary = %Q{Bulk-loading extension for ActiveRecord} + gem.description = %Q{Extraction of the ActiveRecord::Base#import functionality from ar-extensions for Rails 3 and beyond} + gem.email = "zach.dennis@gmail.com" + gem.homepage = "http://github.com/zdennis/activerecord-import" + gem.authors = ["Zach Dennis"] + gem.files = FileList["VERSION", "Rakefile", "README*", "lib/**/*"] + gem.add_bundler_dependencies -ADAPTERS = %w(mysql postgresql sqlite3) + # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings + end + Jeweler::GemcutterTasks.new +rescue LoadError + puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler" +end namespace :display do task :notice do @@ -11,12 +29,38 @@ namespace :display do puts end end +task :default => ["display:notice"] +ADAPTERS = %w(mysql postgresql sqlite3) ADAPTERS.each do |adapter| namespace :test do desc "Runs #{adapter} database tests." Rake::TestTask.new(adapter) do |t| t.test_files = FileList["test/adapters/#{adapter}.rb", "test/*_test.rb", "test/#{adapter}/**/*_test.rb"] end + task adapter => :check_dependencies end end + +begin + require 'rcov/rcovtask' + Rcov::RcovTask.new do |test| + test.libs << 'test' + test.pattern = "test/*_test.rb" + test.verbose = true + end +rescue LoadError + task :rcov do + abort "RCov is not available. In order to run rcov, you must: sudo gem install rcov" + end +end + +require 'rake/rdoctask' +Rake::RDocTask.new do |rdoc| + version = File.exist?('VERSION') ? File.read('VERSION') : "" + + rdoc.rdoc_dir = 'rdoc' + rdoc.title = "activerecord-import #{version}" + rdoc.rdoc_files.include('README*') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/test/import_test.rb b/test/import_test.rb index ce422f8..317c584 100644 --- a/test/import_test.rb +++ b/test/import_test.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.dirname(__FILE__) + '/test_helper') +require File.expand_path('../test_helper', __FILE__) describe "#import" do it "should return the number of inserts performed" do diff --git a/test/support/generate.rb b/test/support/generate.rb index b624ce2..1e6ee1a 100644 --- a/test/support/generate.rb +++ b/test/support/generate.rb @@ -5,7 +5,7 @@ class ActiveSupport::TestCase factory_girl_args = args.shift || {} if n - returning Array.new do |collection| + Array.new.tap do |collection| n.times.each { collection << Factory.build(factory.to_s.singularize.to_sym, factory_girl_args) } end else @@ -19,7 +19,7 @@ class ActiveSupport::TestCase factory_girl_args = args.shift || {} if n - returning Array.new do |collection| + Array.new.tap do |collection| n.times.each { collection << Factory.create(factory.to_s.singularize.to_sym, factory_girl_args) } end else diff --git a/test/test_helper.rb b/test/test_helper.rb index ae1001c..90704b8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,14 +1,20 @@ -ENV["RAILS_ENV"] = "test" require 'pathname' -this_dir = Pathname.new File.dirname(__FILE__) -$LOAD_PATH << this_dir.join("../lib") +test_dir = Pathname.new File.dirname(__FILE__) +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) +$LOAD_PATH.unshift(File.dirname(__FILE__)) + +require "fileutils" +require "rubygems" + +ENV["RAILS_ENV"] = "test" + +require "bundler" +Bundler.setup -gem "rails", "3.0.0.beta" require "rails" require "rails/test_help" require "active_record/fixtures" require "active_support/test_case" -require 'active_support/core_ext/object/returning' require "delorean" @@ -17,6 +23,13 @@ require "logger" require "ruby-debug" +# load test helpers +class MyApplication < Rails::Application ; end +adapter = ENV["ARE_DB"] || "sqlite3" + +# load the library +require "activerecord-import/#{adapter}" + class ActiveSupport::TestCase include ActiveRecord::TestFixtures self.use_transactional_fixtures = true @@ -84,17 +97,10 @@ def describe(description, &blk) ActiveSupport::TestCase.describe(description, true, &blk) end -# load test helpers -require "rails" -class MyApplication < Rails::Application ; end -adapter = ENV["ARE_DB"] || "sqlite3" - -# load the library -require "activerecord-import/#{adapter}" - +FileUtils.mkdir_p'log' ActiveRecord::Base.logger = Logger.new("log/test.log") ActiveRecord::Base.logger.level = Logger::DEBUG -ActiveRecord::Base.configurations["test"] = YAML.load(this_dir.join("database.yml").open)[adapter] +ActiveRecord::Base.configurations["test"] = YAML.load(test_dir.join("database.yml").open)[adapter] ActiveRecord::Base.establish_connection "test" ActiveSupport::Notifications.subscribe(/active_record.sql/) do |event, _, _, _, hsh| @@ -105,9 +111,9 @@ require "factory_girl" Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each{ |file| require file } # Load base/generic schema -require this_dir.join("schema/version") -require this_dir.join("schema/generic_schema") -adapter_schema = this_dir.join("schema/#{adapter}_schema.rb") +require test_dir.join("schema/version") +require test_dir.join("schema/generic_schema") +adapter_schema = test_dir.join("schema/#{adapter}_schema.rb") require adapter_schema if File.exists?(adapter_schema) Dir[File.dirname(__FILE__) + "/models/*.rb"].each{ |file| require file }