2019-10-12 23:23:16 +08:00
|
|
|
# frozen_string_literal: true
|
2019-02-07 02:29:01 +08:00
|
|
|
require('test_helper')
|
|
|
|
require('tempfile')
|
|
|
|
require('tmpdir')
|
|
|
|
require('fileutils')
|
2017-01-19 00:28:16 +08:00
|
|
|
|
|
|
|
class CompileCacheKeyFormatTest < Minitest::Test
|
2019-02-07 02:29:01 +08:00
|
|
|
include(TmpdirHelper)
|
2017-01-19 00:28:16 +08:00
|
|
|
|
|
|
|
R = {
|
2019-02-07 02:29:01 +08:00
|
|
|
version: 0...4,
|
2019-12-05 05:51:11 +08:00
|
|
|
ruby_platform: 4...8,
|
2017-05-24 12:06:48 +08:00
|
|
|
compile_option: 8...12,
|
2019-02-07 02:29:01 +08:00
|
|
|
ruby_revision: 12...16,
|
|
|
|
size: 16...24,
|
|
|
|
mtime: 24...32,
|
|
|
|
data_size: 32...40,
|
2017-01-19 00:28:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def test_key_version
|
2017-05-24 12:06:48 +08:00
|
|
|
key = cache_key_for_file(__FILE__)
|
|
|
|
exp = [2].pack("L")
|
|
|
|
assert_equal(exp, key[R[:version]])
|
2017-01-19 00:28:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_key_compile_option_stable
|
2017-05-24 12:06:48 +08:00
|
|
|
k1 = cache_key_for_file(__FILE__)
|
|
|
|
k2 = cache_key_for_file(__FILE__)
|
2017-01-19 00:28:16 +08:00
|
|
|
RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true }
|
2017-05-24 12:06:48 +08:00
|
|
|
k3 = cache_key_for_file(__FILE__)
|
2017-01-19 00:28:16 +08:00
|
|
|
assert_equal(k1[R[:compile_option]], k2[R[:compile_option]])
|
|
|
|
refute_equal(k1[R[:compile_option]], k3[R[:compile_option]])
|
|
|
|
ensure
|
|
|
|
RubyVM::InstructionSequence.compile_option = { tailcall_optimization: false }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_key_ruby_revision
|
2017-05-24 12:06:48 +08:00
|
|
|
key = cache_key_for_file(__FILE__)
|
2019-10-12 23:23:16 +08:00
|
|
|
exp = if RUBY_REVISION.is_a?(String)
|
|
|
|
[Help.fnv1a_64(RUBY_REVISION) >> 32].pack("L")
|
2019-07-11 14:57:33 +08:00
|
|
|
else
|
2019-10-12 23:23:16 +08:00
|
|
|
[RUBY_REVISION].pack("L")
|
2019-07-11 14:57:33 +08:00
|
|
|
end
|
2017-01-19 00:28:16 +08:00
|
|
|
assert_equal(exp, key[R[:ruby_revision]])
|
|
|
|
end
|
|
|
|
|
2017-05-24 12:06:48 +08:00
|
|
|
def test_key_size
|
|
|
|
key = cache_key_for_file(__FILE__)
|
|
|
|
exp = File.size(__FILE__)
|
|
|
|
act = key[R[:size]].unpack("Q")[0]
|
|
|
|
assert_equal(exp, act)
|
2017-01-19 00:28:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_key_mtime
|
2017-05-24 12:06:48 +08:00
|
|
|
key = cache_key_for_file(__FILE__)
|
|
|
|
exp = File.mtime(__FILE__).to_i
|
2017-01-19 00:28:16 +08:00
|
|
|
act = key[R[:mtime]].unpack("Q")[0]
|
2017-05-24 12:06:48 +08:00
|
|
|
assert_equal(exp, act)
|
2017-01-19 00:28:16 +08:00
|
|
|
end
|
|
|
|
|
2017-05-24 12:06:48 +08:00
|
|
|
def test_fetch
|
|
|
|
actual = Bootsnap::CompileCache::Native.fetch(@tmp_dir, '/dev/null', TestHandler)
|
|
|
|
assert_equal('NEATO /DEV/NULL', actual)
|
|
|
|
data = File.read("#{@tmp_dir}/8c/d2d180bbd995df")
|
2017-12-05 03:58:04 +08:00
|
|
|
assert_equal("neato /dev/null", data.force_encoding(Encoding::BINARY)[64..-1])
|
2017-05-24 12:06:48 +08:00
|
|
|
actual = Bootsnap::CompileCache::Native.fetch(@tmp_dir, '/dev/null', TestHandler)
|
|
|
|
assert_equal('NEATO /DEV/NULL', actual)
|
2017-01-19 00:28:16 +08:00
|
|
|
end
|
|
|
|
|
2018-01-13 07:15:55 +08:00
|
|
|
def test_unexistent_fetch
|
|
|
|
assert_raises(Errno::ENOENT) do
|
|
|
|
Bootsnap::CompileCache::Native.fetch(@tmp_dir, '123', Bootsnap::CompileCache::ISeq)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-24 12:06:48 +08:00
|
|
|
private
|
2017-01-19 00:28:16 +08:00
|
|
|
|
2017-05-24 12:06:48 +08:00
|
|
|
def cache_key_for_file(file)
|
|
|
|
Bootsnap::CompileCache::Native.fetch(@tmp_dir, file, TestHandler)
|
|
|
|
data = File.read(Help.cache_path(@tmp_dir, file))
|
|
|
|
Help.binary(data[0..31])
|
2017-01-19 00:28:16 +08:00
|
|
|
end
|
|
|
|
end
|