tins 1.60.1 → 1.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGES.md +81 -0
- data/lib/tins/attempt.rb +47 -9
- data/lib/tins/dslkit.rb +91 -52
- data/lib/tins/duration.rb +342 -53
- data/lib/tins/enum_joined.rb +68 -0
- data/lib/tins/enum_squeeze.rb +33 -0
- data/lib/tins/null.rb +1 -1
- data/lib/tins/once.rb +22 -14
- data/lib/tins/version.rb +7 -2
- data/lib/tins/xt/enum_joined.rb +7 -0
- data/lib/tins/xt.rb +1 -0
- data/tests/duration_test.rb +313 -0
- data/tests/enum_joined_test.rb +59 -0
- data/tests/file_binary_test.rb +1 -1
- data/tests/find_test.rb +1 -1
- data/tests/lines_file_test.rb +1 -1
- data/tests/once_test.rb +93 -0
- data/tests/secure_write_test.rb +7 -7
- data/tests/test_helper.rb +1 -0
- data/tins.gemspec +7 -7
- metadata +12 -4
data/tests/duration_test.rb
CHANGED
|
@@ -109,5 +109,318 @@ module Tins
|
|
|
109
109
|
assert_equal '3', d.format('%M')
|
|
110
110
|
assert_equal '4', d.format('%u')
|
|
111
111
|
end
|
|
112
|
+
|
|
113
|
+
def test_format_and_parse_literal_suffix
|
|
114
|
+
d = Tins::Duration.new(9000) # 2h 30m
|
|
115
|
+
assert_equal '2h 30m', d.format('%Hh %Mm')
|
|
116
|
+
assert_equal 9000,
|
|
117
|
+
Tins::Duration.parse('2h 30m', template: '%hh %mm')
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def test_parse_literal_suffix_array
|
|
121
|
+
assert_equal 9000,
|
|
122
|
+
Tins::Duration.parse(
|
|
123
|
+
'2h 30m', template: [ '%hh %mm', '%hh', '%mm' ]
|
|
124
|
+
)
|
|
125
|
+
assert_equal 7200,
|
|
126
|
+
Tins::Duration.parse(
|
|
127
|
+
'2h', template: [ '%hh %mm', '%hh', '%mm' ]
|
|
128
|
+
)
|
|
129
|
+
assert_equal 1800,
|
|
130
|
+
Tins::Duration.parse(
|
|
131
|
+
'30m', template: [ '%hh %mm', '%hh', '%mm' ]
|
|
132
|
+
)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def test_parse_raises_on_literal_mismatch
|
|
136
|
+
assert_raise(Tins::Duration::ParserError) do
|
|
137
|
+
Tins::Duration.parse('1x:30', template: '%h:%m')
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def test_parse_raises_on_percent_mismatch
|
|
142
|
+
assert_raise(Tins::Duration::ParserError) do
|
|
143
|
+
Tins::Duration.parse('1x', template: '%h%%%m')
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def test_parser_error_is_argument_error
|
|
148
|
+
assert_operator(Tins::Duration::ParserError, :<, ArgumentError)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def test_parse_template_array_first_match_wins
|
|
152
|
+
# Both templates match '53' but yield different values; order decides.
|
|
153
|
+
assert_equal 190_800,
|
|
154
|
+
Tins::Duration.parse('53', template: [ '%h', '%m' ])
|
|
155
|
+
assert_equal 3180,
|
|
156
|
+
Tins::Duration.parse('53', template: [ '%m', '%h' ])
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def test_parse_template_array_second_match
|
|
160
|
+
# First template fails on the string, second succeeds.
|
|
161
|
+
assert_equal 18_243,
|
|
162
|
+
Tins::Duration.parse('05:04:03',
|
|
163
|
+
template: [ '%S%d+%h:%m:%s.%f', '%h:%m:%s' ])
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def test_parse_template_array_single_element
|
|
167
|
+
# A one-element array behaves like a plain template string.
|
|
168
|
+
assert_equal 18_243,
|
|
169
|
+
Tins::Duration.parse('05:04:03', template: [ '%h:%m:%s' ])
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def test_parse_template_array_none_match
|
|
173
|
+
assert_raise(Tins::Duration::ParserError) do
|
|
174
|
+
Tins::Duration.parse('1x:30',
|
|
175
|
+
template: [ '%h:%m', '%S%d+%h:%m:%s' ])
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def test_parse_template_prefix_consumption
|
|
180
|
+
# The parser is greedy and does not require the input to be
|
|
181
|
+
# fully consumed. '%m' matches '1' in '1m and 30s' and
|
|
182
|
+
# returns; the remaining 'm and 30s' is silently ignored.
|
|
183
|
+
assert_equal 60,
|
|
184
|
+
Tins::Duration.parse('1m and 30s', template: '%m')
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def test_parse_template_array_order_matters_for_prefix
|
|
188
|
+
# '%m' alone matches the prefix '1' of '1m and 30s',
|
|
189
|
+
# returning 60. '%mm and %ss' matches the full string,
|
|
190
|
+
# returning 90. The first template in the array wins,
|
|
191
|
+
# so the order is critical for prefix-matching templates.
|
|
192
|
+
assert_equal 60,
|
|
193
|
+
Tins::Duration.parse(
|
|
194
|
+
'1m and 30s', template: [ '%m', '%mm and %ss' ]
|
|
195
|
+
)
|
|
196
|
+
assert_equal 90,
|
|
197
|
+
Tins::Duration.parse(
|
|
198
|
+
'1m and 30s', template: [ '%mm and %ss', '%m' ]
|
|
199
|
+
)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def test_from_string_default
|
|
203
|
+
d = Tins::Duration.from_string('6+05:04:03.210')
|
|
204
|
+
assert_instance_of Tins::Duration, d
|
|
205
|
+
assert_in_delta 536_643.21, d.to_f, 1e-9
|
|
206
|
+
assert_equal '6+05:04:03.210', d.to_s
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def test_from_string_custom_template
|
|
210
|
+
d = Tins::Duration.from_string('05:04:03', template: '%h:%m:%s')
|
|
211
|
+
assert_in_delta 18_243.0, d.to_f, 1e-9
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def test_from_string_negative
|
|
215
|
+
d = Tins::Duration.from_string(
|
|
216
|
+
'-00:00:42', template: '%S%h:%m:%s'
|
|
217
|
+
)
|
|
218
|
+
assert_true d.negative?
|
|
219
|
+
assert_in_delta 42.0, d.to_f, 1e-9
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def test_from_string_raises_on_mismatch
|
|
223
|
+
assert_raise(Tins::Duration::ParserError) do
|
|
224
|
+
Tins::Duration.from_string('1x:30', template: '%h:%m')
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def test_from_string_roundtrip
|
|
229
|
+
d = Tins::Duration.new(93_784)
|
|
230
|
+
assert_equal d, Tins::Duration.from_string(
|
|
231
|
+
d.to_s, template: '%d+%h:%m:%s'
|
|
232
|
+
)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def test_parse_human_basic
|
|
236
|
+
d = Tins::Duration.parse_human('1 minutes and 30 seconds')
|
|
237
|
+
assert_instance_of Tins::Duration, d
|
|
238
|
+
assert_in_delta 90.0, d.to_f, 1e-9
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def test_parse_human_single_unit
|
|
242
|
+
assert_in_delta 86_400.0,
|
|
243
|
+
Tins::Duration.parse_human('1 days').to_f, 1e-9
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def test_parse_human_three_units
|
|
247
|
+
assert_in_delta 3725.0,
|
|
248
|
+
Tins::Duration.parse_human(
|
|
249
|
+
'1 hours, 2 minutes, and 5 seconds'
|
|
250
|
+
).to_f, 1e-9
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def test_parse_human_four_units
|
|
254
|
+
assert_in_delta 93_784.0,
|
|
255
|
+
Tins::Duration.parse_human(
|
|
256
|
+
'1 days, 2 hours, 3 minutes, and 4 seconds'
|
|
257
|
+
).to_f, 1e-9
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def test_parse_human_style_abbrev
|
|
261
|
+
assert_in_delta 90.0,
|
|
262
|
+
Tins::Duration.parse_human(
|
|
263
|
+
'1m30s', style: :abbrev
|
|
264
|
+
).to_f, 1e-9
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def test_parse_human_roundtrip_abbrev
|
|
268
|
+
[ 90, 3725, 86_400, 93_784, 5_000_000 ].each do |s|
|
|
269
|
+
d = Tins::Duration.new(s)
|
|
270
|
+
round = Tins::Duration.parse_human(
|
|
271
|
+
d.to_human(style: :abbrev), style: :abbrev
|
|
272
|
+
)
|
|
273
|
+
assert_equal d, round
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def test_parse_human_roundtrip
|
|
278
|
+
[ 90, 3725, 86_400, 93_784 ].each do |s|
|
|
279
|
+
d = Tins::Duration.new(s)
|
|
280
|
+
round = Tins::Duration.parse_human(d.to_human)
|
|
281
|
+
assert_equal d, round
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def test_parse_human_custom_connector
|
|
286
|
+
assert_in_delta 3725.0,
|
|
287
|
+
Tins::Duration.parse_human(
|
|
288
|
+
'1h / 2m / 5s', style: :abbrev,
|
|
289
|
+
connector: { words: ' / ', two_words: ' / ', last_word: ' / ' }
|
|
290
|
+
).to_f, 1e-9
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def test_parse_human_invalid_style
|
|
294
|
+
assert_raise(ArgumentError) do
|
|
295
|
+
Tins::Duration.parse_human('1m', style: 'abbrev')
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def test_parse_human_raises_on_mismatch
|
|
300
|
+
assert_raise(Tins::Duration::ParserError) do
|
|
301
|
+
Tins::Duration.parse_human('1x and 30y')
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
def test_to_human_basic
|
|
306
|
+
assert_equal '1 minutes and 30 seconds',
|
|
307
|
+
Tins::Duration.new(90).to_human
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def test_to_human_drops_below_max
|
|
311
|
+
assert_equal '1 hours and 2 minutes',
|
|
312
|
+
Tins::Duration.new(3725).to_human(max: 2)
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def test_to_human_single_unit
|
|
316
|
+
assert_equal '1 days', Tins::Duration.new(86_400).to_human
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def test_to_human_zero
|
|
320
|
+
assert_equal '0 seconds', Tins::Duration.new(0).to_human
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def test_to_human_negative
|
|
324
|
+
assert_equal '-1 minutes and 30 seconds',
|
|
325
|
+
Tins::Duration.new(-90).to_human
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
def test_to_human_custom_max
|
|
329
|
+
assert_equal '1 hours, 2 minutes, and 5 seconds',
|
|
330
|
+
Tins::Duration.new(3725).to_human(max: 3)
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
def test_to_human_connector_presets_distinct
|
|
334
|
+
assert_equal '1 hours 2 minutes',
|
|
335
|
+
Tins::Duration.new(3725).to_human(max: 2, connector: :spaces)
|
|
336
|
+
assert_equal '1 hours2 minutes',
|
|
337
|
+
Tins::Duration.new(3725).to_human(max: 2, connector: :none)
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
def test_to_human_truncates_fractional
|
|
341
|
+
assert_equal '1 minutes and 30 seconds',
|
|
342
|
+
Tins::Duration.new(90.999).to_human
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
def test_to_human_style_abbrev
|
|
346
|
+
assert_equal '1m30s',
|
|
347
|
+
Tins::Duration.new(90).to_human(style: :abbrev)
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
def test_to_human_style_abbrev_drops_below_max
|
|
351
|
+
assert_equal '1h2m',
|
|
352
|
+
Tins::Duration.new(3725).to_human(max: 2, style: :abbrev)
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
def test_to_human_style_custom_hash
|
|
356
|
+
assert_equal '1 Min.30 Sek.',
|
|
357
|
+
Tins::Duration.new(90).to_human(
|
|
358
|
+
style: { minutes: ' Min.', seconds: ' Sek.' }
|
|
359
|
+
)
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def test_to_human_style_custom_hash_partial_merge
|
|
363
|
+
assert_equal '1m30 seconds',
|
|
364
|
+
Tins::Duration.new(90).to_human(style: { seconds: ' seconds' })
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def test_to_human_style_invalid_raises
|
|
368
|
+
assert_raise(ArgumentError) do
|
|
369
|
+
Tins::Duration.new(90).to_human(style: 'abbrev')
|
|
370
|
+
end
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
def test_human_styles_keys
|
|
374
|
+
%i[abbrev words].each do |style|
|
|
375
|
+
assert_equal %i[days hours minutes seconds],
|
|
376
|
+
Tins::Duration::HUMAN_STYLES[style].keys
|
|
377
|
+
end
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
def test_human_connectors_keys
|
|
381
|
+
assert_equal %i[none spaces prose],
|
|
382
|
+
Tins::Duration::HUMAN_CONNECTORS.keys
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
def test_style_default_connector
|
|
386
|
+
assert_equal :none,
|
|
387
|
+
Tins::Duration::STYLE_DEFAULT_CONNECTOR[:abbrev]
|
|
388
|
+
assert_equal :prose,
|
|
389
|
+
Tins::Duration::STYLE_DEFAULT_CONNECTOR[:words]
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
def test_to_human_connector_spaces
|
|
393
|
+
assert_equal '1 minutes 30 seconds',
|
|
394
|
+
Tins::Duration.new(90).to_human(connector: :spaces)
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
def test_to_human_connector_none_with_words
|
|
398
|
+
assert_equal '1 minutes30 seconds',
|
|
399
|
+
Tins::Duration.new(90).to_human(connector: :none)
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
def test_to_human_connector_custom_hash
|
|
403
|
+
assert_equal '1 hours / 2 minutes / 5 seconds',
|
|
404
|
+
Tins::Duration.new(3725).to_human(
|
|
405
|
+
max: 3,
|
|
406
|
+
connector: { words: ' / ', two_words: ' / ', last_word: ' / ' }
|
|
407
|
+
)
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def test_to_human_connector_invalid_raises
|
|
411
|
+
assert_raise(ArgumentError) do
|
|
412
|
+
Tins::Duration.new(90).to_human(connector: 'spaces')
|
|
413
|
+
end
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
def test_parse_human_roundtrip_spaces
|
|
417
|
+
[ 90, 3725, 86_400, 93_784 ].each do |s|
|
|
418
|
+
d = Tins::Duration.new(s)
|
|
419
|
+
round = Tins::Duration.parse_human(
|
|
420
|
+
d.to_human(connector: :spaces), connector: :spaces
|
|
421
|
+
)
|
|
422
|
+
assert_equal d, round
|
|
423
|
+
end
|
|
424
|
+
end
|
|
112
425
|
end
|
|
113
426
|
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
require 'tins/xt/enum_joined'
|
|
3
|
+
|
|
4
|
+
module Tins
|
|
5
|
+
class EnumJoinedTest < Test::Unit::TestCase
|
|
6
|
+
def test_empty
|
|
7
|
+
assert_equal '', [].joined
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def test_single_element
|
|
11
|
+
assert_equal 'foo', ['foo'].joined
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_two_elements_default
|
|
15
|
+
assert_equal 'foo and bar', %w[foo bar].joined
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_three_elements_default
|
|
19
|
+
assert_equal 'foo, bar, and baz', %w[foo bar baz].joined
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_four_elements_default
|
|
23
|
+
assert_equal 'a, b, c, and d', %w[a b c d].joined
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_custom_two_words_connector
|
|
27
|
+
assert_equal 'foo-bar',
|
|
28
|
+
%w[foo bar].joined(two_words: '-')
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_custom_words_connector
|
|
32
|
+
assert_equal 'foo / bar / baz',
|
|
33
|
+
%w[foo bar baz].joined(
|
|
34
|
+
words: ' / ', last_word: ' / '
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_custom_last_word_connector
|
|
39
|
+
assert_equal 'foo, barbaz',
|
|
40
|
+
%w[foo bar baz].joined(last_word: '')
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_to_s_coercion
|
|
44
|
+
assert_equal '1 and 2.5', [1, 2.5].joined
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_range
|
|
48
|
+
assert_equal '1 and 2', (1..2).joined
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_custom_enumerable
|
|
52
|
+
enum = Class.new do
|
|
53
|
+
include Enumerable
|
|
54
|
+
def each = %w[x y z].each { |i| yield i }
|
|
55
|
+
end.new
|
|
56
|
+
assert_equal 'x, y, and z', enum.joined
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
data/tests/file_binary_test.rb
CHANGED
data/tests/find_test.rb
CHANGED
data/tests/lines_file_test.rb
CHANGED
data/tests/once_test.rb
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
module Tins
|
|
4
|
+
class OnceTest < Test::Unit::TestCase
|
|
5
|
+
def fn
|
|
6
|
+
@fn ||= File.join(Dir.tmpdir, "once_test.#{SecureRandom.hex(4)}")
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def cleanup
|
|
10
|
+
File.delete(fn) if File.exist?(fn)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_only_once_touch_creates_missing_lock_file
|
|
14
|
+
File.delete(fn) if File.exist?(fn)
|
|
15
|
+
File.delete("#{fn}.old") if File.exist?("#{fn}.old")
|
|
16
|
+
assert_equal 42, Once.only_once(fn, touch: true) { 42 }
|
|
17
|
+
assert File.exist?(fn)
|
|
18
|
+
ensure
|
|
19
|
+
cleanup
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_only_once_without_touch_raises_enoent_when_missing
|
|
23
|
+
File.delete(fn) if File.exist?(fn)
|
|
24
|
+
assert_raise(Errno::ENOENT) do
|
|
25
|
+
Once.only_once(fn) { 42 }
|
|
26
|
+
end
|
|
27
|
+
ensure
|
|
28
|
+
cleanup
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_try_only_once_touch_creates_missing_lock_file
|
|
32
|
+
File.delete(fn) if File.exist?(fn)
|
|
33
|
+
assert_equal :ok, Once.try_only_once(fn, touch: true) { :ok }
|
|
34
|
+
assert File.exist?(fn)
|
|
35
|
+
ensure
|
|
36
|
+
cleanup
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_try_only_once_without_touch_raises_enoent_when_missing
|
|
40
|
+
File.delete(fn) if File.exist?(fn)
|
|
41
|
+
assert_raise(Errno::ENOENT) do
|
|
42
|
+
Once.try_only_once(fn) { :ok }
|
|
43
|
+
end
|
|
44
|
+
ensure
|
|
45
|
+
cleanup
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def test_only_once_releases_lock_after_block
|
|
49
|
+
Once.only_once(fn, touch: true) { :first }
|
|
50
|
+
# A second invocation (fresh FD) must succeed once the first is done.
|
|
51
|
+
assert_equal :second, Once.only_once(fn, touch: true) { :second }
|
|
52
|
+
ensure
|
|
53
|
+
cleanup
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def test_try_only_once_returns_false_when_lock_already_held
|
|
57
|
+
# Cross-process contention: a forked child holds the flock on
|
|
58
|
+
# the lock file; the parent's try_only_once must return false
|
|
59
|
+
# (flock with LOCK_NB returns false, not raise EAGAIN) and the
|
|
60
|
+
# block must be skipped. This is the real-world scenario (two
|
|
61
|
+
# ollama_chat instances in the same directory).
|
|
62
|
+
# `path` is captured *before* the fork so both parent and
|
|
63
|
+
# child reference the same literal string.
|
|
64
|
+
path = fn
|
|
65
|
+
Once.only_once(path, touch: true) { } # ensure file exists
|
|
66
|
+
|
|
67
|
+
r, w = IO.pipe
|
|
68
|
+
pid = fork do
|
|
69
|
+
r.close
|
|
70
|
+
f = File.open(path, File::RDONLY)
|
|
71
|
+
f.flock(File::LOCK_EX)
|
|
72
|
+
w.write('locked')
|
|
73
|
+
w.close
|
|
74
|
+
sleep 60 # hold the lock until killed
|
|
75
|
+
end
|
|
76
|
+
w.close
|
|
77
|
+
r.read(6) # wait for child to confirm lock is held
|
|
78
|
+
r.close
|
|
79
|
+
|
|
80
|
+
assert_equal false, Once.try_only_once(path) { :blocked }
|
|
81
|
+
ensure
|
|
82
|
+
if pid
|
|
83
|
+
begin
|
|
84
|
+
Process.kill('TERM', pid)
|
|
85
|
+
rescue Errno::ESRCH
|
|
86
|
+
# child already exited (a failed assertion took it down first)
|
|
87
|
+
end
|
|
88
|
+
Process.wait(pid)
|
|
89
|
+
end
|
|
90
|
+
cleanup
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
data/tests/secure_write_test.rb
CHANGED
|
@@ -9,14 +9,14 @@ module Tins
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
def test_secure_write
|
|
12
|
-
assert_equal 4, A.secure_write(fn = File.join(Dir.tmpdir, "A_file
|
|
13
|
-
assert_equal 4, A.secure_write(fn = File.join(Dir.tmpdir, "A_file
|
|
12
|
+
assert_equal 4, A.secure_write(fn = File.join(Dir.tmpdir, "A_file.#{SecureRandom.hex(4)}"), 'test')
|
|
13
|
+
assert_equal 4, A.secure_write(fn = File.join(Dir.tmpdir, "A_file.#{SecureRandom.hex(4)}")) { |f| f.write('test') }
|
|
14
14
|
assert_equal 'test', File.read(fn)
|
|
15
15
|
assert_raise(ArgumentError) { A.secure_write }
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def test_secure_write_with_pathname
|
|
19
|
-
assert_equal 4, A.secure_write(fn = Pathname.new(Dir.tmpdir).join("A_file
|
|
19
|
+
assert_equal 4, A.secure_write(fn = Pathname.new(Dir.tmpdir).join("A_file.#{SecureRandom.hex(4)}"), 'test')
|
|
20
20
|
assert_equal 'test', File.read(fn)
|
|
21
21
|
end
|
|
22
22
|
|
|
@@ -36,11 +36,11 @@ module Tins
|
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
def test_write
|
|
39
|
-
assert_equal 4, B.write(fn = File.join(Dir.tmpdir, "B_file
|
|
40
|
-
assert_equal 4, B.write(fn = File.join(Dir.tmpdir, "B_file
|
|
41
|
-
assert_equal 4, IO.write(fn = File.join(Dir.tmpdir, "IO_file
|
|
39
|
+
assert_equal 4, B.write(fn = File.join(Dir.tmpdir, "B_file.#{SecureRandom.hex(4)}"), 'test')
|
|
40
|
+
assert_equal 4, B.write(fn = File.join(Dir.tmpdir, "B_file.#{SecureRandom.hex(4)}")) { |f| f.write('test') }
|
|
41
|
+
assert_equal 4, IO.write(fn = File.join(Dir.tmpdir, "IO_file.#{SecureRandom.hex(4)}"), 'test')
|
|
42
42
|
assert_equal 'test', File.read(fn)
|
|
43
|
-
result = C.write(fn = File.join(Dir.tmpdir, "C_file
|
|
43
|
+
result = C.write(fn = File.join(Dir.tmpdir, "C_file.#{SecureRandom.hex(4)}"), 'test')
|
|
44
44
|
assert_equal [ fn, 'test' ], result
|
|
45
45
|
end
|
|
46
46
|
end
|