Skip to content

Commit

Permalink
determine fixtures_path in a standard, DRY, backward_compatible, hope…
Browse files Browse the repository at this point in the history
…fully foolproof way
  • Loading branch information
thewoolleyman committed Dec 24, 2016
1 parent 598dab3 commit 7b687d7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/fixture_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'fixture_builder/configuration'
require 'fixture_builder/namer'
require 'fixture_builder/builder'
require 'fixture_builder/fixtures_path'

module FixtureBuilder
class << self
Expand Down
6 changes: 1 addition & 5 deletions lib/fixture_builder/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def tables
end

def fixture_directory
@fixture_directory ||= File.expand_path(File.join(::Rails.root, spec_or_test_dir, 'fixtures'))
@fixture_directory ||= FixturesPath.absolute_rails_fixtures_path
end

def fixtures_dir(path = '')
Expand All @@ -106,10 +106,6 @@ def fixtures_dir(path = '')

private

def spec_or_test_dir
File.exists?(File.join(::Rails.root, 'spec')) ? 'spec' : 'test'
end

def file_hashes
files_to_check.inject({}) do |hash, filename|
hash[filename] = Digest::MD5.hexdigest(File.read(filename))
Expand Down
15 changes: 15 additions & 0 deletions lib/fixture_builder/fixtures_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module FixtureBuilder
class FixturesPath
def self.absolute_rails_fixtures_path
File.expand_path(ActiveRecord::Tasks::DatabaseTasks.fixtures_path)
rescue
if ENV["FIXTURES_PATH"]
ENV["FIXTURES_PATH"]
elsif File.exists?(File.expand_path("spec/fixtures",::Rails.root))
File.expand_path("spec/fixtures",::Rails.root)
else
File.expand_path("test/fixtures",::Rails.root)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/tasks/fixture_builder.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ namespace :spec do
desc "Deletes the generated fixtures in spec/fixtures"
task :clean do
FileUtils.rm_f("tmp/fixture_builder.yml")
FileUtils.rm_f(Dir.glob('spec/fixtures/*.yml'))
FileUtils.rm_f(Dir.glob("#{FixtureBuilder::FixturesPath.absolute_rails_fixtures_path}/*.yml"))
puts "Automatically generated fixtures removed"
end

desc "Build the generated fixtures to spec/fixtures if dirty"
task :build => :environment do
ActiveRecord::Base.establish_connection(:test)
Dir.glob(File.join(Rails.root, '{spec,test}', '**', 'fixture_builder.rb')).each{|file| require(file)}
Dir.glob(File.join(::Rails.root, '{spec,test}', '**', 'fixture_builder.rb')).each{|file| require(file)}
end

desc "Clean and rebuild the generated fixtures to spec/fixtures"
Expand Down
4 changes: 2 additions & 2 deletions test/fixture_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def test_configure
assert @called
end

def test_spec_or_test_dir
assert_equal 'test', FixtureBuilder.configuration.send(:spec_or_test_dir)
def test_absolute_rails_fixtures_path
assert_equal File.expand_path('../../test/fixtures', __FILE__), FixtureBuilder::FixturesPath.absolute_rails_fixtures_path
end

def test_fixtures_dir
Expand Down

0 comments on commit 7b687d7

Please sign in to comment.