Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to use customized file name #71

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/seedbank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

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
Expand Down
15 changes: 10 additions & 5 deletions lib/seedbank/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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\*/, '')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

fq_name = scopes.push(File.basename(seed_file, suffix)).join(':')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


define_seed_task(seed_file, fq_name)
end
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
6 changes: 6 additions & 0 deletions lib/tasks/seed.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# frozen_string_literal: true
if defined?(Rails)
initializer = Rails.root.join('config', 'initializers', 'seedbank.rb')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require initializer if initializer.exist?
end

namespace :db do
using Seedbank::DSL

override_dependency = ['db:seed:common']

namespace :seed do
Expand Down
111 changes: 61 additions & 50 deletions test/lib/seedbank/dsl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

let(:seed_namespace) { %w[development] }

it 'returns the enviroment scope' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

subject.must_equal seed_namespace
end
end

describe 'in a nested directory' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

let(:seed_file) { File.expand_path('development/shared/accounts.seeds.rb', Seedbank.seeds_root) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

let(:seed_namespace) { %w[development shared] }

it 'returns the nested scope' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

let(:seed_file) { File.expand_path('no_block.seeds.rb', Seedbank.seeds_root) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


Seedbank::DSL.override_seed_task(task_name => dependencies)
it 'returns an array' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

subject.must_be_instance_of Array
end

Rake::Task[task_name].full_comment.must_equal description
it 'must be empty' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

subject.must_be_empty
end
end
end

describe 'original_seeds_file_expanded' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

subject { original_seeds_file_expanded(filename, root) }

let(:filename) { '../seeds_original.rb' }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

let(:root) { '/my/seeds/directory' }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


it 'returns an expanded path name' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

subject.must_equal Pathname.new('/my/seeds/seeds_original.rb')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end
end
end
4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

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__))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


class Seedbank::Spec < MiniTest::Spec
def setup
silence_warnings do
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end

super
Expand Down