Skip to content

Commit

Permalink
Fix find_definitions to expand the path instead of always prepending .
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Jul 22, 2011
1 parent d175bf4 commit 17f3ef4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
15 changes: 15 additions & 0 deletions features/find_definitions.feature
Expand Up @@ -14,6 +14,21 @@ Feature: Factory girl can find factory definitions correctly
| name |
| awesome!!! |

Scenario: Find definitions with an absolute path
Given a file named "awesome_factories.rb" with:
"""
FactoryGirl.define do
factory :awesome_category, :parent => :category do
name "awesome!!!"
end
end
"""
When "awesome_factories.rb" is added to Factory Girl's file definitions path as an absolute path
And I create a "awesome_category" instance from Factory Girl
Then I should find the following for the last category:
| name |
| awesome!!! |

Scenario: Find definitions with a folder
Given a file named "nested/great_factories.rb" with:
"""
Expand Down
8 changes: 8 additions & 0 deletions features/step_definitions/factory_girl_steps.rb
Expand Up @@ -5,6 +5,14 @@
FactoryGirl.find_definitions
end

When /^"([^"]*)" is added to Factory Girl's file definitions path as an absolute path$/ do |file_name|
new_factory_file = File.expand_path(File.join(current_dir, file_name.gsub(".rb", "")))

FactoryGirl.definition_file_paths ||= []
FactoryGirl.definition_file_paths << new_factory_file
FactoryGirl.find_definitions
end

When /^I create a "([^"]*)" instance from Factory Girl$/ do |factory_name|
FactoryGirl.create(factory_name)
end
6 changes: 4 additions & 2 deletions lib/factory_girl/find_definitions.rb
Expand Up @@ -11,11 +11,13 @@ class << self

def self.find_definitions #:nodoc:
definition_file_paths.each do |path|
require("./#{path}.rb") if File.exists?("#{path}.rb")
path = File.expand_path(path)

require("#{path}.rb") if File.exists?("#{path}.rb")

if File.directory? path
Dir[File.join(path, '**', '*.rb')].sort.each do |file|
require "./#{file}"
require file
end
end
end
Expand Down
22 changes: 11 additions & 11 deletions spec/factory_girl/find_definitions_spec.rb
Expand Up @@ -10,7 +10,7 @@

RSpec::Matchers.define :require_definitions_from do |file|
match do |given|
@has_received = have_received.method_missing(:require, file)
@has_received = have_received.method_missing(:require, File.expand_path(file))
@has_received.matches?(given)
end

Expand Down Expand Up @@ -47,31 +47,31 @@ def self.in_directory_with_files(*files)
describe "with factories.rb" do
in_directory_with_files 'factories.rb'
it_should_behave_like "finds definitions" do
it { should require_definitions_from('./factories.rb') }
it { should require_definitions_from('factories.rb') }
end
end

%w(spec test).each do |dir|
describe "with a factories file under #{dir}" do
in_directory_with_files File.join(dir, 'factories.rb')
it_should_behave_like "finds definitions" do
it { should require_definitions_from("./#{dir}/factories.rb") }
it { should require_definitions_from("#{dir}/factories.rb") }
end
end

describe "with a factories file under #{dir}/factories" do
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb')
it_should_behave_like "finds definitions" do
it { should require_definitions_from("./#{dir}/factories/post_factory.rb") }
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
end
end

describe "with several factories files under #{dir}/factories" do
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb'),
File.join(dir, 'factories', 'person_factory.rb')
it_should_behave_like "finds definitions" do
it { should require_definitions_from("./#{dir}/factories/post_factory.rb") }
it { should require_definitions_from("./#{dir}/factories/person_factory.rb") }
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
end
end

Expand All @@ -91,18 +91,18 @@ def self.in_directory_with_files(*files)
File.join(dir, 'factories', 'post_factory.rb'),
File.join(dir, 'factories', 'person_factory.rb')
it_should_behave_like "finds definitions" do
it { should require_definitions_from("./#{dir}/factories.rb") }
it { should require_definitions_from("./#{dir}/factories/post_factory.rb") }
it { should require_definitions_from("./#{dir}/factories/person_factory.rb") }
it { should require_definitions_from("#{dir}/factories.rb") }
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
end
end

describe "with deeply nested factory files under #{dir}" do
in_directory_with_files File.join(dir, 'factories', 'subdirectory', 'post_factory.rb'),
File.join(dir, 'factories', 'subdirectory', 'person_factory.rb')
it_should_behave_like "finds definitions" do
it { should require_definitions_from("./#{dir}/factories/subdirectory/post_factory.rb") }
it { should require_definitions_from("./#{dir}/factories/subdirectory/person_factory.rb") }
it { should require_definitions_from("#{dir}/factories/subdirectory/post_factory.rb") }
it { should require_definitions_from("#{dir}/factories/subdirectory/person_factory.rb") }
end
end
end
Expand Down

0 comments on commit 17f3ef4

Please sign in to comment.