2020-10-29 01:29:23 +08:00
|
|
|
# frozen_string_literal: true
|
2022-01-13 17:05:07 +08:00
|
|
|
|
2023-12-14 17:47:17 +08:00
|
|
|
require "test_helper"
|
|
|
|
require "bootsnap/cli"
|
2020-10-29 01:29:23 +08:00
|
|
|
|
|
|
|
module Bootsnap
|
|
|
|
class CLITest < Minitest::Test
|
2023-12-14 17:47:17 +08:00
|
|
|
include TmpdirHelper
|
2020-10-29 01:29:23 +08:00
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
2022-01-13 17:05:07 +08:00
|
|
|
@cache_dir = File.expand_path("tmp/cache/bootsnap/compile-cache")
|
2020-10-29 01:29:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_precompile_single_file
|
2023-06-30 06:59:39 +08:00
|
|
|
skip_unless_iseq
|
2022-01-13 17:05:07 +08:00
|
|
|
path = Help.set_file("a.rb", "a = a = 3", 100)
|
2022-01-13 19:51:39 +08:00
|
|
|
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path))
|
2022-01-13 17:05:07 +08:00
|
|
|
assert_equal 0, CLI.new(["precompile", "-j", "0", path]).run
|
2020-10-29 01:29:23 +08:00
|
|
|
end
|
|
|
|
|
2022-03-09 19:47:58 +08:00
|
|
|
def test_precompile_rake_files
|
2023-06-30 06:59:39 +08:00
|
|
|
skip_unless_iseq
|
2022-03-09 19:47:58 +08:00
|
|
|
path = Help.set_file("a.rake", "a = a = 3", 100)
|
|
|
|
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path))
|
|
|
|
assert_equal 0, CLI.new(["precompile", "-j", "0", path]).run
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_precompile_rakefile
|
2023-06-30 06:59:39 +08:00
|
|
|
skip_unless_iseq
|
2022-03-09 19:47:58 +08:00
|
|
|
path = Help.set_file("Rakefile", "a = a = 3", 100)
|
|
|
|
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path))
|
|
|
|
assert_equal 0, CLI.new(["precompile", "-j", "0", path]).run
|
|
|
|
end
|
|
|
|
|
2021-01-25 23:30:31 +08:00
|
|
|
def test_no_iseq
|
2023-06-30 06:59:39 +08:00
|
|
|
skip_unless_iseq
|
2022-01-13 17:05:07 +08:00
|
|
|
path = Help.set_file("a.rb", "a = a = 3", 100)
|
2021-01-25 23:30:31 +08:00
|
|
|
CompileCache::ISeq.expects(:precompile).never
|
2022-01-13 17:05:07 +08:00
|
|
|
assert_equal 0, CLI.new(["precompile", "-j", "0", "--no-iseq", path]).run
|
2021-01-25 23:30:31 +08:00
|
|
|
end
|
|
|
|
|
2020-10-29 01:29:23 +08:00
|
|
|
def test_precompile_directory
|
2023-06-30 06:59:39 +08:00
|
|
|
skip_unless_iseq
|
2022-01-13 17:05:07 +08:00
|
|
|
path_a = Help.set_file("foo/a.rb", "a = a = 3", 100)
|
|
|
|
path_b = Help.set_file("foo/b.rb", "b = b = 3", 100)
|
2020-10-29 01:29:23 +08:00
|
|
|
|
2022-01-13 19:51:39 +08:00
|
|
|
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path_a))
|
|
|
|
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path_b))
|
2022-01-13 17:05:07 +08:00
|
|
|
assert_equal 0, CLI.new(["precompile", "-j", "0", "foo"]).run
|
2020-10-29 01:29:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_precompile_exclude
|
2023-06-30 06:59:39 +08:00
|
|
|
skip_unless_iseq
|
2022-01-13 17:05:07 +08:00
|
|
|
path_a = Help.set_file("foo/a.rb", "a = a = 3", 100)
|
|
|
|
Help.set_file("foo/b.rb", "b = b = 3", 100)
|
2020-10-29 01:29:23 +08:00
|
|
|
|
2022-01-13 19:51:39 +08:00
|
|
|
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path_a))
|
2022-01-13 17:05:07 +08:00
|
|
|
assert_equal 0, CLI.new(["precompile", "-j", "0", "--exclude", "b.rb", "foo"]).run
|
2020-10-29 01:29:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_precompile_gemfile
|
2022-01-13 17:05:07 +08:00
|
|
|
assert_equal 0, CLI.new(["precompile", "--gemfile"]).run
|
2020-10-29 01:29:23 +08:00
|
|
|
end
|
2021-01-25 23:30:31 +08:00
|
|
|
|
|
|
|
def test_precompile_yaml
|
2022-01-13 17:05:07 +08:00
|
|
|
path = Help.set_file("a.yaml", "foo: bar", 100)
|
2022-01-13 19:51:39 +08:00
|
|
|
CompileCache::YAML.expects(:precompile).with(File.expand_path(path))
|
2022-01-13 17:05:07 +08:00
|
|
|
assert_equal 0, CLI.new(["precompile", "-j", "0", path]).run
|
2021-01-25 23:30:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_yaml
|
2022-01-13 17:05:07 +08:00
|
|
|
path = Help.set_file("a.yaml", "foo: bar", 100)
|
2021-01-25 23:30:31 +08:00
|
|
|
CompileCache::YAML.expects(:precompile).never
|
2022-01-13 17:05:07 +08:00
|
|
|
assert_equal 0, CLI.new(["precompile", "-j", "0", "--no-yaml", path]).run
|
2021-01-25 23:30:31 +08:00
|
|
|
end
|
2023-06-30 06:59:39 +08:00
|
|
|
|
2024-04-04 23:28:01 +08:00
|
|
|
if Process.respond_to?(:fork)
|
|
|
|
def test_version_flag
|
|
|
|
read, write = IO.pipe
|
|
|
|
# optparse --version immediately call exit so we test in a subprocess
|
2024-05-21 15:01:25 +08:00
|
|
|
pid = fork do
|
|
|
|
$stdout.reopen(write)
|
2024-04-04 23:28:01 +08:00
|
|
|
read.close
|
|
|
|
exit(CLI.new(["--version"]).run)
|
|
|
|
end
|
|
|
|
write.close
|
|
|
|
|
|
|
|
_, status = Process.waitpid2(pid)
|
|
|
|
assert_predicate status, :success?
|
|
|
|
|
|
|
|
assert_includes read.read, Bootsnap::VERSION
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-30 06:59:39 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def skip_unless_iseq
|
|
|
|
skip("Unsupported platform") unless defined?(CompileCache::ISeq) && CompileCache::ISeq.supported?
|
|
|
|
end
|
2020-10-29 01:29:23 +08:00
|
|
|
end
|
|
|
|
end
|