@@ -100,18 +100,38 @@ def setup
100
100
@prev_dir = Dir . pwd
101
101
@tmp_dir = Dir . mktmpdir ( "bootsnap-test" )
102
102
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
107
109
end
108
110
109
111
def teardown
110
112
super
111
113
Dir . chdir ( @prev_dir )
112
114
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
116
136
end
117
137
end
0 commit comments