Skip to content

Commit

Permalink
Don't load AR integration if it is unavailable (#188)
Browse files Browse the repository at this point in the history
When writing tests for a project that uses certain parts of Rails, it
should still be possible to say `require "super_diff/rails"`, and
SuperDiff should only load the integrations that correspond to the
available parts. Right now the only two integrations are ActiveSupport
and ActiveRecord. ActiveSupport should always be available, but
ActiveRecord may not be available, so this commit adds a guard around
that integration in particular. It also adds tests for Combustion so we
can test that this guard specifically works for engines where
ActiveRecord is not being loaded.

Co-authored-by: Harry Lascelles <harry@harryl.com>
Co-authored-by: sshaw <skye.shaw@gmail.com>
  • Loading branch information
3 people authored Mar 26, 2023
1 parent 628796a commit 37cedd6
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gemfiles/*.gemfile.lock
node_modules
pkg
spec/examples.txt
spec/internal
tmp
zeus.server.log
zeus.server-start.log
Expand Down
10 changes: 4 additions & 6 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,29 @@ rails_dependencies =
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby
gem "jdbc-sqlite3", platform: :jruby
gem "net-ftp"
gem "combustion"
end

appraisals = {
rails_6_0:
proc do
instance_eval(&rails_dependencies)

gem "activerecord", "~> 6.0.0"
gem "railties", "~> 6.0.0"
gem "rails", "~> 6.0.0"
gem "sqlite3", "~> 1.4.0", platform: %i[ruby mswin mingw]
end,
rails_6_1:
proc do
instance_eval(&rails_dependencies)

gem "activerecord", "~> 6.1.0"
gem "railties", "~> 6.1.0"
gem "rails", "~> 6.1.0"
gem "sqlite3", "~> 1.4.0", platform: %i[ruby mswin mingw]
end,
rails_7_0:
proc do
instance_eval(&rails_dependencies)

gem "activerecord", "~> 7.0.0"
gem "railties", "~> 7.0.0"
gem "rails", "~> 7.0.0"
gem "sqlite3", "~> 1.4.0", platform: %i[ruby mswin mingw]
end,
no_rails: proc {},
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_6_0_rspec_gte_3_10.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ gem "warnings_logger"
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby
gem "jdbc-sqlite3", platform: :jruby
gem "net-ftp"
gem "activerecord", "~> 6.0.0"
gem "railties", "~> 6.0.0"
gem "combustion"
gem "rails", "~> 6.0.0"
gem "sqlite3", "~> 1.4.0", platform: [:ruby, :mswin, :mingw]
gem "rspec", ">= 3.10", "< 4"
gem "rspec-rails"
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_6_0_rspec_lt_3_10.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ gem "warnings_logger"
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby
gem "jdbc-sqlite3", platform: :jruby
gem "net-ftp"
gem "activerecord", "~> 6.0.0"
gem "railties", "~> 6.0.0"
gem "combustion"
gem "rails", "~> 6.0.0"
gem "sqlite3", "~> 1.4.0", platform: [:ruby, :mswin, :mingw]
gem "rspec", "~> 3.9.0"
gem "rspec-rails"
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_6_1_rspec_gte_3_10.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ gem "warnings_logger"
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby
gem "jdbc-sqlite3", platform: :jruby
gem "net-ftp"
gem "activerecord", "~> 6.1.0"
gem "railties", "~> 6.1.0"
gem "combustion"
gem "rails", "~> 6.1.0"
gem "sqlite3", "~> 1.4.0", platform: [:ruby, :mswin, :mingw]
gem "rspec", ">= 3.10", "< 4"
gem "rspec-rails"
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_6_1_rspec_lt_3_10.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ gem "warnings_logger"
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby
gem "jdbc-sqlite3", platform: :jruby
gem "net-ftp"
gem "activerecord", "~> 6.1.0"
gem "railties", "~> 6.1.0"
gem "combustion"
gem "rails", "~> 6.1.0"
gem "sqlite3", "~> 1.4.0", platform: [:ruby, :mswin, :mingw]
gem "rspec", "~> 3.9.0"
gem "rspec-rails"
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_7_0_rspec_gte_3_10.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ gem "warnings_logger"
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby
gem "jdbc-sqlite3", platform: :jruby
gem "net-ftp"
gem "activerecord", "~> 7.0.0"
gem "railties", "~> 7.0.0"
gem "combustion"
gem "rails", "~> 7.0.0"
gem "sqlite3", "~> 1.4.0", platform: [:ruby, :mswin, :mingw]
gem "rspec", ">= 3.10", "< 4"
gem "rspec-rails"
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_7_0_rspec_lt_3_10.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ gem "warnings_logger"
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby
gem "jdbc-sqlite3", platform: :jruby
gem "net-ftp"
gem "activerecord", "~> 7.0.0"
gem "railties", "~> 7.0.0"
gem "combustion"
gem "rails", "~> 7.0.0"
gem "sqlite3", "~> 1.4.0", platform: [:ruby, :mswin, :mingw]
gem "rspec", "~> 3.9.0"
gem "rspec-rails"
Expand Down
3 changes: 2 additions & 1 deletion lib/super_diff/rails.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require "super_diff/active_record"
require "super_diff/active_support"
require "super_diff/active_record" if defined?(ActiveRecord)
20 changes: 20 additions & 0 deletions spec/integration/rails/engines_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "spec_helper"

RSpec.describe "Integration with Rails engines", type: :integration do
context "when ActiveRecord is not loaded via Combustion" do
it "does not fail to load" do
as_both_colored_and_uncolored do |color_enabled|
program =
make_rspec_rails_engine_program(
"expect(true).to be(true)",
combustion_initialize: [:action_controller],
color_enabled: color_enabled
)

expect(program).not_to produce_output_when_run(
"uninitialized constant ActiveRecord"
).in_color(color_enabled)
end
end
end
end
12 changes: 12 additions & 0 deletions spec/support/integration/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ def make_rspec_rails_test_program(test, color_enabled:)
TestPrograms::RSpecRails.new(test, color_enabled: color_enabled)
end

def make_rspec_rails_engine_program(
test,
color_enabled:,
combustion_initialize:
)
TestPrograms::RspecRailsEngine.new(
test,
color_enabled: color_enabled,
combustion_initialize: combustion_initialize
)
end

def build_expected_output(
color_enabled:,
snippet:,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ def failure_message
end
end

def failure_message_when_negated
"Expected output of test not to produce output, but it did.\n\n" +
"Actual output:\n\n" +
SuperDiff::Test::OutputHelpers.bookended(actual_output)
end

private

attr_reader :expected_output, :program, :output_processor
Expand Down
1 change: 0 additions & 1 deletion spec/support/integration/test_programs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def program
color_enabled: #{color_enabled?.inspect},
configuration: #{configuration.inspect}
)
test_plan.boot
#{test_plan_prelude}
test_plan.#{test_plan_command}
Expand Down
6 changes: 6 additions & 0 deletions spec/support/integration/test_programs/plain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module TestPrograms
class Plain < Base
protected

def test_plan_prelude
<<~PRELUDE.strip
test_plan.boot
PRELUDE
end

def test_plan_command
"run_plain_test"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class RSpecActiveRecord < Base
protected

def test_plan_prelude
"test_plan.boot_active_record"
<<~PRELUDE.strip
test_plan.boot
test_plan.boot_active_record
PRELUDE
end

def test_plan_command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class RSpecActiveSupport < Base
protected

def test_plan_prelude
"test_plan.boot_active_support"
<<~PRELUDE.strip
test_plan.boot
test_plan.boot_active_support
PRELUDE
end

def test_plan_command
Expand Down
5 changes: 4 additions & 1 deletion spec/support/integration/test_programs/rspec_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class RSpecRails < Base
protected

def test_plan_prelude
"test_plan.boot_active_record"
<<~PRELUDE.strip
test_plan.boot
test_plan.boot_active_record
PRELUDE
end

def test_plan_command
Expand Down
30 changes: 30 additions & 0 deletions spec/support/integration/test_programs/rspec_rails_engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module SuperDiff
module IntegrationTests
module TestPrograms
class RspecRailsEngine < Base
def initialize(*args, combustion_initialize:, **options)
super(*args, **options)
@combustion_initialize = combustion_initialize
end

protected

def test_plan_prelude
<<~PRELUDE.strip
test_plan.boot_rails_engine(
combustion_initialize: #{combustion_initialize.inspect}
)
PRELUDE
end

def test_plan_command
"run_rspec_rails_test"
end

private

attr_reader :combustion_initialize
end
end
end
end
5 changes: 5 additions & 0 deletions support/test_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ def boot_rails
boot_active_record
end

def boot_rails_engine(combustion_initialize: [])
require "combustion"
Combustion.initialize!(*combustion_initialize)
end

def run_plain_test
run_test_with_libraries("super_diff/rspec")
end
Expand Down
3 changes: 2 additions & 1 deletion zeus.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"boot_rails": {
"run_rspec_rails_test": []
}
}
},
"boot_rails_engine": []
}
}

0 comments on commit 37cedd6

Please sign in to comment.