Skip to content

Commit

Permalink
Simple spec refactoring to extract fixture path helper (#20)
Browse files Browse the repository at this point in the history
I noticed while doing another PR (to be opened shortly!) that there is a
bit of duplication in the specs around generating fixture paths, so this
simple refactoring just extracts a `fixture_path` method to DRY it up.
  • Loading branch information
joehorsnell authored and jarmo committed Feb 12, 2018
1 parent 32ac77f commit 53f8ee3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
26 changes: 12 additions & 14 deletions spec/autoload_spec.rb
Expand Up @@ -4,39 +4,38 @@
describe "autoload_all" do

subject { self }

it "provides require_all functionality by using 'autoload' instead of 'require'" do
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
autoload_all File.dirname(__FILE__) + "/fixtures/autoloaded"
autoload_all fixture_path('autoloaded')
is_expected.to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
end

it "doesn't autoload files with wrong module names" do
autoload_all File.dirname(__FILE__) + "/fixtures/autoloaded"
autoload_all fixture_path('autoloaded')
is_expected.not_to be_loaded("Autoloaded::WrongModule::WithWrongModule", "WrongModule::WithWrongModule")
end

it "autoloads class nested into another class" do
is_expected.not_to be_loaded("Autoloaded::Class1", "Autoloaded::Class1::C")
autoload_all File.dirname(__FILE__) + "/fixtures/autoloaded"
autoload_all fixture_path('autoloaded')
is_expected.to be_loaded("Autoloaded::Class1")
expect(Autoloaded::Class1).to be_a Class
is_expected.to be_loaded("Autoloaded::Class1::C")
end

it "needs to specify base_dir for autoloading if loading something from under top-level module directory" do
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
autoload_all File.dirname(__FILE__) + "/fixtures/autoloaded/module1"
autoload_all fixture_path('autoloaded/module1')
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")

autoload_all File.dirname(__FILE__) + "/fixtures/autoloaded/module1",
base_dir: File.dirname(__FILE__) + "/fixtures/autoloaded"
autoload_all fixture_path('autoloaded/module1'), base_dir: fixture_path('autoloaded')
is_expected.to be_loaded("Autoloaded::Module1::A")
is_expected.not_to be_loaded("Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
end

before(:all) do
@base_dir = File.dirname(__FILE__) + '/fixtures/autoloaded'
@base_dir = fixture_path('autoloaded')
@method = :autoload_all
@autoload_base_dir = @base_dir
end
Expand All @@ -49,25 +48,24 @@

it "provides autoload_all functionality relative to the current file" do
is_expected.not_to be_loaded("Modules::Module1::First", "Modules::Module2::Second", "Modules::Zero")
require File.dirname(__FILE__) + '/fixtures/autoloaded_rel/modules/zero'
require fixture_path('autoloaded_rel/modules/zero')
is_expected.to be_loaded("Modules::Module1::First", "Modules::Module2::Second", "Modules::Zero")
end

it "needs to specify base_dir for autoloading if loading something from under top-level module directory" do
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
autoload_rel "./fixtures/autoloaded/module1"
autoload_rel relative_fixture_path('autoloaded/module1')
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")

autoload_rel "./fixtures/autoloaded/module1",
base_dir: File.dirname(__FILE__) + "/fixtures/autoloaded"
autoload_rel relative_fixture_path('autoloaded/module1'), base_dir: fixture_path('autoloaded')
is_expected.to be_loaded("Autoloaded::Module1::A")
is_expected.not_to be_loaded("Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
end

before(:all) do
@base_dir = './fixtures/autoloaded'
@base_dir = relative_fixture_path('autoloaded')
@method = :autoload_rel
@autoload_base_dir = File.dirname(__FILE__) + "/fixtures/autoloaded"
@autoload_base_dir = fixture_path('autoloaded')
end
it_should_behave_like "#autoload_all syntactic sugar"
end
12 changes: 6 additions & 6 deletions spec/load_spec.rb
Expand Up @@ -2,11 +2,11 @@
require File.dirname(__FILE__) + '/require_shared.rb'

describe "load_all" do

subject { self }

it "provides require_all functionality but using 'load' instead of 'require'" do
require_all File.dirname(__FILE__) + '/fixtures/resolvable'
require_all fixture_path('resolvable')
expect(C.new).to be_cool

class C
Expand All @@ -18,12 +18,12 @@ def cool?
expect(C.new).not_to be_cool
C.send :remove_method, :cool?

load_all File.dirname(__FILE__) + '/fixtures/resolvable'
load_all fixture_path('resolvable')
expect(C.new).to be_cool
end

before(:all) do
@base_dir = File.dirname(__FILE__) + '/fixtures/autoloaded'
@base_dir = fixture_path('autoloaded')
@method = :load_all
end
it_should_behave_like "#require_all syntactic sugar"
Expand All @@ -34,7 +34,7 @@ def cool?
subject { self }

it "provides load_all functionality relative to the current file" do
require File.dirname(__FILE__) + '/fixtures/relative/d/d'
require fixture_path('relative/d/d')

is_expected.to be_loaded("RelativeA", "RelativeC", "RelativeD")
expect(RelativeD.new).to be_ok
Expand All @@ -48,7 +48,7 @@ def ok?
expect(RelativeD.new).not_to be_ok
RelativeD.send :remove_method, :ok?

load File.dirname(__FILE__) + '/fixtures/relative/d/d.rb'
load fixture_path('relative/d/d.rb')
expect(RelativeD.new).to be_ok
end

Expand Down
10 changes: 5 additions & 5 deletions spec/require_spec.rb
Expand Up @@ -7,20 +7,20 @@

describe "dependency resolution" do
it "handles load ordering when dependencies are resolvable" do
require_all File.dirname(__FILE__) + '/fixtures/resolvable/*.rb'
require_all fixture_path('resolvable/*.rb')

is_expected.to be_loaded("A", "B", "C", "D")
end

it "raises NameError if dependencies can't be resolved" do
expect do
require_all File.dirname(__FILE__) + '/fixtures/unresolvable/*.rb'
require_all fixture_path('unresolvable/*.rb')
end.to raise_error(NameError)
end
end

before(:all) do
@base_dir = File.dirname(__FILE__) + '/fixtures/autoloaded'
@base_dir = fixture_path('autoloaded')
@method = :require_all
end
it_should_behave_like "#require_all syntactic sugar"
Expand All @@ -31,14 +31,14 @@
subject { self }

it "provides require_all functionality relative to the current file" do
require File.dirname(__FILE__) + '/fixtures/relative/b/b'
require fixture_path('relative/b/b')

is_expected.to be_loaded("RelativeA", "RelativeB", "RelativeC")
is_expected.not_to be_loaded("RelativeD")
end

before(:all) do
@base_dir = './fixtures/autoloaded'
@base_dir = relative_fixture_path('autoloaded')
@method = :require_rel
end
it_should_behave_like "#require_all syntactic sugar"
Expand Down
10 changes: 9 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -2,11 +2,19 @@
require "coveralls"

SimpleCov.formatter = Coveralls::SimpleCov::Formatter
SimpleCov.start
SimpleCov.start

require File.dirname(__FILE__) + '/../lib/require_all.rb'

module SpecHelper
def fixture_path(fixture_name, relative_dir = File.dirname(__FILE__))
File.join(relative_dir, 'fixtures', fixture_name)
end

def relative_fixture_path(fixture_name)
fixture_path(fixture_name, '.')
end

def unload_all
%w{A B C D WrongModule Autoloaded Modules
RelativeA RelativeB RelativeC RelativeD}.each do |const|
Expand Down

0 comments on commit 53f8ee3

Please sign in to comment.