diff --git a/lib/super_slim.rb b/lib/super_slim.rb index 083e552..669e7ec 100644 --- a/lib/super_slim.rb +++ b/lib/super_slim.rb @@ -8,8 +8,10 @@ require_relative "super_slim/runtime" module SuperSlim - autoload :Formatter, "super_slim/formatter" - autoload :RuboCop, "super_slim/rubocop" + # By their full path: a bare "super_slim/formatter" is the first one on the load path, which is another copy of the + # gem when this one was loaded by its path, as `exe/super-slim` does in a checkout. + autoload :Formatter, File.join(__dir__, "super_slim/formatter") + autoload :RuboCop, File.join(__dir__, "super_slim/rubocop") class Error < StandardError; end class InvalidAttributeNameError < Error; end diff --git a/test/cli_test.rb b/test/cli_test.rb index 708065b..016839d 100644 --- a/test/cli_test.rb +++ b/test/cli_test.rb @@ -3,6 +3,7 @@ require "minitest/autorun" require "stringio" require "fileutils" +require "open3" require "tmpdir" require "super_slim/cli" @@ -164,6 +165,20 @@ def test_control_characters_are_escaped_in_what_lint_prints end end + # `exe/super-slim` loads the checkout it sits in by path: with the gem installed too, a formatter looked up on + # the load path would be the installed one. + def test_the_formatter_comes_from_the_copy_of_the_gem_that_was_loaded + Dir.mktmpdir do |directory| + FileUtils.mkdir_p(File.join(directory, "super_slim")) + File.write(File.join(directory, "super_slim/formatter.rb"), "raise 'the formatter of another copy of the gem'\n") + script = "require #{File.expand_path("../lib/super_slim", __dir__).dump}; print SuperSlim::Formatter.call(%(p\\n | Hello\\n))" + output, status = Open3.capture2e(RbConfig.ruby, "-I", directory, "-e", script) + + assert_predicate status, :success?, output + assert_equal "p\n | Hello\n", output + end + end + private def template_name(number) = format("template_%03d.html.slim", number)