File: test_helper.rb

package info (click to toggle)
ruby-bootsnap 1.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 520 kB
  • sloc: ruby: 3,637; ansic: 844; sh: 14; makefile: 9
file content (152 lines) | stat: -rw-r--r-- 3,420 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))

if Warning.respond_to?(:[]=)
  Warning[:deprecated] = true
end

require "bundler/setup"
require "bootsnap"
require "bootsnap/compile_cache/yaml"

require "tmpdir"
require "fileutils"

require "minitest/autorun"
require "mocha/minitest"

cache_dir = File.expand_path("../tmp/bootsnap/compile-cache", __dir__)
Bootsnap::CompileCache.setup(cache_dir: cache_dir, iseq: true, yaml: false)

if GC.respond_to?(:verify_compaction_references)
  # This method was added in Ruby 3.0.0. Calling it this way asks the GC to
  # move objects around, helping to find object movement bugs.
  begin
    GC.verify_compaction_references(expand_heap: true, toward: :empty)
  rescue NotImplementedError, ArgumentError
    # some platforms do not support GC compaction
  end
end

module TestHandler
  def self.input_to_storage(_input, path)
    "neato #{path}"
  end

  def self.storage_to_output(data, _kwargs)
    data.upcase
  end

  def self.input_to_output(_data, _kwargs)
    raise("but why tho")
  end
end

module NullCache
  def self.get(*)
  end

  def self.set(*)
  end

  def self.transaction(*)
    yield
  end

  def self.fetch(*)
    yield
  end
end

module Minitest
  class Test
    module Help
      class << self
        def cache_path(dir, file, args_key = nil)
          hash = fnv1a_64(file)
          unless args_key.nil?
            hash ^= fnv1a_64(args_key)
          end

          hex = hash.to_s(16).rjust(16, "0")
          "#{dir}/#{hex[0..1]}/#{hex[2..]}"
        end

        def fnv1a_64(data)
          hash = 0xcbf29ce484222325
          data.bytes.each do |byte|
            hash = hash ^ byte
            hash = (hash * 0x100000001b3) % (2**64)
          end
          hash
        end

        def set_file(path, contents, mtime = nil)
          FileUtils.mkdir_p(File.dirname(path))
          File.write(path, contents)
          FileUtils.touch(path, mtime: mtime) if mtime
          path
        end
      end
    end
  end
end

module CompileCacheISeqHelper
  def setup
    unless defined?(Bootsnap::CompileCache::ISeq) && Bootsnap::CompileCache::ISeq.supported?
      skip("Unsupported platform")
    end

    super
  end
end

module LoadPathCacheHelper
  def setup
    skip("Unsupported platform") unless Bootsnap::LoadPathCache.supported?

    super
  end
end

module TmpdirHelper
  def setup
    super
    @prev_dir = Dir.pwd
    @tmp_dir = Dir.mktmpdir("bootsnap-test")
    Dir.chdir(@tmp_dir)

    if Bootsnap::CompileCache.supported?
      set_compile_cache_dir(:ISeq, @tmp_dir)
      set_compile_cache_dir(:YAML, @tmp_dir)
    end
  end

  def teardown
    super
    Dir.chdir(@prev_dir)
    FileUtils.remove_entry(@tmp_dir)

    if Bootsnap::CompileCache.supported?
      restore_compile_cache_dir(:ISeq)
      restore_compile_cache_dir(:YAML)
    end
  end

  private

  def restore_compile_cache_dir(mod_name)
    prev = instance_variable_get("@prev_#{mod_name.downcase}")
    # Restore directly to instance var to avoid duplication of suffix logic.
    Bootsnap::CompileCache.const_get(mod_name).instance_variable_set(:@cache_dir, prev) if prev
  end

  def set_compile_cache_dir(mod_name, dir)
    mod = Bootsnap::CompileCache.const_get(mod_name)
    instance_variable_set("@prev_#{mod_name.downcase}", mod.cache_dir)
    # Use setter method when setting to tmp dir.
    mod.cache_dir = dir
  end
end