Permalink
Browse files

Create Seedbank::DSL#override_seed_task method.

Implemented override_seed_task and integrated into seed.rake.

Signed-off-by: James McCarthy <james2mccarthy@gmail.com>
  • Loading branch information...
1 parent 504c0ac commit 2a4d1bf4edccf5471c5fd57cba08128ab94bc94c @james2m committed Jul 23, 2012
Showing with 82 additions and 64 deletions.
  1. +5 −9 lib/seedbank/dsl.rb
  2. +2 −2 lib/tasks/seed.rake
  3. +44 −33 test/lib/seedbank/dsl_test.rb
  4. +31 −20 test/lib/tasks/seed_rake_test.rb
View
@@ -2,16 +2,12 @@ module Seedbank
module DSL
def self.included(base)
- Rake::Task.extend(Seedbank::RenameTask)
+ Rake::Task.send(:include, Seedbank::RenameTask)
Rake::Application.send(:include, Seedbank::TaskManager)
end
- def override_task(*args, &block)
- name, params, deps = Rake.application.resolve_args(args.dup)
- fq_name = Rake.application.instance_variable_get(:@scope).dup.push(name).join(':')
- new_name = "#{fq_name}:original"
- Rake::Task.rename_task(fq_name, new_name)
- Rake::Task.define_task(*args, &block)
+ def override_seed_task(*args, &block)
+ Rake.application.override_task(*args, &block)
end
def seed_task_from_file(seed_file)
@@ -21,7 +17,7 @@ def seed_task_from_file(seed_file)
define_seed_task(seed_file, args)
end
-
+
def glob_seed_files_matching(*args, &block)
Dir.glob(File.join(seeds_root, *args), &block)
end
@@ -33,7 +29,7 @@ def define_seed_task(seed_file, *args)
task.add_description "Load the seed data from #{seed_file}"
task.name
end
-
+
def scope_from_seed_file(seed_file)
dirname = Pathname.new(seed_file).dirname
return [] if dirname == seeds_root
View
@@ -4,7 +4,7 @@ namespace :db do
base_dependencies = ['db:seed:original']
override_dependency = []
-
+
namespace :seed do
# Create seed tasks for all the seeds in seeds_path and add them to the dependency
# list along with the original db/seeds.rb.
@@ -33,6 +33,6 @@ namespace :db do
Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/ENVIRONMENT/*.seeds.rb.
ENVIRONMENT is the current environment in Rails.env.
EOT
- override_task :seed => ['db:seed:common'] + override_dependency
+ override_seed_task :seed => ['db:seed:common'] + override_dependency
end
@@ -6,130 +6,141 @@
it "is added to the namesapce" do
ns = Rake.application.in_namespace(:seedy) { self }
-
+
ns.must_respond_to :scope_from_seed_file
end
describe "in an environment directory" do
-
+
let(:seed_file) { File.expand_path('development/users.seeds.rb', Seedbank.seeds_root) }
let(:seed_namespace) { %w(development) }
-
+
subject { Seedbank::DSL.scope_from_seed_file seed_file }
-
+
it "returns the enviroment scope" do
subject.must_equal seed_namespace
end
end
describe "in a nested directory" do
-
+
let(:seed_file) { File.expand_path('development/shared/accounts.seeds.rb', Seedbank.seeds_root) }
let(:seed_namespace) { %w(development shared) }
-
+
subject { Seedbank::DSL.scope_from_seed_file seed_file }
-
+
it "returns the nested scope" do
subject.must_equal seed_namespace
end
end
describe "in seeds root" do
-
+
let(:seed_file) { File.expand_path('no_block.seeds.rb', Seedbank.seeds_root) }
-
+
subject { Seedbank::DSL.scope_from_seed_file seed_file }
-
+
it "returns an array" do
subject.must_be_instance_of Array
end
-
+
it "must be empty" do
subject.must_be_empty
end
end
-
end
describe "seeds_root" do
-
+
let(:seeds_root) { '/my/seeds/directory' }
subject { Seedbank::DSL.seeds_root }
it "returns a Pathname" do
flexmock(Seedbank).should_receive(:seeds_root).and_return(seeds_root).by_default
-
+
subject.must_equal Pathname.new(seeds_root)
end
-
+
end
describe "glob_seed_files_matching" do
-
+
describe "with no namespace" do
-
+
let(:pattern) { '*.seeds.rb' }
-
+
it "returns all the files matching the pattern in seeds_root" do
expected_files = Dir.glob(File.join(Seedbank.seeds_root, pattern))
-
+
Seedbank::DSL.glob_seed_files_matching(pattern).must_equal expected_files
end
-
+
end
-
+
describe "with a namespace" do
-
+
let(:pattern) { '*.seeds.rb' }
let(:namespace) { 'development' }
-
+
it "returns all the files matching the pattern in seeds_root" do
expected_files = Dir.glob(File.join(Seedbank.seeds_root, namespace, pattern))
-
+
Seedbank::DSL.glob_seed_files_matching(namespace, pattern).must_equal expected_files
end
-
+
end
-
+
end
describe "define_seed_task" do
-
+
let(:name) { 'scoped:my_seed' }
let(:dependencies) { ['environment'] }
let(:seed_file) { File.expand_path('development/users.seeds.rb', Seedbank.seeds_root) }
it "returns a fully qualified task name" do
returned_name = Seedbank::DSL.define_seed_task(seed_file, name => dependencies)
-
+
returned_name.must_equal name
end
it "creates a Rake Task" do
Seedbank::DSL.define_seed_task(seed_file, name => dependencies)
-
+
Rake::Task[name].wont_be_nil
end
it "sets Rake Task dependencies" do
Seedbank::DSL.define_seed_task(seed_file, name => dependencies)
-
+
Rake::Task[name].prerequisite_tasks.must_equal dependencies.map { |dependency| Rake::Task[dependency] }
end
it "sets Rake Task description" do
Seedbank::DSL.define_seed_task(seed_file, name => dependencies)
-
+
Rake::Task[name].comment.must_equal "Load the seed data from #{seed_file}"
end
it "sets Rake Task action to the seed_file contents" do
Seedbank::DSL.define_seed_task(seed_file, name => dependencies)
-
+
flexmock(FakeModel).should_receive(:seed).with('development:users').once.ordered
-
+
Rake::Task[name].invoke
end
end
+
+ describe "override_seed_task" do
+
+ let(:arguments) { 'my_task' }
+
+ it "calls Rake::TaskManager#override_task" do
+ block = proc {}
+ flexmock(Rake.application).should_receive(:override_task).with(arguments, block).once
+
+ Seedbank::DSL.override_seed_task(arguments, &block)
+ end
+ end
end
@@ -10,18 +10,18 @@
seeds = %w(db:seed:circular1 db:seed:circular2 db:seed:common db:seed:dependency db:seed:dependency2
db:seed:dependent db:seed:dependent_on_nested db:seed:dependent_on_several db:seed:development
db:seed:development:users db:seed:no_block db:seed:original)
-
+
subject.map(&:to_s).must_equal seeds
end
end
describe "common seeds in the root directory" do
-
+
Dir[File.expand_path('../../../dummy/db/seeds/*.seeds.rb', __FILE__)].each do |seed_file|
seed = File.basename(seed_file, '.seeds.rb')
-
+
describe seed do
-
+
subject { Rake.application.lookup(seed, %w[db seed]) }
it "is dependent on db:abort_if_pending_migrations" do
@@ -32,39 +32,50 @@
end
describe "db:seed:common" do
-
+
subject { Rake::Task['db:seed:common'] }
it "is dependent on the common seeds and db:seed:original" do
prerequisite_seeds = Dir[File.expand_path('../../../dummy/db/seeds/*.seeds.rb', __FILE__)].map do |seed_file|
['db', 'seed', File.basename(seed_file, '.seeds.rb')].join(':')
end.unshift('db:seed:original')
-
+
subject.prerequisites.must_equal prerequisite_seeds
end
end
describe "db:seed:original" do
-
+
subject { Rake::Task['db:seed:original'] }
it "has no dependencies" do
subject.prerequisites.must_be_empty
end
+
+ describe "when seeds are reloaded" do
+
+ before do
+ Dummy::Application.load_tasks
+ end
+
+ it "still has no dependencies" do
+ subject.prerequisites.must_be_empty
+ end
+ end
end
describe "environment seeds" do
-
+
Dir[File.expand_path('../../../dummy/db/seeds', __FILE__) + '/*/'].each do |environment_directory|
environment = File.basename(environment_directory)
-
+
describe "seeds in the #{environment} environment" do
-
+
Dir[File.expand_path("../../../dummy/db/seeds/#{environment}/*.seeds.rb", __FILE__)].each do |seed_file|
seed = File.basename(seed_file, '.seeds.rb')
-
+
describe seed do
-
+
subject { Rake.application.lookup(seed, ['db', 'seed', environment]) }
it "is dependent on db:abort_if_pending_migrations" do
@@ -75,36 +86,36 @@
end
describe "db:seed:#{environment}" do
-
+
subject { Rake.application.lookup(environment, %w[db seed]) }
it "is dependent on the seeds in the environment directory" do
prerequisite_seeds = Dir[File.expand_path("../../../dummy/db/seeds/#{environment}/*.seeds.rb", __FILE__)].map do |seed_file|
['db', 'seed', environment, File.basename(seed_file, '.seeds.rb')].join(':')
end.unshift('db:seed:common')
-
+
subject.prerequisites.must_equal prerequisite_seeds
end
end
end
end
describe "db:seed task" do
-
+
subject { Rake::Task['db:seed'] }
-
+
describe "when no environment seeds are defined" do
-
+
it "is dependent on db:seed:common" do
subject.prerequisites.must_equal %w[db:seed:common]
end
end
-
+
describe "when environment seeds are defined" do
-
+
it "is dependent on db:seed:common" do
flexmock(Rails).should_receive(:env).and_return('development').once
-
+
Rake.application.clear
Dummy::Application.load_tasks

0 comments on commit 2a4d1bf

Please sign in to comment.