-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
resolv-replace
compatibility test
While `dalli` doesn't depend on `resolv-replace`, we want to ensure it still works if it is required by the host application. This regression test ensures we don't break compatibility, and adds a framework allowing us to test compatibility with other gems in the future.
- Loading branch information
1 parent
67942b8
commit d406bce
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#! /usr/bin/env ruby | ||
|
||
# frozen_string_literal: true | ||
|
||
# This file is meant to be run in a subprocess from a test file, to test compatibility with other gems in isolation. | ||
# If the test fails, it will exit with a non-zero status code and print the marshaled error to stdout. | ||
|
||
require 'minitest' | ||
require 'dalli' | ||
|
||
def assert_round_trip(message) | ||
key = "random-key-#{Time.now.to_f}" | ||
expected = Time.now.to_f.to_s | ||
ttl = 10 # seconds | ||
|
||
client = Dalli::Client.new(nil) | ||
client.set(key, expected, ttl) | ||
|
||
actual = client.get(key) | ||
|
||
# assert_equal is not available in this context, so we implement it ourselves. | ||
raise Minitest::Assertion.new, <<~MESSAGE unless expected == actual | ||
#{message}. | ||
Expected: #{expected.inspect} | ||
Actual: #{actual.inspect} | ||
MESSAGE | ||
end | ||
|
||
begin | ||
gems = ARGV | ||
gem_list = gems.map(&:inspect).join(', ') | ||
raise 'No gems specified to require' if gems.empty? | ||
|
||
assert_round_trip("Failed to round-trip key before requiring #{gem_list}") | ||
|
||
gems.each { |gem_name| require gem_name } | ||
|
||
assert_round_trip("Failed to round-trip key after requiring #{gem_list}") | ||
rescue Exception => e # rubocop:disable Lint/RescueException | ||
# Marshal the exception to stdout so the parent process can raise it. | ||
# Don't use stderr, as it might also contain other output, such as warnings. | ||
puts Marshal.dump(e) | ||
abort | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'helper' | ||
require 'open3' | ||
|
||
describe 'gem compatibility' do | ||
[ | ||
'resolv-replace' | ||
].each do |gem_name| | ||
it "passes smoke test with #{gem_name.inspect} gem required" do | ||
# We use a separate Ruby process so we can require the gem in isolation without affecting the rest of the tests. | ||
stdout, stderr, status = Open3.capture3(File.join(__dir__, '../scripts/gems_smoke_test.rb'), gem_name) | ||
return pass if status.success? | ||
|
||
error = begin | ||
# Marshal.load is fine here because we're loading what we just dumped in the subprocess. | ||
Marshal.load(stdout) # rubocop:disable Security/MarshalLoad | ||
rescue StandardError => e | ||
raise <<~MESSAGE | ||
Failed to unmarshal error from subprocess! | ||
#{e.class}: #{e} | ||
stdout: #{stdout} | ||
stderr: #{stderr} | ||
MESSAGE | ||
end | ||
|
||
pass if error.is_a?(Minitest::Assertion) # Increment assertion count | ||
raise error | ||
end | ||
end | ||
end |