Skip to content
Browse files

Refactored Seedbank::DSL and added tests.

Signed-off-by: James McCarthy <james2mccarthy@gmail.com>
  • Loading branch information...
1 parent dbcbb2e commit ae5a451ac7669ae6280f6fa75968f69dd292af4a @james2m committed Jul 20, 2012
Showing with 113 additions and 40 deletions.
  1. +14 −10 lib/seedbank/dsl.rb
  2. +24 −22 lib/tasks/seed.rake
  3. +1 −0 test/dummy/db/seeds/development/users.seeds.rb
  4. +74 −6 test/seedbank/dsl_test.rb
  5. +0 −2 test/test_helper.rb
View
24 lib/seedbank/dsl.rb
@@ -14,28 +14,32 @@ def override_task(*args, &block)
Rake::Task.define_task(*args, &block)
end
- # Creates a task namespaced in @seeds_path
- def define_seed_task(seed_file)
- relative_root = seed_file.sub(seeds_root.to_s + '/', '')
- scopes = File.dirname(relative_root).gsub(/^\./, '').split('/').unshift('seed')
+ def seed_task_from_file(seed_file)
+ scopes = scope_from_seed_file(seed_file)
fq_name = scopes.push(File.basename(seed_file, '.seeds.rb')).join(':')
+ args = Rake::Task.task_defined?('db:abort_if_pending_migrations') ? { fq_name => 'db:abort_if_pending_migrations' } : fq_name
- args = Rake::Task.task_defined?('db:abort_if_pending_migrations') ? { fq_name => 'db:abort_if_pending_migrations' } : fq_name
- task = Rake::Task.define_task(args) do |seed_task|
+ define_seed_task(seed_file, args)
+ end
+
+ def define_seed_task(seed_file, *args)
+ task = Rake::Task.define_task(*args) do |seed_task|
Seedbank::Runner.new(seed_task).module_eval(File.read(seed_file)) if File.exist?(seed_file)
end
task.add_description "Load the seed data from #{seed_file}"
- fq_name
+ task.name
end
def scope_from_seed_file(seed_file)
- pathname = Pathname.new(seed_file).relative_path_from(seeds_root)
- pathname.dirname.to_s.gsub(File::Separator, ':')
+ dirname = Pathname.new(seed_file).dirname
+ return [] if dirname == seeds_root
+ relative = dirname.relative_path_from(seeds_root)
+ relative.to_s.split(File::Separator)
end
def seeds_root
Pathname.new Seedbank.seeds_root
end
end
-end
+end
View
46 lib/tasks/seed.rake
@@ -5,33 +5,35 @@ namespace :db do
base_dependencies = ['db:seed:original']
override_dependency = []
common_dependencies = []
+
+ 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.
+ Dir.glob(File.join(seeds_root, '*.seeds.rb')).each do |seed_file|
+ common_dependencies << seed_task_from_file(seed_file)
+ end
- # Create seed tasks for all the seeds in seeds_path and add them to the dependency
- # list along with the original db/seeds.rb.
- Dir.glob(File.join(seeds_root, '*.seeds.rb')).each do |seed_file|
- common_dependencies << define_seed_task(seed_file)
- end
+ desc "Load the seed data from db/seeds.rb and db/seeds/*.seeds.rb."
+ task 'common' => base_dependencies + common_dependencies
- desc "Load the seed data from db/seeds.rb and db/seeds/*.seeds.rb."
- task ['seed', 'common'] => base_dependencies + common_dependencies
+ # Glob through the directories under seeds_path assuming they are all environments
+ # and create a task for each and add it to the dependency list. Then create a task
+ # for the environment
+ Dir.glob(File.join(seeds_root, '/*/')).each do |e|
+ environment = File.basename(e)
- # Glob through the directories under seeds_path assuming they are all environments
- # and create a task for each and add it to the dependency list. Then create a task
- # for the environment
- Dir[seeds_root + '/*/'].each do |e|
- environment = File.basename(e)
+ environment_dependencies = []
+ Dir.glob(File.join(seeds_root, environment, '*.seeds.rb')).sort.each do |seed_file|
+ environment_dependencies << seed_task_from_file(seed_file)
+ end
- environment_dependencies = []
- Dir.glob(File.join(seeds_root, environment, '*.seeds.rb')).sort.each do |seed_file|
- environment_dependencies << define_seed_task(seed_file)
- end
+ desc <<-EOT
+ Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/#{environment}/*.seeds.rb.
+ EOT
+ task environment => ['db:seed:common'] + environment_dependencies
- desc <<-EOT
- Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/#{environment}/*.seeds.rb.
- EOT
- task ['seed', environment] => ['db:seed:common'] + environment_dependencies
-
- override_dependency << "db:seed:#{environment}" if Rails.env == environment
+ override_dependency << "db:seed:#{environment}" if Rails.env == environment
+ end
end
# Change db:seed task to run all the base seeds tasks defined above.
View
1 test/dummy/db/seeds/development/users.seeds.rb
@@ -0,0 +1 @@
+FakeModel.seed('development:users')
View
80 test/seedbank/dsl_test.rb
@@ -10,29 +10,97 @@
ns.must_respond_to :scope_from_seed_file
end
- describe "with an environment directory" do
+ describe "in an environment directory" do
let(:seed_file) { File.expand_path('development/users.seeds.rb', Seedbank.seeds_root) }
- let(:seed_namespace) { 'development' }
+ let(:seed_namespace) { %w(development) }
- subject {Seedbank::DSL.scope_from_seed_file seed_file }
+ subject { Seedbank::DSL.scope_from_seed_file seed_file }
it "returns the enviroment scope" do
subject.must_equal seed_namespace
end
end
- describe "with a nested directory" do
+ describe "in a nested directory" do
let(:seed_file) { File.expand_path('development/shared/accounts.seeds.rb', Seedbank.seeds_root) }
- let(:seed_namespace) { 'development:shared' }
+ let(:seed_namespace) { %w(development shared) }
- subject {Seedbank::DSL.scope_from_seed_file seed_file }
+ 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 "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
end
View
2 test/test_helper.rb
@@ -24,6 +24,4 @@ def setup
end
-
-
MiniTest::Spec.register_spec_type(/^Seedbank::/, Seedbank::Spec)

0 comments on commit ae5a451

Please sign in to comment.
Something went wrong with that request. Please try again.