knapsack_pro 8.3.3 → 8.4.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.
@@ -1,545 +0,0 @@
1
- # Named _specs.rb on purpose because it hangs if run as part of `bundle exec rspec`.
2
- # Use `bundle exec ruby spec/knapsack_pro/formatters/time_tracker_specs.rb` instead.
3
-
4
- require 'rspec/core'
5
- require 'knapsack_pro'
6
- require 'stringio'
7
- require 'tempfile'
8
- require_relative '../../../lib/knapsack_pro/formatters/time_tracker'
9
-
10
- class TestTimeTracker
11
- def test_single_example
12
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
13
- false
14
- end
15
-
16
- spec = <<~SPEC
17
- describe "KnapsackPro::Formatters::TimeTracker" do
18
- it do
19
- sleep 0.1
20
- expect(1).to eq 1
21
- end
22
- end
23
- SPEC
24
-
25
- run_specs(spec) do |spec_paths, times|
26
- raise unless times.size == 1
27
- raise unless times[0]["path"] == spec_paths.first
28
- raise unless times[0]["time_execution"] > 0.10
29
- raise unless times[0]["time_execution"] < 0.15
30
- end
31
- end
32
-
33
- def test_two_files
34
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
35
- false
36
- end
37
-
38
- spec_1 = <<~SPEC
39
- describe "KnapsackPro::Formatters::TimeTracker 1" do
40
- it do
41
- sleep 0.1
42
- expect(1).to eq 1
43
- end
44
- end
45
- SPEC
46
-
47
- spec_2 = <<~SPEC
48
- describe "KnapsackPro::Formatters::TimeTracker 2" do
49
- it do
50
- sleep 0.2
51
- expect(1).to eq 1
52
- end
53
- end
54
- SPEC
55
-
56
- run_specs([spec_1, spec_2]) do |spec_paths, times|
57
- raise unless times.size == 2
58
- raise unless times.first["path"] == spec_paths.first
59
- raise unless times.first["time_execution"] > 0.10
60
- raise unless times.first["time_execution"] < 0.15
61
- raise unless times.last["path"] == spec_paths.last
62
- raise unless times.last["time_execution"] > 0.20
63
- raise unless times.last["time_execution"] < 0.25
64
- end
65
- end
66
-
67
- def test_failing_example
68
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
69
- false
70
- end
71
-
72
- spec = <<~SPEC
73
- describe "KnapsackPro::Formatters::TimeTracker" do
74
- it do
75
- sleep 0.1
76
- expect(1).to eq 2
77
- end
78
- end
79
- SPEC
80
-
81
- run_specs(spec) do |spec_paths, times|
82
- raise unless times.size == 1
83
- raise unless times[0]["path"] == spec_paths.first
84
- raise unless times[0]["time_execution"] > 0.10
85
- raise unless times[0]["time_execution"] < 0.15
86
- end
87
- end
88
-
89
- def test_pending_example
90
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
91
- false
92
- end
93
-
94
- spec = <<~SPEC
95
- describe "KnapsackPro::Formatters::TimeTracker" do
96
- xit do
97
- sleep 0.1
98
- expect(1).to eq 2
99
- end
100
- end
101
- SPEC
102
-
103
- run_specs(spec) do |spec_paths, times|
104
- raise unless times.size == 1
105
- raise unless times[0]["path"] == spec_paths.first
106
- raise unless times[0]["time_execution"] == 0.0
107
- end
108
- end
109
-
110
- def test_multiple_top_level_groups
111
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
112
- false
113
- end
114
-
115
- spec = <<~SPEC
116
- describe "KnapsackPro::Formatters::TimeTracker 1" do
117
- it do
118
- sleep 0.1
119
- expect(1).to eq 1
120
- end
121
- end
122
-
123
- describe "KnapsackPro::Formatters::TimeTracker 2" do
124
- it do
125
- sleep 0.2
126
- expect(1).to eq 1
127
- end
128
- end
129
- SPEC
130
-
131
- run_specs(spec) do |spec_paths, times|
132
- raise unless times.size == 1
133
- raise unless times[0]["path"] == spec_paths.first
134
- raise unless times[0]["time_execution"] > 0.30
135
- raise unless times[0]["time_execution"] < 0.35
136
- end
137
- end
138
-
139
- def test_rspec_split_by_test_example
140
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
141
- true
142
- end
143
-
144
- spec = <<~SPEC
145
- describe "KnapsackPro::Formatters::TimeTracker 1" do
146
- it do
147
- expect(1).to eq 1
148
- end
149
-
150
- it do
151
- sleep 0.1
152
- expect(1).to eq 1
153
- end
154
- end
155
-
156
- describe "KnapsackPro::Formatters::TimeTracker 2" do
157
- it do
158
- sleep 0.2
159
- expect(1).to eq 1
160
- end
161
-
162
- it do
163
- sleep 0.3
164
- expect(1).to eq 1
165
- end
166
- end
167
- SPEC
168
-
169
- run_specs(spec) do |spec_paths, times|
170
- raise unless times.size == 4
171
- spec_path = spec_paths.first
172
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:1]" }["time_execution"] < 0.05
173
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:2]" }["time_execution"] > 0.10
174
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:2]" }["time_execution"] < 0.15
175
- raise unless times.find { |time| time["path"] == "#{spec_path}[2:1]" }["time_execution"] > 0.20
176
- raise unless times.find { |time| time["path"] == "#{spec_path}[2:1]" }["time_execution"] < 0.25
177
- raise unless times.find { |time| time["path"] == "#{spec_path}[2:2]" }["time_execution"] > 0.30
178
- raise unless times.find { |time| time["path"] == "#{spec_path}[2:2]" }["time_execution"] < 0.35
179
- end
180
- end
181
-
182
- def test_hooks
183
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
184
- false
185
- end
186
-
187
- spec = <<~SPEC
188
- describe "KnapsackPro::Formatters::TimeTracker" do
189
- before(:all) do
190
- sleep 0.1
191
- end
192
-
193
- before(:each) do
194
- sleep 0.1
195
- end
196
-
197
- after(:each) do
198
- sleep 0.1
199
- end
200
-
201
- it do
202
- expect(1).to eq 1
203
- end
204
-
205
- it do
206
- expect(1).to eq 1
207
- end
208
-
209
- after(:all) do
210
- sleep 0.1
211
- end
212
- end
213
- SPEC
214
-
215
- run_specs(spec) do |spec_paths, times|
216
- raise unless times.size == 1
217
- raise unless times[0]["path"] == spec_paths.first
218
- raise unless times[0]["time_execution"] > 0.60
219
- raise unless times[0]["time_execution"] < 0.65
220
- end
221
- end
222
-
223
- def test_nested_hooks
224
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
225
- false
226
- end
227
-
228
- spec = <<~SPEC
229
- describe "KnapsackPro::Formatters::TimeTracker" do
230
- before(:all) do
231
- sleep 0.1
232
- end
233
-
234
- after(:all) do
235
- sleep 0.1
236
- end
237
-
238
- it do
239
- expect(1).to eq 1
240
- end
241
-
242
- describe do
243
- before(:all) do
244
- sleep 0.1
245
- end
246
-
247
- after(:all) do
248
- sleep 0.1
249
- end
250
-
251
- it do
252
- expect(1).to eq 1
253
- end
254
- end
255
-
256
- describe do
257
- before(:all) do
258
- sleep 0.1
259
- end
260
-
261
- after(:all) do
262
- sleep 0.1
263
- end
264
-
265
- it do
266
- expect(1).to eq 1
267
- end
268
- end
269
- end
270
- SPEC
271
-
272
- run_specs(spec) do |spec_paths, times|
273
- raise unless times.size == 1
274
- raise unless times[0]["path"] == spec_paths.first
275
- raise unless times[0]["time_execution"] > 0.60
276
- raise unless times[0]["time_execution"] < 0.65
277
- end
278
- end
279
-
280
- def test_hooks_with_rspec_split_by_test_example
281
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
282
- true
283
- end
284
-
285
- spec = <<~SPEC
286
- describe "KnapsackPro::Formatters::TimeTracker" do
287
- before(:all) do
288
- sleep 0.1
289
- end
290
-
291
- before(:each) do
292
- sleep 0.1
293
- end
294
-
295
- after(:each) do
296
- sleep 0.1
297
- end
298
-
299
- it do
300
- expect(1).to eq 1
301
- end
302
-
303
- it do
304
- expect(1).to eq 1
305
- end
306
-
307
- after(:all) do
308
- sleep 0.1
309
- end
310
- end
311
- SPEC
312
-
313
- run_specs(spec) do |spec_paths, times|
314
- raise unless times.size == 2
315
- spec_path = spec_paths.first
316
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:1]" }["time_execution"] > 0.40
317
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:1]" }["time_execution"] < 0.45
318
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:2]" }["time_execution"] > 0.40
319
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:2]" }["time_execution"] < 0.45
320
- end
321
- end
322
-
323
- def test_nested_hooks_with_rspec_split_by_test_example
324
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
325
- true
326
- end
327
-
328
- spec = <<~SPEC
329
- describe "KnapsackPro::Formatters::TimeTracker" do
330
- before(:all) do
331
- sleep 0.1
332
- end
333
-
334
- after(:all) do
335
- sleep 0.1
336
- end
337
-
338
- it do
339
- expect(1).to eq 1
340
- end
341
-
342
- describe do
343
- before(:all) do
344
- sleep 0.1
345
- end
346
-
347
- after(:all) do
348
- sleep 0.1
349
- end
350
-
351
- it do
352
- expect(1).to eq 1
353
- end
354
- end
355
-
356
- describe do
357
- before(:all) do
358
- sleep 0.1
359
- end
360
-
361
- after(:all) do
362
- sleep 0.1
363
- end
364
-
365
- it do
366
- expect(1).to eq 1
367
- end
368
- end
369
- end
370
- SPEC
371
-
372
- run_specs(spec) do |spec_paths, times|
373
- raise unless times.size == 3
374
- spec_path = spec_paths.first
375
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:1]" }["time_execution"] > 0.20
376
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:1]" }["time_execution"] < 0.25
377
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:2:1]" }["time_execution"] > 0.40
378
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:2:1]" }["time_execution"] < 0.45
379
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:3:1]" }["time_execution"] > 0.40
380
- raise unless times.find { |time| time["path"] == "#{spec_path}[1:3:1]" }["time_execution"] < 0.45
381
- end
382
- end
383
-
384
-
385
- def test_unknown_path
386
- KnapsackPro::Formatters::TimeTracker.class_eval do
387
- alias_method :original_file_path_for, :file_path_for
388
-
389
- define_method(:file_path_for) do |_example|
390
- ""
391
- end
392
- end
393
-
394
- spec = <<~SPEC
395
- describe "KnapsackPro::Formatters::TimeTracker" do
396
- it do
397
- expect(1).to eq 1
398
- end
399
- end
400
- SPEC
401
-
402
- run_specs(spec) do |spec_paths, times|
403
- raise unless times.size == 1
404
- raise unless times[0]["path"] == spec_paths.first
405
- raise unless times[0]["time_execution"] == 0.0
406
- end
407
-
408
- ensure
409
- KnapsackPro::Formatters::TimeTracker.class_eval do
410
- undef :file_path_for
411
- alias_method :file_path_for, :original_file_path_for
412
- end
413
- end
414
-
415
- def test_empty_group
416
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
417
- false
418
- end
419
-
420
- spec = <<~SPEC
421
- describe "KnapsackPro::Formatters::TimeTracker" do
422
- end
423
- SPEC
424
-
425
- run_specs(spec) do |spec_paths, times|
426
- raise unless times.size == 1
427
- raise unless times[0]["path"] == spec_paths.first
428
- raise unless times[0]["time_execution"] == 0.0
429
- end
430
- end
431
-
432
- def test_duration
433
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
434
- false
435
- end
436
-
437
- spec = <<~SPEC
438
- describe "KnapsackPro::Formatters::TimeTracker" do
439
- it do
440
- expect(1).to eq 1
441
- end
442
- end
443
- SPEC
444
-
445
- run_specs(spec) do |_, _, time_tracker|
446
- raise unless time_tracker.duration > 0.0
447
- end
448
- end
449
-
450
- def test_unexecuted_test_files
451
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
452
- false
453
- end
454
-
455
- spec = <<~SPEC
456
- describe "KnapsackPro::Formatters::TimeTracker" do
457
- xit do
458
- end
459
- end
460
- SPEC
461
-
462
- run_specs(spec) do |spec_paths, _, time_tracker|
463
- unexecuted_test_files = ["foo_spec.rb", "bar_spec.rb"]
464
- # Need to filter because RSpec keeps accumulating state.
465
- time_tracker.scheduled_paths = spec_paths + unexecuted_test_files
466
- files = time_tracker
467
- .unexecuted_test_files
468
- .filter { |file| spec_paths.include?(file) || unexecuted_test_files.include?(file) }
469
-
470
- raise unless files.size == 3
471
- end
472
- end
473
-
474
- def test_subset
475
- KnapsackPro::Formatters::TimeTracker.define_method(:rspec_split_by_test_example?) do |_file|
476
- false
477
- end
478
-
479
- spec = <<~SPEC
480
- describe "KnapsackPro::Formatters::TimeTracker" do
481
- it "works" do
482
- sleep 0.1
483
- expect(1).to eq 1
484
- end
485
- end
486
- SPEC
487
-
488
- run_specs(spec) do |spec_paths, times, time_tracker|
489
- # Need to filter because RSpec keeps accumulating state.
490
- files = time_tracker
491
- .batch
492
- .filter { |file| spec_paths.include?(file["path"]) }
493
-
494
- raise unless files.size == 1
495
- raise unless files[0]["path"] == spec_paths.first
496
- raise unless files[0]["time_execution"] > 0.10
497
- raise unless files[0]["time_execution"] < 0.15
498
- end
499
- end
500
-
501
- private
502
-
503
- def run_specs(specs)
504
- files = Array(specs).map.with_index do |spec, i|
505
- file = Tempfile.new(["tmp_time_tracker_#{i}", "_spec.rb"], "./spec/knapsack_pro/formatters/")
506
- file.write(spec)
507
- file.rewind
508
- file
509
- end
510
-
511
- paths = files.map(&:path).map { _1.sub("./", "") }
512
-
513
- options = ::RSpec::Core::ConfigurationOptions.new([
514
- "--format", KnapsackPro::Formatters::TimeTracker.to_s,
515
- *paths,
516
- ])
517
- runner = ::RSpec::Core::Runner.new(options)
518
- runner.run(StringIO.new, StringIO.new)
519
-
520
- time_tracker = runner.configuration.formatters.find { |f| f.class.to_s == KnapsackPro::Formatters::TimeTracker.to_s }
521
- # Need to filter because RSpec keeps accumulating state.
522
- time_tracker.scheduled_paths = paths
523
- times = time_tracker
524
- .queue
525
- .sort_by { |time| time["path"] }
526
- .filter do |time|
527
- paths.any? { |path| time["path"].start_with?(path) }
528
- end
529
- yield(paths, times, time_tracker)
530
-
531
- ensure
532
- # Need to reset because RSpec keeps reusing the same instance.
533
- time_tracker.instance_variable_set(:@queue, {}) if time_tracker
534
- time_tracker.instance_variable_set(:@started, time_tracker.send(:now)) if time_tracker
535
- end
536
- end
537
-
538
- TestTimeTracker
539
- .instance_methods
540
- .filter { |method| method.to_s.start_with?("test_") }
541
- .shuffle
542
- .each do |method|
543
- puts method
544
- TestTimeTracker.new.public_send(method)
545
- end