|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | + |
| 5 | +module Bootsnap |
| 6 | + module LoadPathCache |
| 7 | + class RealpathCacheTest < MiniTest::Test |
| 8 | + EXTENSIONS = ['', *CACHED_EXTENSIONS] |
| 9 | + |
| 10 | + def setup |
| 11 | + @cache = RealpathCache.new |
| 12 | + @base_dir = Dir.mktmpdir |
| 13 | + @absolute_dir = "#{@base_dir}/absolute" |
| 14 | + Dir.mkdir(@absolute_dir) |
| 15 | + |
| 16 | + @symlinked_dir = "#{@base_dir}/symlink" |
| 17 | + FileUtils.ln_s(@absolute_dir, @symlinked_dir) |
| 18 | + |
| 19 | + real_caller = File.new("#{@absolute_dir}/real_caller.rb", 'w').path |
| 20 | + symlinked_caller = "#{@absolute_dir}/symlinked_caller.rb" |
| 21 | + |
| 22 | + FileUtils.ln_s(real_caller, symlinked_caller) |
| 23 | + |
| 24 | + EXTENSIONS.each do |ext| |
| 25 | + real_required = File.new("#{@absolute_dir}/real_required#{ext}", 'w').path |
| 26 | + |
| 27 | + symlinked_required = "#{@absolute_dir}/symlinked_required#{ext}" |
| 28 | + FileUtils.ln_s(real_required, symlinked_required) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + def teardown |
| 33 | + FileUtils.remove_entry(@base_dir) |
| 34 | + end |
| 35 | + |
| 36 | + def remove_required(extensions) |
| 37 | + extensions.each do |ext| |
| 38 | + FileUtils.rm("#{@absolute_dir}/real_required#{ext}") |
| 39 | + FileUtils.rm("#{@absolute_dir}/symlinked_required#{ext}") |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + variants = %w(absolute symlink).product(%w(absolute symlink), |
| 44 | + %w(real_caller symlinked_caller), |
| 45 | + %w(real_required symlinked_required)) |
| 46 | + |
| 47 | + variants.each do |caller_dir, required_dir, caller_file, required_file| |
| 48 | + method_name = "test_with_#{caller_dir}_caller_dir_" \ |
| 49 | + "#{required_dir}_require_dir_" \ |
| 50 | + "#{caller_file}_#{required_file}" |
| 51 | + define_method(method_name) do |
| 52 | + caller_path = "#{@base_dir}/#{caller_dir}/#{caller_file}" |
| 53 | + require_path = "../#{required_dir}/#{required_file}.rb" |
| 54 | + |
| 55 | + expected = "#{@absolute_dir}/real_required.rb" |
| 56 | + |
| 57 | + assert @cache.call(caller_path, require_path).eql?(expected) |
| 58 | + end |
| 59 | + |
| 60 | + (EXTENSIONS.size - 1).times do |n| |
| 61 | + removing = EXTENSIONS[0..n] |
| 62 | + |
| 63 | + define_method("#{method_name}_no#{removing.join('_')}_extensions") do |
| 64 | + caller_path = "#{@base_dir}/#{caller_dir}/#{caller_file}" |
| 65 | + require_path = "../#{required_dir}/#{required_file}" |
| 66 | + |
| 67 | + remove_required(removing) |
| 68 | + |
| 69 | + expected = "#{@absolute_dir}/real_required#{EXTENSIONS[n + 1]}" |
| 70 | + |
| 71 | + assert @cache.call(caller_path, require_path).eql?(expected) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + define_method("#{method_name}_no_files") do |
| 76 | + caller_path = "#{@base_dir}/#{caller_dir}/#{caller_file}" |
| 77 | + require_path = "../#{required_dir}/#{required_file}" |
| 78 | + |
| 79 | + remove_required(EXTENSIONS) |
| 80 | + |
| 81 | + expected = "#{@base_dir}/#{required_dir}/#{required_file}" |
| 82 | + assert @cache.call(caller_path, require_path).eql?(expected) |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments