diff --git a/lib/seedbank.rb b/lib/seedbank.rb index 2f3d6b5..a058f7f 100644 --- a/lib/seedbank.rb +++ b/lib/seedbank.rb @@ -4,7 +4,7 @@ module Seedbank class << self - attr_writer :application_root, :seeds_root, :nesting, :matcher + attr_writer :application_root, :seeds_root, :nesting, :matcher, :original_seeds_file def application_root @application_root ||= Pathname.new(Rake.application.original_dir) @@ -21,10 +21,14 @@ def nesting def matcher @matcher ||= '*.seeds.rb' end + + def original_seeds_file + @original_seeds_file ||= File.join(application_root, 'db', 'seeds.rb') + end end def self.load_tasks - Dir[File.expand_path('../tasks/*.rake', __FILE__)].each { |ext| load ext } + Dir[File.expand_path('tasks/*.rake', __dir__)].each { |ext| load ext } end require 'seedbank/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3 diff --git a/lib/seedbank/dsl.rb b/lib/seedbank/dsl.rb index 3e8b778..d05498b 100644 --- a/lib/seedbank/dsl.rb +++ b/lib/seedbank/dsl.rb @@ -17,8 +17,9 @@ def seed_tasks_matching(*pattern) end 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(':') + scopes = scope_from_seed_file(seed_file) + suffix = Seedbank.matcher.gsub(/\A\*/, '') + fq_name = scopes.push(File.basename(seed_file, suffix)).join(':') define_seed_task(seed_file, fq_name) end @@ -40,11 +41,11 @@ def define_seed_task(seed_file, *args) end def original_seeds_file - @_seedbank_original ||= existent(Pathname.new('../seeds.rb').expand_path(seeds_root)) + @original_seeds_file ||= existent(original_seeds_file_expanded(Seedbank.original_seeds_file, seeds_root)) end def seeds_root - Pathname.new Seedbank.seeds_root + Pathname.new(Seedbank.seeds_root) end private @@ -71,7 +72,7 @@ def add_environment_dependency(task) end def runner - @_seedbank_runner ||= Seedbank::Runner.new + @runner ||= Seedbank::Runner.new end def add_comment_to(seed_task, comment) @@ -81,6 +82,10 @@ def add_comment_to(seed_task, comment) seed_task.send :instance_variable_set, '@full_comment', comment end end + + def original_seeds_file_expanded(filename, root_dir) + Pathname.new(filename).expand_path(root_dir) + end end end end diff --git a/lib/tasks/seed.rake b/lib/tasks/seed.rake index 1cea8cb..7c876d5 100644 --- a/lib/tasks/seed.rake +++ b/lib/tasks/seed.rake @@ -1,6 +1,12 @@ # frozen_string_literal: true +if defined?(Rails) + initializer = Rails.root.join('config', 'initializers', 'seedbank.rb') + require initializer if initializer.exist? +end + namespace :db do using Seedbank::DSL + override_dependency = ['db:seed:common'] namespace :seed do diff --git a/test/lib/seedbank/dsl_test.rb b/test/lib/seedbank/dsl_test.rb index d15e1ef..e522e77 100644 --- a/test/lib/seedbank/dsl_test.rb +++ b/test/lib/seedbank/dsl_test.rb @@ -4,49 +4,31 @@ using Seedbank::DSL describe Seedbank::DSL do - # TODO: This is private so should really be tested indirectly. - describe 'scope_from_seed_file' do - subject { scope_from_seed_file(seed_file) } - - describe 'in an environment directory' do - let(:seed_file) { File.expand_path('development/users.seeds.rb', Seedbank.seeds_root) } - let(:seed_namespace) { %w[development] } - - it 'returns the enviroment scope' do - subject.must_equal seed_namespace - end - end + describe 'override_seed_task' do + describe 'when no task exists to override' do + let(:task_name) { 'my_task' } + let(:dependencies) { ['db:abort_if_pending_migrations'] } - 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] } + it 'creates a new task' do + Seedbank::DSL.override_seed_task(task_name => dependencies) - it 'returns the nested scope' do - subject.must_equal seed_namespace + Rake::Task[task_name].wont_be_nil end - end - describe 'in seeds root' do - let(:seed_file) { File.expand_path('no_block.seeds.rb', Seedbank.seeds_root) } - - it 'returns an array' do - subject.must_be_instance_of Array - end + it 'applies the dependencies' do + expected_dependencies = dependencies.map { |dependency| Rake::Task[dependency] } + Seedbank::DSL.override_seed_task(task_name => dependencies) - it 'must be empty' do - subject.must_be_empty + Rake::Task[task_name].prerequisite_tasks.must_equal expected_dependencies end - end - end - describe 'seeds_root' do - let(:seeds_root) { '/my/seeds/directory' } + it 'applies the description' do + description = 'Expected Description' + Rake.application.last_description = description - subject { Seedbank::DSL.seeds_root } + Seedbank::DSL.override_seed_task(task_name => dependencies) - it 'returns a Pathname' do - Seedbank.stub(:seeds_root, seeds_root) do - subject.must_equal Pathname.new(seeds_root) + Rake::Task[task_name].full_comment.must_equal description end end end @@ -135,32 +117,61 @@ def define_prerequisite_task end end - describe 'override_seed_task' do - describe 'when no task exists to override' do - let(:task_name) { 'my_task' } - let(:dependencies) { ['db:abort_if_pending_migrations'] } + describe 'seeds_root' do + let(:seeds_root) { '/my/seeds/directory' } - it 'creates a new task' do - Seedbank::DSL.override_seed_task(task_name => dependencies) + subject { Seedbank::DSL.seeds_root } - Rake::Task[task_name].wont_be_nil + it 'returns a Pathname' do + Seedbank.stub(:seeds_root, seeds_root) do + subject.must_equal Pathname.new(seeds_root) end + end + end - it 'applies the dependencies' do - expected_dependencies = dependencies.map { |dependency| Rake::Task[dependency] } - Seedbank::DSL.override_seed_task(task_name => dependencies) + # TODO: This is private so should really be tested indirectly. + describe 'scope_from_seed_file' do + subject { scope_from_seed_file(seed_file) } - Rake::Task[task_name].prerequisite_tasks.must_equal expected_dependencies + describe 'in an environment directory' do + let(:seed_file) { File.expand_path('development/users.seeds.rb', Seedbank.seeds_root) } + let(:seed_namespace) { %w[development] } + + 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] } + + it 'returns the nested scope' do + subject.must_equal seed_namespace end + end - it 'applies the description' do - description = 'Expected Description' - Rake.application.last_description = description + describe 'in seeds root' do + let(:seed_file) { File.expand_path('no_block.seeds.rb', Seedbank.seeds_root) } - Seedbank::DSL.override_seed_task(task_name => dependencies) + it 'returns an array' do + subject.must_be_instance_of Array + end - Rake::Task[task_name].full_comment.must_equal description + it 'must be empty' do + subject.must_be_empty end end end + + describe 'original_seeds_file_expanded' do + subject { original_seeds_file_expanded(filename, root) } + + let(:filename) { '../seeds_original.rb' } + let(:root) { '/my/seeds/directory' } + + it 'returns an expanded path name' do + subject.must_equal Pathname.new('/my/seeds/seeds_original.rb') + end + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index d1b9f20..ff4c1d6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,7 +10,7 @@ Rails.backtrace_cleaner.remove_silencers! -Seedbank.application_root = Pathname.new(File.expand_path('../dummy', __FILE__)) +Seedbank.application_root = Pathname.new(File.expand_path('dummy', __dir__)) class Seedbank::Spec < MiniTest::Spec def setup @@ -18,7 +18,7 @@ def setup Rake.application = Rake::Application.new Dummy::Application.load_tasks Object.const_set :FakeModel, MiniTest::Mock.new - TOPLEVEL_BINDING.eval('self').send(:instance_variable_set, :@_seedbank_runner, Seedbank::Runner.new) + TOPLEVEL_BINDING.eval('self').send(:instance_variable_set, :@runner, Seedbank::Runner.new) end super