Skip to content

Commit

Permalink
feat: Support configuring MT_COMPAT in the minitest template (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
dazuma authored Oct 6, 2023
1 parent 60fc011 commit 2eabac6
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
2 changes: 1 addition & 1 deletion toys-core/.toys/.toys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

expand :clean, paths: :gitignore

expand :minitest, libs: ["lib", "test"], bundler: true
expand :minitest, libs: ["lib", "test"], bundler: true, mt_compat: true

tool "test" do
flag :integration_tests, "--integration-tests", "--integration", desc: "Enable integration tests"
Expand Down
2 changes: 1 addition & 1 deletion toys/.toys/.toys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

expand :clean, paths: :gitignore

expand :minitest, libs: ["lib", "test"], bundler: true
expand :minitest, libs: ["lib", "test"], bundler: true, mt_compat: true

tool "test" do
flag :integration_tests, "--integration-tests", "--integration", desc: "Enable integration tests"
Expand Down
28 changes: 27 additions & 1 deletion toys/lib/toys/templates/minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Minitest
# enabled. See the documentation for the
# [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
# for information on available options.
# @param mt_compat [boolean] If set to `true` or `false`, sets the
# `MT_COMPAT` environment variable accordingly. This may be required
# for certain older Minitest plugins. Optional. If not present, keeps
# any current setting.
# @param context_directory [String] A custom context directory to use
# when executing this tool.
#
Expand All @@ -66,6 +70,7 @@ def initialize(name: nil,
verbose: false,
warnings: true,
bundler: false,
mt_compat: nil,
context_directory: nil)
@name = name
@gem_version = gem_version
Expand All @@ -75,6 +80,7 @@ def initialize(name: nil,
@verbose = verbose
@warnings = warnings
@bundler = bundler
@mt_compat = mt_compat
@context_directory = context_directory
end

Expand Down Expand Up @@ -160,6 +166,18 @@ def initialize(name: nil,
#
attr_writer :bundler

##
# Adjust the `MT_COMPAT` environment variable when running tests. This
# setting may be necessary for certain older Minitest plugins.
#
# Pass `true` to enable compat mode, `false` to disable it, or `nil` to
# use any ambient setting from the current environment.
#
# @param value [true,false,nil]
# @return [true,false,nil]
#
attr_writer :mt_compat

##
# Use bundler for this tool.
#
Expand Down Expand Up @@ -195,6 +213,11 @@ def use_bundler(**opts)
#
attr_reader :context_directory

##
# @private
#
attr_reader :mt_compat

##
# @private
#
Expand Down Expand Up @@ -287,7 +310,10 @@ def bundler_settings
ruby_args << "--name" << name if name
ruby_args << "--exclude" << exclude if exclude

result = exec_ruby(ruby_args)
env = {}
env["MT_COMPAT"] = template.mt_compat ? "true" : nil unless template.mt_compat.nil?

result = exec_ruby(ruby_args, env: env)
if result.error?
logger.error("Minitest failed!")
exit(result.exit_code)
Expand Down
9 changes: 9 additions & 0 deletions toys/test/minitest-cases/mt-compat/foo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "minitest/autorun"

describe "foo" do
it "sets MT_COMPAT" do
assert_equal("true", ENV["MT_COMPAT"])
end
end
57 changes: 57 additions & 0 deletions toys/test/test_minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@
assert_equal(false, template.warnings)
end

it "handles the mt_compat field" do
assert_nil(template.mt_compat)
template.mt_compat = false
assert_equal(false, template.mt_compat)
template.mt_compat = true
assert_equal(true, template.mt_compat)
end

it "handles the gem_version field with bundler" do
template.use_bundler
assert_equal([], template.gem_version)
Expand Down Expand Up @@ -166,6 +174,55 @@
assert_match(/1 failure/, out)
end

it "passes MT_COMPAT" do
dir = cases_dir
loader.add_block do
set_context_directory dir
expand :minitest, files: "mt-compat/*.rb"
end
expected_failures = ENV["MT_COMPAT"] ? 0 : 1
out, _err = capture_subprocess_io do
assert_equal(expected_failures, cli.run("test"))
end
assert_match(/#{expected_failures} failure/, out)
end

it "sets MT_COMPAT" do
dir = cases_dir
loader.add_block do
set_context_directory dir
expand :minitest, files: "mt-compat/*.rb", mt_compat: true
end
old_mt_compat = ENV["MT_COMPAT"]
begin
out, _err = capture_subprocess_io do
ENV["MT_COMPAT"] = nil
assert_equal(0, cli.run("test"))
end
ensure
ENV["MT_COMPAT"] = old_mt_compat
end
assert_match(/0 failures/, out)
end

it "clears MT_COMPAT" do
dir = cases_dir
loader.add_block do
set_context_directory dir
expand :minitest, files: "mt-compat/*.rb", mt_compat: false
end
old_mt_compat = ENV["MT_COMPAT"]
begin
out, _err = capture_subprocess_io do
ENV["MT_COMPAT"] = "true"
assert_equal(1, cli.run("test"))
end
ensure
ENV["MT_COMPAT"] = old_mt_compat
end
assert_match(/1 failure/, out)
end

it "supports input streams" do
dir = "#{cases_dir}/stream"
args = ["--disable=gems", Toys.executable_path, "test"]
Expand Down

0 comments on commit 2eabac6

Please sign in to comment.