Skip to content

Commit 91977e3

Browse files
committed
Guard against unsupported platforms so that code can still be loaded
With this change the test suite can finish on unsupported platforms. 131 runs, 22 assertions, 0 failures, 0 errors, 114 skips
1 parent 6c1dc26 commit 91977e3

19 files changed

+67
-2
lines changed

lib/bootsnap/compile_cache/iseq.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class << self
1212
def cache_dir=(cache_dir)
1313
@cache_dir = cache_dir.end_with?("/") ? "#{cache_dir}iseq" : "#{cache_dir}-iseq"
1414
end
15+
16+
def supported?
17+
CompileCache.supported? && defined?(RubyVM)
18+
end
1519
end
1620

1721
has_ruby_bug_18250 = begin # https://bugs.ruby-lang.org/issues/18250
@@ -103,11 +107,15 @@ def self.compile_option_updated
103107
crc = Zlib.crc32(option.inspect)
104108
Bootsnap::CompileCache::Native.compile_option_crc32 = crc
105109
end
106-
compile_option_updated
110+
compile_option_updated if supported?
107111

108112
def self.install!(cache_dir)
109113
Bootsnap::CompileCache::ISeq.cache_dir = cache_dir
114+
115+
return unless supported?
116+
110117
Bootsnap::CompileCache::ISeq.compile_option_updated
118+
111119
class << RubyVM::InstructionSequence
112120
prepend(InstructionSequenceMixin)
113121
end

lib/bootsnap/load_path_cache.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def unload!
5050
@loaded_features_index = nil
5151
@realpath_cache = nil
5252
@load_path_cache = nil
53-
ChangeObserver.unregister($LOAD_PATH)
53+
ChangeObserver.unregister($LOAD_PATH) if supported?
5454
end
5555

5656
def supported?

test/cli_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,35 @@ def setup
1313
end
1414

1515
def test_precompile_single_file
16+
skip_unless_iseq
1617
path = Help.set_file("a.rb", "a = a = 3", 100)
1718
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path))
1819
assert_equal 0, CLI.new(["precompile", "-j", "0", path]).run
1920
end
2021

2122
def test_precompile_rake_files
23+
skip_unless_iseq
2224
path = Help.set_file("a.rake", "a = a = 3", 100)
2325
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path))
2426
assert_equal 0, CLI.new(["precompile", "-j", "0", path]).run
2527
end
2628

2729
def test_precompile_rakefile
30+
skip_unless_iseq
2831
path = Help.set_file("Rakefile", "a = a = 3", 100)
2932
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path))
3033
assert_equal 0, CLI.new(["precompile", "-j", "0", path]).run
3134
end
3235

3336
def test_no_iseq
37+
skip_unless_iseq
3438
path = Help.set_file("a.rb", "a = a = 3", 100)
3539
CompileCache::ISeq.expects(:precompile).never
3640
assert_equal 0, CLI.new(["precompile", "-j", "0", "--no-iseq", path]).run
3741
end
3842

3943
def test_precompile_directory
44+
skip_unless_iseq
4045
path_a = Help.set_file("foo/a.rb", "a = a = 3", 100)
4146
path_b = Help.set_file("foo/b.rb", "b = b = 3", 100)
4247

@@ -46,6 +51,7 @@ def test_precompile_directory
4651
end
4752

4853
def test_precompile_exclude
54+
skip_unless_iseq
4955
path_a = Help.set_file("foo/a.rb", "a = a = 3", 100)
5056
Help.set_file("foo/b.rb", "b = b = 3", 100)
5157

@@ -68,5 +74,11 @@ def test_no_yaml
6874
CompileCache::YAML.expects(:precompile).never
6975
assert_equal 0, CLI.new(["precompile", "-j", "0", "--no-yaml", path]).run
7076
end
77+
78+
private
79+
80+
def skip_unless_iseq
81+
skip("Unsupported platform") unless defined?(CompileCache::ISeq) && CompileCache::ISeq.supported?
82+
end
7183
end
7284
end

test/compile_cache/iseq_cache_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require("test_helper")
44

55
class CompileCacheISeqTest < Minitest::Test
6+
include(CompileCacheISeqHelper)
67
include(TmpdirHelper)
78

89
def test_ruby_bug_18250

test/compile_cache/json_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def load_file(_path, symbolize_names: false, freeze: false, fallback: nil)
1515
end
1616

1717
def setup
18+
skip("Unsupported platform") unless Bootsnap::CompileCache.supported?
1819
super
1920
Bootsnap::CompileCache::JSON.init!
2021
FakeJson.singleton_class.prepend(Bootsnap::CompileCache::JSON::Patch)

test/compile_cache/yaml_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def unsafe_load_file(_path, symbolize_names: false, freeze: false, fallback: nil
1919
end
2020

2121
def setup
22+
skip("Unsupported platform") unless Bootsnap::CompileCache.supported?
2223
super
2324
Bootsnap::CompileCache::YAML.init!
2425
FakeYaml.singleton_class.prepend(Bootsnap::CompileCache::YAML.patch)

test/compile_cache_handler_errors_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require("test_helper")
44

55
class CompileCacheHandlerErrorsTest < Minitest::Test
6+
include(CompileCacheISeqHelper)
67
include(TmpdirHelper)
78

89
# now test three failure modes of each handler method:

test/compile_cache_key_format_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class CompileCacheKeyFormatTest < Minitest::Test
99
FILE = File.expand_path(__FILE__)
10+
include(CompileCacheISeqHelper)
1011
include(TmpdirHelper)
1112

1213
R = {

test/compile_cache_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require("test_helper")
44

55
class CompileCacheTest < Minitest::Test
6+
include(CompileCacheISeqHelper)
67
include(TmpdirHelper)
78

89
def teardown

test/helper_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require("test_helper")
44

55
class HelperTest < MiniTest::Test
6+
include(CompileCacheISeqHelper)
67
include(TmpdirHelper)
78

89
def test_validate_cache_path

0 commit comments

Comments
 (0)