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

RSpec initialiser #25

Merged
merged 2 commits into from
Oct 26, 2023
Merged
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
11 changes: 10 additions & 1 deletion lib/kangaru.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@
require "yaml"

module Kangaru
@loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false).tap(&:setup)
INFLECTIONS = {
"rspec" => "RSpec"
}.freeze

@loader = Zeitwerk::Loader.for_gem(
warn_on_extra_files: false
).tap do |loader|
loader.inflector.inflect(INFLECTIONS)
loader.setup
end

class << self
attr_accessor :application
Expand Down
11 changes: 11 additions & 0 deletions lib/kangaru/initialisers/rspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Kangaru
module Initialisers
module RSpec
if Object.const_defined?(:RSpec)
::RSpec.configure do
Kangaru.env = :test
end
end
end
end
end
1 change: 1 addition & 0 deletions sig/kangaru.rbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Kangaru
VERSION: String
INFLECTIONS: Hash[String, String]

extend ClassMethods

Expand Down
4 changes: 0 additions & 4 deletions sig/kangaru/initialisers.rbs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module Kangaru
module Initialisers
module RSpec
end
end
end
156 changes: 91 additions & 65 deletions spec/features/initialiser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,88 +10,114 @@ class Foobar
RUBY
end

context "when the target gem does not extend the initialiser" do
before do
gem.main_file.write(<<~RUBY)
require "kangaru"
describe "initialising application" do
context "when the target gem does not extend the initialiser" do
before do
gem.main_file.write(<<~RUBY)
require "kangaru"

module SomeGem
end
RUBY
end

module SomeGem
end
RUBY
end
it "loads the gem module" do
expect { require_gem }
.to change { Object.const_defined?(:SomeGem) }
.from(false)
.to(true)
end

it "loads the gem module" do
expect { require_gem }
.to change { Object.const_defined?(:SomeGem) }
.from(false)
.to(true)
end
it "does not autoload gem files" do
expect { require_gem }
.not_to change { Object.const_defined?("SomeGem::Foobar") }
.from(false)
end

it "does not autoload gem files" do
expect { require_gem }
.not_to change { Object.const_defined?("SomeGem::Foobar") }
.from(false)
end
it "does not set the Kangaru application reference" do
expect { require_gem }.not_to change { Kangaru.application }.from(nil)
end

it "does not set the Kangaru application reference" do
expect { require_gem }.not_to change { Kangaru.application }.from(nil)
it "does not define the run! method in the gem's root module" do
require_gem
expect(SomeGem).not_to respond_to(:run!)
end
end

it "does not define the run! method in the gem's root module" do
require_gem
expect(SomeGem).not_to respond_to(:run!)
end
end
context "when the target gem extends the initialiser" do
before do
gem.main_file.write(<<~RUBY)
require "kangaru"

context "when the target gem extends the initialiser" do
before do
gem.main_file.write(<<~RUBY)
require "kangaru"
module SomeGem
extend Kangaru::Initialiser
end
RUBY
end

module SomeGem
extend Kangaru::Initialiser
end
RUBY
end
it "loads the gem module" do
expect { require_gem }
.to change { Object.const_defined?("SomeGem") }
.from(false)
.to(true)
end

it "loads the gem module" do
expect { require_gem }
.to change { Object.const_defined?("SomeGem") }
.from(false)
.to(true)
end
it "autoloads gem files" do
expect { require_gem }
.to change { Object.const_defined?("SomeGem::Foobar") }
.from(false)
.to(true)
end

it "autoloads gem files" do
expect { require_gem }
.to change { Object.const_defined?("SomeGem::Foobar") }
.from(false)
.to(true)
end
it "sets the Kangaru application reference" do
expect { require_gem }
.to change { Kangaru.application }
.from(nil)
.to(a_kind_of(Kangaru::Application))
end

it "sets the Kangaru application reference" do
expect { require_gem }
.to change { Kangaru.application }
.from(nil)
.to(a_kind_of(Kangaru::Application))
end
it "defines the run! method in the gem's root module" do
require_gem
expect(SomeGem).to respond_to(:run!)
end

it "defines the run! method in the gem's root module" do
require_gem
expect(SomeGem).to respond_to(:run!)
end
describe "application reference" do
subject(:application) { Kangaru.application }

describe "application reference" do
subject(:application) { Kangaru.application }
before { require_gem }

before { require_gem }
it "sets the application source to the gem main file" do
expect(application.paths.source).to eq(gem.main_file)
end

it "sets the application source to the gem main file" do
expect(application.paths.source).to eq(gem.main_file)
it "sets the application name to the expected value" do
expect(application.paths.name).to eq("some_gem")
end
end
end
end

it "sets the application name to the expected value" do
expect(application.paths.name).to eq("some_gem")
end
describe "setting test environment in RSpec" do
subject(:run_test) { gem.exec("rspec #{test_file}") }

let(:test_file) { gem.spec_path("test_spec") }

before do
gem.spec_path.mkdir unless gem.spec_path.exist?

test_file.write(<<~RUBY)
RSpec.describe do
it "sets the test environment" do
expect(Kangaru).to be_test
end
end
RUBY
end

include_context :kangaru_initialised

it "configures Kangaru to the test environment" do
expect(run_test).not_to include("FAILED")
end
end
end