From b86f80756347a07887286b3b09b1b1883cdd10d2 Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Sat, 25 Mar 2023 16:48:16 -0600 Subject: [PATCH] Don't load AR integration if it is unavailable 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 Co-authored-by: sshaw --- .gitignore | 1 + Appraisals | 10 +++---- gemfiles/rails_6_0_rspec_gte_3_10.gemfile | 4 +-- gemfiles/rails_6_0_rspec_lt_3_10.gemfile | 4 +-- gemfiles/rails_6_1_rspec_gte_3_10.gemfile | 4 +-- gemfiles/rails_6_1_rspec_lt_3_10.gemfile | 4 +-- gemfiles/rails_7_0_rspec_gte_3_10.gemfile | 4 +-- gemfiles/rails_7_0_rspec_lt_3_10.gemfile | 4 +-- lib/super_diff/rails.rb | 3 +- spec/integration/rails/engines_spec.rb | 20 +++++++++++++ spec/support/integration/helpers.rb | 12 ++++++++ .../produce_output_when_run_matcher.rb | 6 ++++ .../support/integration/test_programs/base.rb | 1 - .../integration/test_programs/plain.rb | 6 ++++ .../test_programs/rspec_active_record.rb | 5 +++- .../test_programs/rspec_active_support.rb | 5 +++- .../integration/test_programs/rspec_rails.rb | 5 +++- .../test_programs/rspec_rails_engine.rb | 30 +++++++++++++++++++ support/test_plan.rb | 5 ++++ zeus.json | 3 +- 20 files changed, 112 insertions(+), 24 deletions(-) create mode 100644 spec/integration/rails/engines_spec.rb create mode 100644 spec/support/integration/test_programs/rspec_rails_engine.rb diff --git a/.gitignore b/.gitignore index a2258b23..87d7767b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ gemfiles/*.gemfile.lock node_modules pkg spec/examples.txt +spec/internal tmp zeus.server.log zeus.server-start.log diff --git a/Appraisals b/Appraisals index c6e8c6de..5cb2556e 100644 --- a/Appraisals +++ b/Appraisals @@ -3,6 +3,7 @@ rails_dependencies = gem "activerecord-jdbcsqlite3-adapter", platform: :jruby gem "jdbc-sqlite3", platform: :jruby gem "net-ftp" + gem "combustion" end appraisals = { @@ -10,24 +11,21 @@ appraisals = { 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 {}, diff --git a/gemfiles/rails_6_0_rspec_gte_3_10.gemfile b/gemfiles/rails_6_0_rspec_gte_3_10.gemfile index 77e9c2c2..2651bb8f 100644 --- a/gemfiles/rails_6_0_rspec_gte_3_10.gemfile +++ b/gemfiles/rails_6_0_rspec_gte_3_10.gemfile @@ -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" diff --git a/gemfiles/rails_6_0_rspec_lt_3_10.gemfile b/gemfiles/rails_6_0_rspec_lt_3_10.gemfile index 3a9fd1e2..43a69561 100644 --- a/gemfiles/rails_6_0_rspec_lt_3_10.gemfile +++ b/gemfiles/rails_6_0_rspec_lt_3_10.gemfile @@ -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" diff --git a/gemfiles/rails_6_1_rspec_gte_3_10.gemfile b/gemfiles/rails_6_1_rspec_gte_3_10.gemfile index 44a7eb41..20d7982a 100644 --- a/gemfiles/rails_6_1_rspec_gte_3_10.gemfile +++ b/gemfiles/rails_6_1_rspec_gte_3_10.gemfile @@ -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" diff --git a/gemfiles/rails_6_1_rspec_lt_3_10.gemfile b/gemfiles/rails_6_1_rspec_lt_3_10.gemfile index 6a6472df..3d4a0e98 100644 --- a/gemfiles/rails_6_1_rspec_lt_3_10.gemfile +++ b/gemfiles/rails_6_1_rspec_lt_3_10.gemfile @@ -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" diff --git a/gemfiles/rails_7_0_rspec_gte_3_10.gemfile b/gemfiles/rails_7_0_rspec_gte_3_10.gemfile index 70e656a9..93abe4dd 100644 --- a/gemfiles/rails_7_0_rspec_gte_3_10.gemfile +++ b/gemfiles/rails_7_0_rspec_gte_3_10.gemfile @@ -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" diff --git a/gemfiles/rails_7_0_rspec_lt_3_10.gemfile b/gemfiles/rails_7_0_rspec_lt_3_10.gemfile index 07f2a8f9..d77e94d3 100644 --- a/gemfiles/rails_7_0_rspec_lt_3_10.gemfile +++ b/gemfiles/rails_7_0_rspec_lt_3_10.gemfile @@ -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" diff --git a/lib/super_diff/rails.rb b/lib/super_diff/rails.rb index 300a23e7..fb7b98a4 100644 --- a/lib/super_diff/rails.rb +++ b/lib/super_diff/rails.rb @@ -1 +1,2 @@ -require "super_diff/active_record" +require "super_diff/active_support" +require "super_diff/active_record" if defined?(ActiveRecord) diff --git a/spec/integration/rails/engines_spec.rb b/spec/integration/rails/engines_spec.rb new file mode 100644 index 00000000..1d0e5755 --- /dev/null +++ b/spec/integration/rails/engines_spec.rb @@ -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 diff --git a/spec/support/integration/helpers.rb b/spec/support/integration/helpers.rb index 3561a740..e30ac813 100644 --- a/spec/support/integration/helpers.rb +++ b/spec/support/integration/helpers.rb @@ -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:, diff --git a/spec/support/integration/matchers/produce_output_when_run_matcher.rb b/spec/support/integration/matchers/produce_output_when_run_matcher.rb index 18500602..66859e40 100644 --- a/spec/support/integration/matchers/produce_output_when_run_matcher.rb +++ b/spec/support/integration/matchers/produce_output_when_run_matcher.rb @@ -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 diff --git a/spec/support/integration/test_programs/base.rb b/spec/support/integration/test_programs/base.rb index de27f5dc..aab55097 100644 --- a/spec/support/integration/test_programs/base.rb +++ b/spec/support/integration/test_programs/base.rb @@ -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} diff --git a/spec/support/integration/test_programs/plain.rb b/spec/support/integration/test_programs/plain.rb index c9157678..7465203b 100644 --- a/spec/support/integration/test_programs/plain.rb +++ b/spec/support/integration/test_programs/plain.rb @@ -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 diff --git a/spec/support/integration/test_programs/rspec_active_record.rb b/spec/support/integration/test_programs/rspec_active_record.rb index b5691b61..e303f387 100644 --- a/spec/support/integration/test_programs/rspec_active_record.rb +++ b/spec/support/integration/test_programs/rspec_active_record.rb @@ -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 diff --git a/spec/support/integration/test_programs/rspec_active_support.rb b/spec/support/integration/test_programs/rspec_active_support.rb index 31ab8d93..bce2ec9d 100644 --- a/spec/support/integration/test_programs/rspec_active_support.rb +++ b/spec/support/integration/test_programs/rspec_active_support.rb @@ -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 diff --git a/spec/support/integration/test_programs/rspec_rails.rb b/spec/support/integration/test_programs/rspec_rails.rb index fe405e10..4ce1152b 100644 --- a/spec/support/integration/test_programs/rspec_rails.rb +++ b/spec/support/integration/test_programs/rspec_rails.rb @@ -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 diff --git a/spec/support/integration/test_programs/rspec_rails_engine.rb b/spec/support/integration/test_programs/rspec_rails_engine.rb new file mode 100644 index 00000000..439f3cf9 --- /dev/null +++ b/spec/support/integration/test_programs/rspec_rails_engine.rb @@ -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 diff --git a/support/test_plan.rb b/support/test_plan.rb index 8f4073b4..f76f98e1 100644 --- a/support/test_plan.rb +++ b/support/test_plan.rb @@ -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 diff --git a/zeus.json b/zeus.json index 6e696e7f..41644621 100644 --- a/zeus.json +++ b/zeus.json @@ -12,6 +12,7 @@ "boot_rails": { "run_rspec_rails_test": [] } - } + }, + "boot_rails_engine": [] } }