knapsack_pro 5.7.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/knapsack_pro.rb CHANGED
@@ -11,7 +11,6 @@ require 'securerandom'
11
11
  require 'timeout'
12
12
  require_relative 'knapsack_pro/urls'
13
13
  require_relative 'knapsack_pro/version'
14
- require_relative 'knapsack_pro/extensions/time'
15
14
  require_relative 'knapsack_pro/hooks/queue'
16
15
  require_relative 'knapsack_pro/utils'
17
16
  require_relative 'knapsack_pro/config/ci/base'
@@ -1,3 +1,5 @@
1
+ require_relative '../../../lib/knapsack_pro/formatters/time_tracker'
2
+
1
3
  describe KnapsackPro::Adapters::RSpecAdapter do
2
4
  it 'backwards compatibility with knapsack gem old rspec adapter name' do
3
5
  expect(KnapsackPro::Adapters::RspecAdapter.new).to be_kind_of(described_class)
@@ -8,7 +10,7 @@ describe KnapsackPro::Adapters::RSpecAdapter do
8
10
  end
9
11
 
10
12
  context do
11
- before { expect(::RSpec).to receive(:configure) }
13
+ before { expect(::RSpec).to receive(:configure).at_least(:once) }
12
14
  it_behaves_like 'adapter'
13
15
  end
14
16
 
@@ -237,59 +239,70 @@ describe KnapsackPro::Adapters::RSpecAdapter do
237
239
  end
238
240
  end
239
241
 
240
- describe '.test_path' do
241
- let(:example_group) do
242
- {
243
- file_path: '1_shared_example.rb',
244
- parent_example_group: {
245
- file_path: '2_shared_example.rb',
246
- parent_example_group: {
247
- file_path: 'a_spec.rb'
248
- }
249
- }
250
- }
242
+ describe '.file_path_for' do
243
+ let(:current_example) { ::RSpec.describe.example }
244
+
245
+ subject { described_class.file_path_for(current_example) }
246
+
247
+ context "when id ends in _spec.rb" do
248
+ it "returns the first part of the id" do
249
+ allow(current_example).to receive(:id).and_return("./foo_spec.rb[1:1]")
250
+
251
+ expect(subject).to eq('./foo_spec.rb')
252
+ end
251
253
  end
252
- let(:current_example) do
253
- OpenStruct.new(metadata: {
254
- example_group: example_group
255
- })
254
+
255
+ context "when id does not end in _spec.rb" do
256
+ it "returns the file_path" do
257
+ allow(current_example).to receive(:id).and_return("./foo.rb")
258
+ allow(current_example).to receive(:metadata).and_return(file_path: "./foo_spec.rb")
259
+
260
+ expect(subject).to eq('./foo_spec.rb')
261
+ end
256
262
  end
257
263
 
258
- subject { described_class.test_path(current_example) }
264
+ context "when id and file_path do not end in _spec.rb" do
265
+ it "returns the example_group's file_path" do
266
+ allow(current_example).to receive(:id).and_return("./foo.rb")
267
+ allow(current_example).to receive(:metadata).and_return(
268
+ file_path: "./foo.rb", example_group: { file_path: "./foo_spec.rb" }
269
+ )
259
270
 
260
- it { should eql 'a_spec.rb' }
271
+ expect(subject).to eq('./foo_spec.rb')
272
+ end
273
+ end
261
274
 
262
- context 'with turnip features' do
263
- describe 'when the turnip version is less than 2' do
264
- let(:example_group) do
265
- {
266
- file_path: "./spec/features/logging_in.feature",
267
- turnip: true,
275
+ context "when id, file_path, and example_group's file_path do not end in _spec.rb" do
276
+ it "returns the top_level_group's file_path" do
277
+ allow(current_example).to receive(:id).and_return("./foo.rb")
278
+ allow(current_example).to receive(:metadata).and_return(
279
+ file_path: "./foo.rb",
280
+ example_group: {
281
+ file_path: "./foo.rb",
268
282
  parent_example_group: {
269
- file_path: "gems/turnip-1.2.4/lib/turnip/rspec.rb"
283
+ file_path: "./foo_spec.rb",
270
284
  }
271
285
  }
272
- end
286
+ )
273
287
 
274
- before { stub_const("Turnip::VERSION", '1.2.4') }
275
-
276
- it { should eql './spec/features/logging_in.feature' }
288
+ expect(subject).to eq('./foo_spec.rb')
277
289
  end
290
+ end
278
291
 
279
- describe 'when turnip is version 2 or greater' do
280
- let(:example_group) do
281
- {
282
- file_path: "gems/turnip-2.0.0/lib/turnip/rspec.rb",
283
- turnip: true,
292
+ context "when id, file_path, example_group's, and top_level_group's file_path do not end in _spec.rb" do
293
+ it "returns empty string" do
294
+ allow(current_example).to receive(:id).and_return("./foo.rb")
295
+ allow(current_example).to receive(:metadata).and_return(
296
+ file_path: "./foo.rb",
297
+ example_group: {
298
+ file_path: "./foo.rb",
284
299
  parent_example_group: {
285
- file_path: "./spec/features/logging_in.feature",
300
+ file_path: "./foo.rb",
286
301
  }
287
302
  }
288
- end
289
-
290
- before { stub_const("Turnip::VERSION", '2.0.0') }
303
+ )
291
304
 
292
- it { should eql './spec/features/logging_in.feature' }
305
+ expect(subject).to eq('')
293
306
  end
294
307
  end
295
308
  end
@@ -298,155 +311,48 @@ describe KnapsackPro::Adapters::RSpecAdapter do
298
311
  let(:config) { double }
299
312
 
300
313
  describe '#bind_time_tracker' do
301
- let(:tracker) { instance_double(KnapsackPro::Tracker) }
302
- let(:logger) { instance_double(Logger) }
303
- let(:global_time) { 'Global time: 01m 05s' }
304
- let(:test_path) { 'spec/a_spec.rb' }
305
314
  let(:current_example) { double(metadata: {}) }
306
315
 
307
316
  context "when the example's metadata has :focus tag AND RSpec inclusion rule includes :focus" do
308
317
  let(:current_example) { double(metadata: { focus: true }) }
318
+ let(:test_path) { 'spec/a_spec.rb' }
309
319
 
310
320
  it do
311
- expect(KnapsackPro::Config::Env).to receive(:rspec_split_by_test_examples?).and_return(false)
312
-
313
- expect(config).to receive(:prepend_before).with(:context).and_yield
314
-
315
- allow(KnapsackPro).to receive(:tracker).and_return(tracker)
316
- expect(tracker).to receive(:start_timer).ordered
317
-
318
321
  expect(config).to receive(:around).with(:each).and_yield(current_example)
319
322
  expect(::RSpec).to receive(:configure).and_yield(config)
320
323
 
321
- expect(tracker).to receive(:current_test_path).ordered.and_return(test_path)
322
- expect(tracker).to receive(:stop_timer).ordered
323
-
324
- expect(described_class).to receive(:test_path).with(current_example).and_return(test_path)
325
-
326
- expect(tracker).to receive(:current_test_path=).with(test_path).ordered
324
+ expect(described_class).to receive(:file_path_for).with(current_example).and_return(test_path)
327
325
 
328
326
  expect(described_class).to receive_message_chain(:rspec_configuration, :filter, :rules, :[]).with(:focus).and_return(true)
329
327
 
330
328
  expect {
331
329
  subject.bind_time_tracker
332
- }.to raise_error /We detected a test file path spec\/a_spec\.rb with a test using the metadata `:focus` tag/
330
+ }.to raise_error /Knapsack Pro found an example tagged with focus in spec\/a_spec\.rb/i
333
331
  end
334
332
  end
335
333
 
336
- context 'when rspec split by test examples is disabled' do
337
- before do
338
- expect(KnapsackPro::Config::Env).to receive(:rspec_split_by_test_examples?).and_return(false)
339
- end
334
+ context 'with no focus' do
335
+ let(:logger) { instance_double(Logger) }
336
+ let(:duration) { 65 }
337
+ let(:global_time) { 'Global time execution for tests: 01m 05s' }
338
+ let(:time_tracker) { instance_double(KnapsackPro::Formatters::TimeTracker) }
340
339
 
341
340
  it 'records time for current test path' do
342
- expect(config).to receive(:prepend_before).with(:context).and_yield
343
-
344
- allow(KnapsackPro).to receive(:tracker).and_return(tracker)
345
- expect(tracker).to receive(:start_timer).ordered
346
-
347
341
  expect(config).to receive(:around).with(:each).and_yield(current_example)
348
- expect(config).to receive(:append_after).with(:context).and_yield
349
342
  expect(config).to receive(:after).with(:suite).and_yield
350
- expect(::RSpec).to receive(:configure).and_yield(config)
351
-
352
- expect(tracker).to receive(:current_test_path).ordered.and_return(test_path)
353
- expect(tracker).to receive(:stop_timer).ordered
354
-
355
- expect(described_class).to receive(:test_path).with(current_example).and_return(test_path)
356
-
357
- expect(tracker).to receive(:current_test_path=).with(test_path).ordered
343
+ expect(::RSpec).to receive(:configure).twice.and_yield(config)
358
344
 
359
345
  expect(current_example).to receive(:run)
360
346
 
361
- expect(tracker).to receive(:stop_timer).ordered
347
+ expect(time_tracker).to receive(:batch_duration).and_return(duration)
348
+ expect(KnapsackPro::Formatters::TimeTrackerFetcher).to receive(:call).and_return(time_tracker)
362
349
 
363
- expect(KnapsackPro::Presenter).to receive(:global_time).and_return(global_time)
364
350
  expect(KnapsackPro).to receive(:logger).and_return(logger)
365
351
  expect(logger).to receive(:debug).with(global_time)
366
352
 
367
353
  subject.bind_time_tracker
368
354
  end
369
355
  end
370
-
371
- context 'when rspec split by test examples is enabled' do
372
- let(:test_example_path) { 'spec/a_spec.rb[1:1]' }
373
-
374
- before do
375
- expect(KnapsackPro::Config::Env).to receive(:rspec_split_by_test_examples?).and_return(true)
376
- end
377
-
378
- context 'when current test_path is a slow test file' do
379
- before do
380
- expect(described_class).to receive(:slow_test_file?).with(described_class, test_path).and_return(true)
381
- end
382
-
383
- it 'records time for example.id' do
384
- expect(current_example).to receive(:id).and_return(test_example_path)
385
-
386
- expect(config).to receive(:prepend_before).with(:context).and_yield
387
-
388
- allow(KnapsackPro).to receive(:tracker).and_return(tracker)
389
- expect(tracker).to receive(:start_timer).ordered
390
-
391
- expect(config).to receive(:around).with(:each).and_yield(current_example)
392
- expect(config).to receive(:append_after).with(:context).and_yield
393
- expect(config).to receive(:after).with(:suite).and_yield
394
- expect(::RSpec).to receive(:configure).and_yield(config)
395
-
396
- expect(tracker).to receive(:current_test_path).ordered.and_return(test_path)
397
- expect(tracker).to receive(:stop_timer).ordered
398
-
399
- expect(described_class).to receive(:test_path).with(current_example).and_return(test_path)
400
-
401
- expect(tracker).to receive(:current_test_path=).with(test_example_path).ordered
402
-
403
- expect(current_example).to receive(:run)
404
-
405
- expect(tracker).to receive(:stop_timer).ordered
406
-
407
- expect(KnapsackPro::Presenter).to receive(:global_time).and_return(global_time)
408
- expect(KnapsackPro).to receive(:logger).and_return(logger)
409
- expect(logger).to receive(:debug).with(global_time)
410
-
411
- subject.bind_time_tracker
412
- end
413
- end
414
-
415
- context 'when current test_path is not a slow test file' do
416
- before do
417
- expect(described_class).to receive(:slow_test_file?).with(described_class, test_path).and_return(false)
418
- end
419
-
420
- it 'records time for current test path' do
421
- expect(config).to receive(:prepend_before).with(:context).and_yield
422
-
423
- allow(KnapsackPro).to receive(:tracker).and_return(tracker)
424
- expect(tracker).to receive(:start_timer).ordered
425
-
426
- expect(config).to receive(:around).with(:each).and_yield(current_example)
427
- expect(config).to receive(:append_after).with(:context).and_yield
428
- expect(config).to receive(:after).with(:suite).and_yield
429
- expect(::RSpec).to receive(:configure).and_yield(config)
430
-
431
- expect(described_class).to receive(:test_path).with(current_example).and_return(test_path)
432
-
433
- expect(tracker).to receive(:current_test_path).ordered.and_return(test_path)
434
- expect(tracker).to receive(:stop_timer).ordered
435
-
436
- expect(tracker).to receive(:current_test_path=).with(test_path).ordered
437
-
438
- expect(current_example).to receive(:run)
439
-
440
- expect(tracker).to receive(:stop_timer).ordered
441
-
442
- expect(KnapsackPro::Presenter).to receive(:global_time).and_return(global_time)
443
- expect(KnapsackPro).to receive(:logger).and_return(logger)
444
- expect(logger).to receive(:debug).with(global_time)
445
-
446
- subject.bind_time_tracker
447
- end
448
- end
449
- end
450
356
  end
451
357
 
452
358
  describe '#bind_save_report' do
@@ -454,7 +360,11 @@ describe KnapsackPro::Adapters::RSpecAdapter do
454
360
  expect(config).to receive(:after).with(:suite).and_yield
455
361
  expect(::RSpec).to receive(:configure).and_yield(config)
456
362
 
457
- expect(KnapsackPro::Report).to receive(:save)
363
+ time_tracker = instance_double(KnapsackPro::Formatters::TimeTracker)
364
+ times = [{ path: "foo_spec.rb", time_execution: 1.0 }]
365
+ expect(time_tracker).to receive(:batch).and_return(times)
366
+ expect(KnapsackPro::Formatters::TimeTrackerFetcher).to receive(:call).and_return(time_tracker)
367
+ expect(KnapsackPro::Report).to receive(:save).with(times)
458
368
 
459
369
  subject.bind_save_report
460
370
  end