Skip to content

Commit 6c1dc26

Browse files
committed
Set and restore compile cache dirs independently in tests
This avoids ending up with cache dirs like .../tmp/bootsnap/compile-cache-iseq-iseq-iseq-iseq-iseq-iseq-iseq-iseq-iseq-iseq-iseq-iseq-iseq-yaml
1 parent cd15951 commit 6c1dc26

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

test/test_helper.rb

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,38 @@ def setup
100100
@prev_dir = Dir.pwd
101101
@tmp_dir = Dir.mktmpdir("bootsnap-test")
102102
Dir.chdir(@tmp_dir)
103-
@prev = Bootsnap::CompileCache::ISeq.cache_dir
104-
Bootsnap::CompileCache::ISeq.cache_dir = @tmp_dir
105-
Bootsnap::CompileCache::YAML.cache_dir = @tmp_dir
106-
Bootsnap::CompileCache::JSON.cache_dir = @tmp_dir
103+
104+
if Bootsnap::CompileCache.supported?
105+
set_compile_cache_dir(:ISeq, @tmp_dir)
106+
set_compile_cache_dir(:YAML, @tmp_dir)
107+
set_compile_cache_dir(:JSON, @tmp_dir)
108+
end
107109
end
108110

109111
def teardown
110112
super
111113
Dir.chdir(@prev_dir)
112114
FileUtils.remove_entry(@tmp_dir)
113-
Bootsnap::CompileCache::ISeq.cache_dir = @prev
114-
Bootsnap::CompileCache::YAML.cache_dir = @prev
115-
Bootsnap::CompileCache::JSON.cache_dir = @prev
115+
116+
if Bootsnap::CompileCache.supported?
117+
restore_compile_cache_dir(:ISeq)
118+
restore_compile_cache_dir(:YAML)
119+
restore_compile_cache_dir(:JSON)
120+
end
121+
end
122+
123+
private
124+
125+
def restore_compile_cache_dir(mod_name)
126+
prev = instance_variable_get("@prev_#{mod_name.downcase}")
127+
# Restore directly to instance var to avoid duplication of suffix logic.
128+
Bootsnap::CompileCache.const_get(mod_name).instance_variable_set(:@cache_dir, prev) if prev
129+
end
130+
131+
def set_compile_cache_dir(mod_name, dir)
132+
mod = Bootsnap::CompileCache.const_get(mod_name)
133+
instance_variable_set("@prev_#{mod_name.downcase}", mod.cache_dir)
134+
# Use setter method when setting to tmp dir.
135+
mod.cache_dir = dir
116136
end
117137
end

0 commit comments

Comments
 (0)