10
10
module TestIRB
11
11
class HistoryTest < TestCase
12
12
def setup
13
+ @original_verbose , $VERBOSE = $VERBOSE, nil
14
+ @tmpdir = Dir . mktmpdir ( "test_irb_history_" )
15
+ @backup_home = ENV [ "HOME" ]
16
+ @backup_xdg_config_home = ENV . delete ( "XDG_CONFIG_HOME" )
17
+ @backup_irbrc = ENV . delete ( "IRBRC" )
18
+ @backup_default_external = Encoding . default_external
19
+ ENV [ "HOME" ] = @tmpdir
13
20
IRB . conf [ :RC_NAME_GENERATOR ] = nil
14
21
end
15
22
16
23
def teardown
17
24
IRB . conf [ :RC_NAME_GENERATOR ] = nil
25
+ ENV [ "HOME" ] = @backup_home
26
+ ENV [ "XDG_CONFIG_HOME" ] = @backup_xdg_config_home
27
+ ENV [ "IRBRC" ] = @backup_irbrc
28
+ Encoding . default_external = @backup_default_external
29
+ $VERBOSE = @original_verbose
18
30
end
19
31
20
32
class TestInputMethodWithRelineHistory < TestInputMethod
@@ -123,35 +135,23 @@ def test_history_concurrent_use_readline
123
135
end
124
136
125
137
def test_history_concurrent_use_not_present
126
- backup_home = ENV [ "HOME" ]
127
- backup_xdg_config_home = ENV . delete ( "XDG_CONFIG_HOME" )
128
- backup_irbrc = ENV . delete ( "IRBRC" )
129
138
IRB . conf [ :LC_MESSAGES ] = IRB ::Locale . new
130
139
IRB . conf [ :SAVE_HISTORY ] = 1
131
- Dir . mktmpdir ( "test_irb_history_" ) do |tmpdir |
132
- ENV [ "HOME" ] = tmpdir
133
- io = TestInputMethodWithRelineHistory . new
134
- io . class ::HISTORY . clear
135
- io . load_history
136
- io . class ::HISTORY << 'line1'
137
- io . class ::HISTORY << 'line2'
140
+ io = TestInputMethodWithRelineHistory . new
141
+ io . class ::HISTORY . clear
142
+ io . load_history
143
+ io . class ::HISTORY << 'line1'
144
+ io . class ::HISTORY << 'line2'
138
145
139
- history_file = IRB . rc_files ( "_history" ) . first
140
- assert_not_send [ File , :file? , history_file ]
141
- File . write ( history_file , "line0\n " )
142
- io . save_history
143
- assert_equal ( %w" line0 line1 line2 " , File . read ( history_file ) . split )
144
- end
145
- ensure
146
- ENV [ "HOME" ] = backup_home
147
- ENV [ "XDG_CONFIG_HOME" ] = backup_xdg_config_home
148
- ENV [ "IRBRC" ] = backup_irbrc
146
+ history_file = IRB . rc_files ( "_history" ) . first
147
+ assert_not_send [ File , :file? , history_file ]
148
+ File . write ( history_file , "line0\n " )
149
+ io . save_history
150
+ assert_equal ( %w" line0 line1 line2 " , File . read ( history_file ) . split )
149
151
end
150
152
151
153
def test_history_different_encodings
152
- backup_default_external = Encoding . default_external
153
154
IRB . conf [ :SAVE_HISTORY ] = 2
154
- verbose_bak , $VERBOSE = $VERBOSE, nil
155
155
Encoding . default_external = Encoding ::US_ASCII
156
156
locale = IRB ::Locale . new ( "C" )
157
157
assert_history ( <<~EXPECTED_HISTORY . encode ( Encoding ::US_ASCII ) , <<~INITIAL_HISTORY . encode ( Encoding ::UTF_8 ) , <<~INPUT , locale : locale )
@@ -162,9 +162,6 @@ def test_history_different_encodings
162
162
INITIAL_HISTORY
163
163
exit
164
164
INPUT
165
- ensure
166
- Encoding . default_external = backup_default_external
167
- $VERBOSE = verbose_bak
168
165
end
169
166
170
167
def test_history_does_not_raise_when_history_file_directory_does_not_exist
@@ -176,6 +173,11 @@ def test_history_does_not_raise_when_history_file_directory_does_not_exist
176
173
assert_warn ( /history file does not exist/ ) do
177
174
io . save_history
178
175
end
176
+
177
+ # assert_warn reverts $VERBOSE to EnvUtil.original_verbose, which is true in some cases
178
+ # We want to keep $VERBOSE as nil until teardown is called
179
+ # TODO: check if this is an assert_warn issue
180
+ $VERBOSE = nil
179
181
ensure
180
182
IRB . conf [ :HISTORY_FILE ] = backup_history_file
181
183
end
@@ -212,46 +214,37 @@ def history_concurrent_use_for_input_method(input_method)
212
214
end
213
215
214
216
def assert_history ( expected_history , initial_irb_history , input , input_method = TestInputMethodWithRelineHistory , locale : IRB ::Locale . new )
215
- backup_verbose , $VERBOSE = $VERBOSE, nil
216
- backup_home = ENV [ "HOME" ]
217
- backup_xdg_config_home = ENV . delete ( "XDG_CONFIG_HOME" )
218
217
IRB . conf [ :LC_MESSAGES ] = locale
219
218
actual_history = nil
220
219
history_file = IRB . rc_files ( "_history" ) . first
221
- Dir . mktmpdir ( "test_irb_history_" ) do |tmpdir |
222
- ENV [ "HOME" ] = tmpdir
223
- File . open ( history_file , "w" ) do |f |
224
- f . write ( initial_irb_history )
225
- end
220
+ ENV [ "HOME" ] = @tmpdir
221
+ File . open ( history_file , "w" ) do |f |
222
+ f . write ( initial_irb_history )
223
+ end
226
224
227
- io = input_method . new
225
+ io = input_method . new
226
+ io . class ::HISTORY . clear
227
+ io . load_history
228
+ if block_given?
229
+ previous_history = [ ]
230
+ io . class ::HISTORY . each { |line | previous_history << line }
231
+ yield history_file
228
232
io . class ::HISTORY . clear
229
- io . load_history
230
- if block_given?
231
- previous_history = [ ]
232
- io . class ::HISTORY . each { |line | previous_history << line }
233
- yield history_file
234
- io . class ::HISTORY . clear
235
- previous_history . each { |line | io . class ::HISTORY << line }
236
- end
237
- input . split . each { |line | io . class ::HISTORY << line }
238
- io . save_history
233
+ previous_history . each { |line | io . class ::HISTORY << line }
234
+ end
235
+ input . split . each { |line | io . class ::HISTORY << line }
236
+ io . save_history
239
237
240
- io . load_history
241
- File . open ( history_file , "r" ) do |f |
242
- actual_history = f . read
243
- end
238
+ io . load_history
239
+ File . open ( history_file , "r" ) do |f |
240
+ actual_history = f . read
244
241
end
245
242
assert_equal ( expected_history , actual_history , <<~MESSAGE )
246
243
expected:
247
244
#{ expected_history }
248
245
but actual:
249
246
#{ actual_history }
250
247
MESSAGE
251
- ensure
252
- $VERBOSE = backup_verbose
253
- ENV [ "HOME" ] = backup_home
254
- ENV [ "XDG_CONFIG_HOME" ] = backup_xdg_config_home
255
248
end
256
249
257
250
def with_temp_stdio
0 commit comments