From f7795790070af1037ef52a1ad00b59cfa9701311 Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Tue, 20 Aug 2024 13:50:25 -0700 Subject: [PATCH] Fix CI env variables --- .github/workflows/test.yml | 22 +++++++++-------- Gemfile | 5 ++-- spec/rails_helper.rb | 50 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0aeb60a..cddf51f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,29 +18,31 @@ jobs: ### TEST RUBY VERSIONS - ruby: "2.7" - ruby: "3.0" + db_gem_version: "~> 1.4" # fixes sqlite3 gem dependency issue - ruby: "3.1" - ruby: "3.2" - ruby: "3.3" ### TEST RAILS VERSIONS - ruby: "2.7" - env: - RAILS_VERSION: "6.0" + rails_version: "~> 6.0" - ruby: "2.7" - env: - RAILS_VERSION: "6.1" + rails_version: "~> 6.1" - ruby: "3.3" - env: - RAILS_VERSION: "7.0" + rails_version: "~> 7.0" - ruby: "3.3" - env: - RAILS_VERSION: "7.1" + rails_version: "~> 7.1" - ruby: "3.3" - env: - RAILS_VERSION: "7.2" + rails_version: "~> 7.2" steps: - uses: actions/checkout@v3 + - name: Set env variables + run: | + echo "RAILS_VERSION=${{ matrix.rails_version }}" >> "$GITHUB_ENV" + echo "DB_GEM=${{ matrix.db_gem }}" >> "$GITHUB_ENV" + echo "DB_GEM_VERSION=${{ matrix.db_gem_version }}" >> "$GITHUB_ENV" + - name: Install ruby uses: ruby/setup-ruby@v1 with: diff --git a/Gemfile b/Gemfile index f67a26b..7936a9a 100644 --- a/Gemfile +++ b/Gemfile @@ -20,12 +20,11 @@ end gem "rails", get_env("RAILS_VERSION") +db_gem = get_env("DB_GEM") || "sqlite3" +gem db_gem, get_env("DB_GEM_VERSION") if get_env("RAILS_VERSION").to_f >= 7.2 gem "activerecord-enhancedsqlite3-adapter" - gem "sqlite3", "~> 2.0" -else - gem "sqlite3", "~> 1.7" end group :development do diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index d81fd05..e1b8ca7 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -22,3 +22,53 @@ config.filter_rails_from_backtrace! end + +if Rails::VERSION::MAJOR <= 6 + ActiveSupport::Testing::Assertions.module_eval do + def assert_difference(expression, *args, &block) + expressions = + if expression.is_a?(Hash) + message = args[0] + expression + else + difference = args[0] || 1 + message = args[1] + Array(expression).index_with(difference) + end + + exps = expressions.keys.map { |e| + e.respond_to?(:call) ? e : lambda { eval(e, block.binding) } + } + before = exps.map(&:call) + + retval = _assert_nothing_raised_or_warn("assert_difference", &block) + + expressions.zip(exps, before) do |(code, diff), exp, before_value| + actual = exp.call + error = "#{code.inspect} didn't change by #{diff}, but by #{actual - before_value}" + error = "#{message}.\n#{error}" if message + assert_equal(before_value + diff, actual, error) + end + + retval + end + + private + + def _assert_nothing_raised_or_warn(assertion, &block) + assert_nothing_raised(&block) + rescue Minitest::UnexpectedError => e + if tagged_logger && tagged_logger.warn? + warning = <<~MSG + #{self.class} - #{name}: #{e.error.class} raised. + If you expected this exception, use `assert_raises` as near to the code that raises as possible. + Other block based assertions (e.g. `#{assertion}`) can be used, as long as `assert_raises` is inside their block. + MSG + tagged_logger.warn warning + end + + raise + end + + end +end