puppetlabs_spec_helper 9.0.0 → 9.0.1

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.
@@ -4,6 +4,13 @@ require 'spec_helper'
4
4
  require 'puppetlabs_spec_helper/tasks/fixtures'
5
5
 
6
6
  describe PuppetlabsSpecHelper::Tasks::FixtureHelpers do
7
+ # Reset memoized state on the module object between tests
8
+ before do
9
+ %i[@repositories @forge_modules @symlinks @logger @module_target_dir @max_thread_limit].each do |var|
10
+ described_class.remove_instance_variable(var) if described_class.instance_variable_defined?(var)
11
+ end
12
+ end
13
+
7
14
  describe '.module_name' do
8
15
  subject(:module_name) { described_class.module_name }
9
16
 
@@ -178,7 +185,7 @@ describe PuppetlabsSpecHelper::Tasks::FixtureHelpers do
178
185
  it 'correctly sets --forge_authorization' do
179
186
  allow(ENV).to receive(:fetch).with('FORGE_API_KEY', nil).and_return('myforgeapikey')
180
187
  # Mock the system call to prevent actual execution
181
- allow_any_instance_of(Kernel).to receive(:system) do |command| # rubocop:disable RSpec/AnyInstance
188
+ allow_any_instance_of(Kernel).to receive(:system) do |command|
182
189
  expect(command).to include('--forge_authorization "Bearer myforgeapikey"')
183
190
  # Simulate setting $CHILD_STATUS to a successful status
184
191
  allow($CHILD_STATUS).to receive(:success?).and_return(true)
@@ -282,4 +289,676 @@ describe PuppetlabsSpecHelper::Tasks::FixtureHelpers do
282
289
  end
283
290
  end
284
291
  end
292
+
293
+ describe '.module_version' do
294
+ subject(:helper) { described_class }
295
+
296
+ context 'when metadata.json is readable and contains a version' do
297
+ before do
298
+ allow(File).to receive(:file?).with('mymodule/metadata.json').and_return(true)
299
+ allow(File).to receive(:readable?).with('mymodule/metadata.json').and_return(true)
300
+ allow(File).to receive(:read).with('mymodule/metadata.json').and_return('{"version":"2.0.0"}')
301
+ end
302
+
303
+ it 'returns the version' do
304
+ expect(helper.module_version('mymodule')).to eq('2.0.0')
305
+ end
306
+ end
307
+
308
+ context 'when metadata.json has no version key' do
309
+ before do
310
+ allow(File).to receive(:file?).with('mymodule/metadata.json').and_return(true)
311
+ allow(File).to receive(:readable?).with('mymodule/metadata.json').and_return(true)
312
+ allow(File).to receive(:read).with('mymodule/metadata.json').and_return('{}')
313
+ end
314
+
315
+ it 'returns 0.0.1' do
316
+ expect(helper.module_version('mymodule')).to eq('0.0.1')
317
+ end
318
+ end
319
+
320
+ context 'when metadata.json does not exist' do
321
+ before do
322
+ allow(File).to receive(:file?).with('mymodule/metadata.json').and_return(false)
323
+ end
324
+
325
+ it 'returns 0.0.1' do
326
+ expect(helper.module_version('mymodule')).to eq('0.0.1')
327
+ end
328
+ end
329
+
330
+ context 'when metadata.json contains invalid JSON' do
331
+ before do
332
+ allow(File).to receive(:file?).with('mymodule/metadata.json').and_return(true)
333
+ allow(File).to receive(:readable?).with('mymodule/metadata.json').and_return(true)
334
+ allow(File).to receive(:read).with('mymodule/metadata.json').and_return('{invalid}')
335
+ end
336
+
337
+ it 'returns 0.0.1' do
338
+ expect(helper.module_version('mymodule')).to eq('0.0.1')
339
+ end
340
+ end
341
+ end
342
+
343
+ describe '.clone_repo' do
344
+ subject(:helper) { described_class }
345
+
346
+ before do
347
+ allow(helper).to receive(:system).and_return(true)
348
+ allow(File).to receive(:exist?).with('target').and_return(true)
349
+ end
350
+
351
+ it 'clones a git repo with --depth 1 when no ref is given' do
352
+ expect(helper).to receive(:system).with(a_string_including('git clone --depth 1'))
353
+ helper.clone_repo('git', 'https://example.com/repo.git', 'target')
354
+ end
355
+
356
+ it 'clones a git repo without --depth 1 when a ref is given' do
357
+ expect(helper).to receive(:system).with(satisfy { |s| !s.include?('--depth 1') })
358
+ helper.clone_repo('git', 'https://example.com/repo.git', 'target', nil, 'abc123')
359
+ end
360
+
361
+ it 'includes the branch flag when branch is given' do
362
+ expect(helper).to receive(:system).with(a_string_including('-b mybranch'))
363
+ helper.clone_repo('git', 'https://example.com/repo.git', 'target', nil, nil, 'mybranch')
364
+ end
365
+
366
+ it 'includes custom flags when given' do
367
+ expect(helper).to receive(:system).with(a_string_including('--some-flag'))
368
+ helper.clone_repo('git', 'https://example.com/repo.git', 'target', nil, nil, nil, '--some-flag')
369
+ end
370
+
371
+ it 'clones an hg repo' do
372
+ expect(helper).to receive(:system).with(a_string_including('hg clone'))
373
+ helper.clone_repo('hg', 'https://example.com/hgrepo', 'target')
374
+ end
375
+
376
+ it 'includes hg branch flag when branch is given' do
377
+ expect(helper).to receive(:system).with(a_string_including('-b stable'))
378
+ helper.clone_repo('hg', 'https://example.com/hgrepo', 'target', nil, nil, 'stable')
379
+ end
380
+
381
+ it 'raises for unsupported scm' do
382
+ expect { helper.clone_repo('svn', 'url', 'target') }.to raise_error(/not supported/)
383
+ end
384
+
385
+ it 'raises when target does not exist after clone' do
386
+ allow(File).to receive(:exist?).with('missing').and_return(false)
387
+ allow(helper).to receive(:system).and_return(true)
388
+ expect { helper.clone_repo('git', 'url', 'missing') }.to raise_error(/Failed to clone/)
389
+ end
390
+ end
391
+
392
+ describe '.update_repo' do
393
+ subject(:helper) { described_class }
394
+
395
+ before do
396
+ allow(helper).to receive(:system).and_return(true)
397
+ allow(helper).to receive(:shallow_git_repo?).and_return(false)
398
+ end
399
+
400
+ it 'runs git fetch for a normal git repo' do
401
+ expect(helper).to receive(:system).with('git fetch', chdir: 'target')
402
+ helper.update_repo('git', 'target')
403
+ end
404
+
405
+ it 'runs git fetch --unshallow for a shallow git repo' do
406
+ allow(helper).to receive(:shallow_git_repo?).and_return(true)
407
+ expect(helper).to receive(:system).with('git fetch --unshallow', chdir: 'target')
408
+ helper.update_repo('git', 'target')
409
+ end
410
+
411
+ it 'runs hg pull' do
412
+ expect(helper).to receive(:system).with('hg pull', chdir: 'target')
413
+ helper.update_repo('hg', 'target')
414
+ end
415
+
416
+ it 'raises for unsupported scm' do
417
+ expect { helper.update_repo('svn', 'target') }.to raise_error(/not supported/)
418
+ end
419
+ end
420
+
421
+ describe '.shallow_git_repo?' do
422
+ subject(:helper) { described_class }
423
+
424
+ it 'returns true when .git/shallow exists' do
425
+ allow(File).to receive(:file?).with(File.join('.git', 'shallow')).and_return(true)
426
+ expect(helper.shallow_git_repo?).to be true
427
+ end
428
+
429
+ it 'returns false when .git/shallow does not exist' do
430
+ allow(File).to receive(:file?).with(File.join('.git', 'shallow')).and_return(false)
431
+ expect(helper.shallow_git_repo?).to be false
432
+ end
433
+ end
434
+
435
+ describe '.revision' do
436
+ subject(:helper) { described_class }
437
+
438
+ before do
439
+ allow(helper).to receive(:system).and_return(true)
440
+ end
441
+
442
+ it 'runs git reset --hard for git scm' do
443
+ expect(helper).to receive(:system).with('git reset --hard abc123', chdir: 'target')
444
+ helper.revision('git', 'target', 'abc123')
445
+ end
446
+
447
+ it 'runs hg update for hg scm' do
448
+ expect(helper).to receive(:system).with('hg update --clean -r abc123', chdir: 'target')
449
+ helper.revision('hg', 'target', 'abc123')
450
+ end
451
+
452
+ it 'raises for unsupported scm' do
453
+ expect { helper.revision('svn', 'target', 'ref') }.to raise_error(/not supported/)
454
+ end
455
+
456
+ it 'raises when system command fails' do
457
+ allow(helper).to receive(:system).and_return(false)
458
+ expect { helper.revision('git', 'target', 'badref') }.to raise_error(/Invalid ref/)
459
+ end
460
+ end
461
+
462
+ describe '.valid_repo?' do
463
+ subject(:helper) { described_class }
464
+
465
+ it 'returns false when target is not a directory' do
466
+ allow(File).to receive(:directory?).with('target').and_return(false)
467
+ expect(helper.valid_repo?('git', 'target', 'remote')).to be false
468
+ end
469
+
470
+ it 'returns true for hg when directory exists' do
471
+ allow(File).to receive(:directory?).with('target').and_return(true)
472
+ expect(helper.valid_repo?('hg', 'target', 'remote')).to be true
473
+ end
474
+
475
+ it 'returns true when git remote matches' do
476
+ allow(File).to receive(:directory?).with('target').and_return(true)
477
+ allow(helper).to receive(:git_remote_url).with('target').and_return('https://example.com/repo.git')
478
+ expect(helper.valid_repo?('git', 'target', 'https://example.com/repo.git')).to be true
479
+ end
480
+
481
+ it 'removes target and returns false when git remote does not match' do
482
+ allow(File).to receive(:directory?).with('target').and_return(true)
483
+ allow(helper).to receive(:git_remote_url).with('target').and_return('https://example.com/other.git')
484
+ expect(FileUtils).to receive(:rm_rf).with('target')
485
+ expect(helper.valid_repo?('git', 'target', 'https://example.com/repo.git')).to be false
486
+ end
487
+ end
488
+
489
+ describe '.git_remote_url' do
490
+ subject(:helper) { described_class }
491
+
492
+ it 'returns the stripped remote URL on success' do
493
+ allow(Open3).to receive(:capture2e).and_return(["https://example.com/repo.git\n", double(success?: true)])
494
+ expect(helper.git_remote_url('target')).to eq('https://example.com/repo.git')
495
+ end
496
+
497
+ it 'returns nil on failure' do
498
+ allow(Open3).to receive(:capture2e).and_return(['', double(success?: false)])
499
+ expect(helper.git_remote_url('target')).to be_nil
500
+ end
501
+ end
502
+
503
+ describe '.remove_subdirectory' do
504
+ subject(:helper) { described_class }
505
+
506
+ it 'returns immediately when subdir is nil' do
507
+ expect(Dir).not_to receive(:mktmpdir)
508
+ helper.remove_subdirectory('target', nil)
509
+ end
510
+
511
+ it 'moves subdir contents up to target and removes the subdir' do
512
+ Dir.mktmpdir do |base|
513
+ target = File.join(base, 'target')
514
+ FileUtils.mkdir_p(File.join(target, 'sub'))
515
+ File.write(File.join(target, 'sub', 'file.txt'), 'content')
516
+ helper.remove_subdirectory(target, 'sub')
517
+ expect(File.exist?(File.join(target, 'file.txt'))).to be true
518
+ expect(File.directory?(File.join(target, 'sub'))).to be false
519
+ end
520
+ end
521
+ end
522
+
523
+ describe '.logger' do
524
+ subject(:helper) { described_class }
525
+
526
+ it 'returns a Logger instance' do
527
+ expect(helper.logger).to be_a(Logger)
528
+ end
529
+
530
+ it 'memoizes the logger' do
531
+ expect(helper.logger).to equal(helper.logger)
532
+ end
533
+
534
+ it 'uses INFO level by default' do
535
+ allow(ENV).to receive(:[]).and_call_original
536
+ allow(ENV).to receive(:[]).with('ENABLE_LOGGER').and_return(nil)
537
+ described_class.remove_instance_variable(:@logger) if described_class.instance_variable_defined?(:@logger)
538
+ expect(helper.logger.level).to eq(Logger::INFO)
539
+ end
540
+
541
+ it 'uses DEBUG level when ENABLE_LOGGER is set' do
542
+ allow(ENV).to receive(:[]).and_call_original
543
+ allow(ENV).to receive(:[]).with('ENABLE_LOGGER').and_return('true')
544
+ described_class.remove_instance_variable(:@logger) if described_class.instance_variable_defined?(:@logger)
545
+ expect(helper.logger.level).to eq(Logger::DEBUG)
546
+ end
547
+ end
548
+
549
+ describe '.module_working_directory' do
550
+ subject(:helper) { described_class }
551
+
552
+ it 'returns the default working directory' do
553
+ allow(ENV).to receive(:fetch).and_call_original
554
+ allow(ENV).to receive(:fetch).with('MODULE_WORKING_DIR', nil).and_return(nil)
555
+ expect(helper.module_working_directory).to eq(File.expand_path('spec/fixtures/work-dir'))
556
+ end
557
+
558
+ it 'uses MODULE_WORKING_DIR env when set' do
559
+ allow(ENV).to receive(:[]).and_call_original
560
+ allow(ENV).to receive(:[]).with('MODULE_WORKING_DIR').and_return('/custom/path')
561
+ expect(helper.module_working_directory).to eq('/custom/path')
562
+ end
563
+ end
564
+
565
+ describe '.current_thread_count' do
566
+ subject(:helper) { described_class }
567
+
568
+ it 'returns 0 when no threads are tracked' do
569
+ items = { 'a' => {}, 'b' => {} }
570
+ expect(helper.current_thread_count(items)).to eq(0)
571
+ end
572
+
573
+ it 'returns 0 when all tracked threads have finished' do
574
+ t = Thread.new { nil }
575
+ t.join
576
+ items = { 'a' => { thread: t } }
577
+ expect(helper.current_thread_count(items)).to eq(0)
578
+ end
579
+
580
+ it 'counts threads that are still running' do
581
+ ready = Queue.new
582
+ done = Queue.new
583
+ t = Thread.new do
584
+ ready.push(true)
585
+ done.pop
586
+ end
587
+ ready.pop
588
+ items = { 'a' => { thread: t } }
589
+ count = helper.current_thread_count(items)
590
+ done.push(true)
591
+ t.join
592
+ expect(count).to eq(1)
593
+ end
594
+ end
595
+
596
+ describe '.max_thread_limit' do
597
+ subject(:helper) { described_class }
598
+
599
+ it 'defaults to 10' do
600
+ allow(ENV).to receive(:[]).and_call_original
601
+ allow(ENV).to receive(:[]).with('MAX_FIXTURE_THREAD_COUNT').and_return(nil)
602
+ expect(helper.max_thread_limit).to eq(10)
603
+ end
604
+
605
+ it 'reads MAX_FIXTURE_THREAD_COUNT when set' do
606
+ allow(ENV).to receive(:[]).and_call_original
607
+ allow(ENV).to receive(:[]).with('MAX_FIXTURE_THREAD_COUNT').and_return('3')
608
+ expect(helper.max_thread_limit).to eq(3)
609
+ end
610
+ end
611
+
612
+ describe '.download_items' do
613
+ subject(:helper) { described_class }
614
+
615
+ it 'calls the block for each item and waits for all threads' do
616
+ results = []
617
+ mutex = Mutex.new
618
+ items = { 'remote1' => {}, 'remote2' => {} }
619
+ helper.download_items(items) { |remote, _opts| mutex.synchronize { results << remote } }
620
+ expect(results).to contain_exactly('remote1', 'remote2')
621
+ end
622
+ end
623
+
624
+ describe '.setup_symlink' do
625
+ subject(:helper) { described_class }
626
+
627
+ it 'skips creation when the symlink already exists' do
628
+ allow(File).to receive(:symlink?).with('link/path').and_return(true)
629
+ expect(FileUtils).not_to receive(:ln_sf)
630
+ helper.setup_symlink('target', { 'target' => 'link/path' })
631
+ end
632
+
633
+ it 'creates a symlink on non-Windows systems' do
634
+ allow(File).to receive(:symlink?).with('link/path').and_return(false)
635
+ allow(helper).to receive(:windows?).and_return(false)
636
+ expect(FileUtils).to receive(:ln_sf).with('the_target', 'link/path')
637
+ helper.setup_symlink('the_target', { 'target' => 'link/path' })
638
+ end
639
+ end
640
+
641
+ describe '.download_repository' do
642
+ subject(:helper) { described_class }
643
+
644
+ let(:opts) do
645
+ { 'target' => 'spec/fixtures/modules/mymod', 'scm' => 'git',
646
+ 'ref' => nil, 'branch' => nil, 'flags' => nil, 'subdir' => nil, }
647
+ end
648
+
649
+ before do
650
+ allow(helper).to receive(:valid_repo?).and_return(false)
651
+ allow(helper).to receive(:clone_repo)
652
+ allow(helper).to receive(:update_repo)
653
+ allow(helper).to receive(:revision)
654
+ allow(helper).to receive(:remove_subdirectory)
655
+ end
656
+
657
+ it 'calls clone_repo when repo is not valid' do
658
+ expect(helper).to receive(:clone_repo)
659
+ helper.download_repository('https://example.com/repo.git', opts)
660
+ end
661
+
662
+ it 'calls update_repo when repo is valid' do
663
+ allow(helper).to receive(:valid_repo?).and_return(true)
664
+ expect(helper).to receive(:update_repo).with('git', 'spec/fixtures/modules/mymod')
665
+ helper.download_repository('https://example.com/repo.git', opts)
666
+ end
667
+
668
+ it 'calls revision when ref is set' do
669
+ allow(helper).to receive(:valid_repo?).and_return(true)
670
+ expect(helper).to receive(:revision).with('git', 'spec/fixtures/modules/mymod', 'abc123')
671
+ helper.download_repository('https://example.com/repo.git', opts.merge('ref' => 'abc123'))
672
+ end
673
+
674
+ it 'calls remove_subdirectory when subdir is set' do
675
+ allow(helper).to receive(:valid_repo?).and_return(true)
676
+ expect(helper).to receive(:remove_subdirectory).with('spec/fixtures/modules/mymod', 'sub')
677
+ helper.download_repository('https://example.com/repo.git', opts.merge('subdir' => 'sub'))
678
+ end
679
+
680
+ it 'uses opts scm when provided' do
681
+ allow(helper).to receive(:valid_repo?).and_return(false)
682
+ expect(helper).to receive(:clone_repo).with('hg', anything, anything, anything, anything, anything, anything)
683
+ helper.download_repository('https://example.com/repo', opts.merge('scm' => 'hg'))
684
+ end
685
+ end
686
+
687
+ describe '.module_target_dir' do
688
+ subject(:helper) { described_class }
689
+
690
+ it 'returns the expanded path to spec/fixtures/modules' do
691
+ expect(helper.module_target_dir).to eq(File.expand_path('spec/fixtures/modules'))
692
+ end
693
+
694
+ it 'is memoized' do
695
+ expect(helper.module_target_dir).to equal(helper.module_target_dir)
696
+ end
697
+ end
698
+
699
+ describe '.download_module (additional cases)' do
700
+ subject(:helper) { described_class }
701
+
702
+ before do
703
+ RSpec::Mocks.configuration.allow_message_expectations_on_nil = true
704
+ allow(ENV).to receive(:fetch).and_call_original
705
+ allow(ENV).to receive(:fetch).with('FORGE_API_KEY', nil).and_return(nil)
706
+ end
707
+
708
+ after do
709
+ RSpec::Mocks.configuration.allow_message_expectations_on_nil = false
710
+ end
711
+
712
+ context 'when opts is a String' do
713
+ it 'uses the string as the target and returns false when already installed' do
714
+ allow(File).to receive(:directory?).with('direct/target').and_return(true)
715
+ allow(helper).to receive(:module_version).with('direct/target').and_return('0.0.1')
716
+ expect(helper.download_module('puppetlabs-stdlib', 'direct/target')).to be false
717
+ end
718
+ end
719
+
720
+ context 'when the module is already installed at the requested version' do
721
+ let(:opts) { { 'target' => 'spec/fixtures/modules/stdlib', 'ref' => '1.0.0' } }
722
+
723
+ it 'returns false without downloading' do
724
+ allow(File).to receive(:directory?).with('spec/fixtures/modules/stdlib').and_return(true)
725
+ allow(helper).to receive(:module_version).with('spec/fixtures/modules/stdlib').and_return('1.0.0')
726
+ expect(helper.download_module('puppetlabs-stdlib', opts)).to be false
727
+ end
728
+ end
729
+
730
+ context 'when the module needs to be downloaded' do
731
+ let(:opts) { { 'target' => 'spec/fixtures/modules/stdlib' } }
732
+
733
+ it 'runs puppet module install' do
734
+ allow(File).to receive(:directory?).with('spec/fixtures/modules/stdlib').and_return(false)
735
+ `true` # ensure $CHILD_STATUS is a real successful status
736
+ allow(helper).to receive(:system).and_return(true)
737
+ expect(helper).to receive(:system).with(a_string_including('puppet module install'))
738
+ helper.download_module('puppetlabs-stdlib', opts)
739
+ end
740
+ end
741
+ end
742
+
743
+ describe '.fixtures (additional coverage)' do
744
+ subject(:helper) { described_class }
745
+
746
+ before do
747
+ allow(ENV).to receive(:[]).and_call_original
748
+ allow(ENV).to receive(:[]).with('FIXTURES_YML').and_return(nil)
749
+ end
750
+
751
+ context 'when FIXTURES_YML env is set to a valid file path' do
752
+ before do
753
+ allow(ENV).to receive(:[]).with('FIXTURES_YML').and_return('.fixtures.yml')
754
+ allow(YAML).to receive(:load_file).with('.fixtures.yml').and_return('fixtures' => {})
755
+ end
756
+
757
+ it 'uses the FIXTURES_YML path (line 76)' do
758
+ expect(helper.fixtures('forge_modules')).to eq({})
759
+ end
760
+ end
761
+
762
+ context 'when .fixtures.yaml exists but .fixtures.yml does not' do
763
+ before do
764
+ allow(File).to receive(:exist?).with('.fixtures.yml').and_return(false)
765
+ allow(File).to receive(:exist?).with('.fixtures.yaml').and_return(true)
766
+ allow(YAML).to receive(:load_file).with('.fixtures.yaml').and_return('fixtures' => {})
767
+ end
768
+
769
+ it 'falls back to .fixtures.yaml (line 80)' do
770
+ expect(helper.fixtures('forge_modules')).to eq({})
771
+ end
772
+ end
773
+
774
+ context 'when FIXTURES_YML is set but the file is missing' do
775
+ before do
776
+ allow(ENV).to receive(:[]).with('FIXTURES_YML').and_return('nonexistent.yml')
777
+ allow(YAML).to receive(:load_file).with('nonexistent.yml').and_raise(Errno::ENOENT)
778
+ end
779
+
780
+ it 'raises a friendly error (line 92)' do
781
+ expect { helper.fixtures('forge_modules') }.to raise_error(/Fixtures file not found/)
782
+ end
783
+ end
784
+
785
+ context 'when a git repository fixture has a valid (slash-free) ref' do
786
+ before do
787
+ allow(File).to receive(:exist?).with('.fixtures.yml').and_return(true)
788
+ allow(YAML).to receive(:load_file).with('.fixtures.yml').and_return(
789
+ 'fixtures' => {
790
+ 'repositories' => {
791
+ 'stdlib' => {
792
+ 'scm' => 'git',
793
+ 'repo' => 'https://github.com/puppetlabs/puppetlabs-stdlib.git',
794
+ 'ref' => 'v8.5.0',
795
+ },
796
+ },
797
+ },
798
+ )
799
+ end
800
+
801
+ it 'returns the fixture hash (validate_fixture_hash! line 155)' do
802
+ result = helper.fixtures('repositories')
803
+ expect(result).to include('https://github.com/puppetlabs/puppetlabs-stdlib.git')
804
+ end
805
+ end
806
+ end
807
+
808
+ describe '.download_items (throttling)' do
809
+ subject(:helper) { described_class }
810
+
811
+ it 'throttles threads to stay within max_thread_limit' do
812
+ m = described_class
813
+ m.remove_instance_variable(:@max_thread_limit) if m.instance_variable_defined?(:@max_thread_limit)
814
+ allow(ENV).to receive(:[]).and_call_original
815
+ allow(ENV).to receive(:[]).with('MAX_FIXTURE_THREAD_COUNT').and_return('1')
816
+
817
+ results = []
818
+ mutex = Mutex.new
819
+ items = { 'slow' => {}, 'fast' => {} }
820
+
821
+ helper.download_items(items) do |remote, _opts|
822
+ # Slow the first thread so the main loop reaches 'fast' while it is
823
+ # still alive, triggering the throttle path (lines 312-316).
824
+ sleep(0.02) if remote == 'slow'
825
+ mutex.synchronize { results << remote }
826
+ end
827
+
828
+ expect(results).to contain_exactly('slow', 'fast')
829
+ end
830
+ end
831
+
832
+ describe '.setup_symlink (Windows paths)' do
833
+ subject(:helper) { described_class }
834
+
835
+ context 'on a Windows system with Dir.create_junction available' do
836
+ before do
837
+ allow(File).to receive(:symlink?).with('link/path').and_return(false)
838
+ allow(helper).to receive(:windows?).and_return(true)
839
+ allow(Dir).to receive(:respond_to?).and_call_original
840
+ allow(Dir).to receive(:respond_to?).with(:create_junction).and_return(true)
841
+ allow(Dir).to receive(:create_junction)
842
+ end
843
+
844
+ it 'adjusts relative target path and calls Dir.create_junction (lines 332-334)' do
845
+ expect(Dir).to receive(:create_junction)
846
+ helper.setup_symlink('target', { 'target' => 'link/path' })
847
+ end
848
+ end
849
+
850
+ context 'on a Windows system without Dir.create_junction' do
851
+ before do
852
+ allow(File).to receive(:symlink?).with('link/path').and_return(false)
853
+ allow(helper).to receive(:windows?).and_return(true)
854
+ allow(Dir).to receive(:respond_to?).and_call_original
855
+ allow(Dir).to receive(:respond_to?).with(:create_junction).and_return(false)
856
+ allow(helper).to receive(:system)
857
+ end
858
+
859
+ it 'falls back to mklink (line 336)' do
860
+ expect(helper).to receive(:system).with(a_string_including('mklink'))
861
+ helper.setup_symlink('target', { 'target' => 'link/path' })
862
+ end
863
+ end
864
+ end
865
+ end
866
+
867
+ describe 'rake spec_prep', type: :task do
868
+ before do
869
+ # Reset memoized state so fixture lookups go through FakeFS
870
+ %i[@repositories @forge_modules @symlinks @logger @module_target_dir @max_thread_limit].each do |var|
871
+ m = PuppetlabsSpecHelper::Tasks::FixtureHelpers
872
+ m.remove_instance_variable(var) if m.instance_variable_defined?(var)
873
+ end
874
+ allow(ENV).to receive(:[]).and_call_original
875
+ allow(ENV).to receive(:[]).with('FIXTURES_YML').and_return(nil)
876
+ allow(ENV).to receive(:fetch).and_call_original
877
+ allow(ENV).to receive(:fetch).with('FORGE_API_KEY', nil).and_return(nil)
878
+ # Use an empty fixtures file so only the auto-symlink runs
879
+ File.write('.fixtures.yml', "fixtures:\n symlinks:\n")
880
+ end
881
+
882
+ it 'creates the spec/fixtures/modules directory' do
883
+ task.execute
884
+ expect(File.directory?('spec/fixtures/modules')).to be true
885
+ end
886
+
887
+ it 'creates spec/fixtures/manifests/site.pp' do
888
+ task.execute
889
+ expect(File.exist?('spec/fixtures/manifests/site.pp')).to be true
890
+ end
891
+
892
+ context 'on Windows (lines 412, 414)' do
893
+ before do
894
+ # windows? is called as an instance method on main inside the task block
895
+ allow_any_instance_of(Object).to receive(:windows?).and_return(true)
896
+ # suppress the real mklink system call that setup_symlink issues on Windows
897
+ allow_any_instance_of(Object).to receive(:system).and_return(true)
898
+ end
899
+
900
+ it 'attempts to require win32/dir without error' do
901
+ expect { task.execute }.not_to raise_error
902
+ end
903
+ end
904
+ end
905
+
906
+ describe 'rake spec_clean', type: :task do
907
+ before do
908
+ %i[@repositories @forge_modules @symlinks @logger @module_target_dir @max_thread_limit].each do |var|
909
+ m = PuppetlabsSpecHelper::Tasks::FixtureHelpers
910
+ m.remove_instance_variable(var) if m.instance_variable_defined?(var)
911
+ end
912
+ allow(ENV).to receive(:[]).and_call_original
913
+ allow(ENV).to receive(:[]).with('FIXTURES_YML').and_return(nil)
914
+ allow(ENV).to receive(:fetch).and_call_original
915
+ allow(ENV).to receive(:fetch).with('MODULE_WORKING_DIR', nil).and_return(nil)
916
+ File.write('.fixtures.yml', "fixtures:\n symlinks:\n")
917
+ # Create the site.pp so spec_clean can remove it
918
+ FileUtils.mkdir_p('spec/fixtures/manifests')
919
+ FileUtils.touch('spec/fixtures/manifests/site.pp')
920
+ # Ensure spec_clean_symlinks task can be re-invoked
921
+ Rake::Task[:spec_clean_symlinks].reenable
922
+ end
923
+
924
+ it 'runs without raising an error' do
925
+ expect { task.execute }.not_to raise_error
926
+ end
927
+
928
+ it 'removes an empty site.pp' do
929
+ task.execute
930
+ expect(File.exist?('spec/fixtures/manifests/site.pp')).to be false
931
+ end
932
+
933
+ context 'with repositories and forge modules (lines 434-435, 439-440)' do
934
+ before do
935
+ # repositories/forge_modules are called as instance methods on main inside the task block
936
+ allow_any_instance_of(Object).to receive(:repositories).and_return(
937
+ 'https://example.com/repo.git' => { 'target' => 'spec/fixtures/modules/myrepo' },
938
+ )
939
+ allow_any_instance_of(Object).to receive(:forge_modules).and_return(
940
+ 'puppetlabs-stdlib' => { 'target' => 'spec/fixtures/modules/stdlib' },
941
+ )
942
+ end
943
+
944
+ it 'removes repository and forge module targets' do
945
+ expect { task.execute }.not_to raise_error
946
+ end
947
+ end
948
+ end
949
+
950
+ describe 'rake spec_clean_symlinks', type: :task do
951
+ before do
952
+ %i[@repositories @forge_modules @symlinks @logger @module_target_dir @max_thread_limit].each do |var|
953
+ m = PuppetlabsSpecHelper::Tasks::FixtureHelpers
954
+ m.remove_instance_variable(var) if m.instance_variable_defined?(var)
955
+ end
956
+ allow(ENV).to receive(:[]).and_call_original
957
+ allow(ENV).to receive(:[]).with('FIXTURES_YML').and_return(nil)
958
+ File.write('.fixtures.yml', "fixtures:\n symlinks:\n")
959
+ end
960
+
961
+ it 'runs without raising an error' do
962
+ expect { task.execute }.not_to raise_error
963
+ end
285
964
  end