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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4cd4445a342012f408a122df72e4cc1b1fd9ea91ed4d10c438e56374313bc7f
4
- data.tar.gz: 3ca527e89669a54feb5e31b8295d0e7360332be23b700d1f1ee8c2e2d60846af
3
+ metadata.gz: 6b95848727f123d17da1f22c9b36da32616dc3a9c3ddd046a5dabd11ef935771
4
+ data.tar.gz: 6f06ea9d8b695cf9b8158319108c8169a0d8c8d8fb669e3e250893c20198df4b
5
5
  SHA512:
6
- metadata.gz: d4ff8c4c842191dcece38c2b846e6ac1601ebfd3156839b9b5eacd38acc5a28eef889c78577f5feee70d040301706362d112c4a7de5f04ea8af4180e3ee716ff
7
- data.tar.gz: 656a6365b0074ba0dfca155399f6aad0e5012dd1d3c7740928fd2a4edf5b48106dcbdcba2b1cba223e7d4f494cc8f979e7cea514c12dd4ee449362acd7ad3177
6
+ metadata.gz: ff57232c40aade285175321c3c9bc7668df6bd055f439732def884fab06440c19fc8a01bc777e48600a2b90de578703c3df213dd73df589f6db9e3974d35aa55
7
+ data.tar.gz: 2e80efb6293957388fa41903a0db190261d6b20abf072568d0ee61d8225fc0b3b43f1d8c1388086487f9974a5b38fee133a247f7e27550267bd4e8ca9527e8bb
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PuppetlabsSpecHelper
4
- VERSION = '9.0.0'
4
+ VERSION = '9.0.1'
5
5
  end
data/spec/spec_helper.rb CHANGED
@@ -26,6 +26,9 @@ if ENV['COVERAGE'] == 'yes'
26
26
  end
27
27
  end
28
28
 
29
+ # pp must be required before FakeFS activates; pp.rb defines `class File < IO`
30
+ # which conflicts with FakeFS::File < StringIO if loaded while FakeFS is active.
31
+ require 'pp' # rubocop:disable Lint/RedundantRequireStatement
29
32
  require 'fakefs/spec_helpers'
30
33
 
31
34
  FakeFS::Pathname.class_eval do
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'spec_helper'
5
+
6
+ # Create a real fixtures/modules/stdlib directory so that the Dir.exist? path
7
+ # in module_spec_helper.rb is taken (covering lines 64, 66, 68, 69).
8
+ FileUtils.mkdir_p('spec/fixtures/modules/stdlib')
9
+
10
+ # Set MODULEPATH so that line 22 (module_path join) is covered.
11
+ ENV['MODULEPATH'] = 'spec/fixtures/modules'
12
+
13
+ # Requiring module_spec_helper covers all load-time lines. When COVERAGE=yes
14
+ # (i.e. `rake spec:coverage`), ENV['SIMPLECOV'] is already 'yes' so the
15
+ # SimpleCov configuration block (lines 26-57) is also exercised.
16
+ require 'puppetlabs_spec_helper/module_spec_helper'
17
+
18
+ RSpec.configure do |c|
19
+ c.after(:suite) do
20
+ FileUtils.rm_rf('spec/fixtures/modules') if File.directory?('spec/fixtures/modules')
21
+ ENV.delete('MODULEPATH')
22
+ end
23
+ end
24
+
25
+ RSpec.describe 'module_spec_helper' do
26
+ describe 'param_value helper' do
27
+ # param_value is defined at the top level by module_spec_helper.rb (line 7-9).
28
+ # Calling it here covers the method body (line 8).
29
+ it 'returns the named parameter value from a catalog resource' do
30
+ mock_params = { content: 'hello' }
31
+ mock_resource = double('resource', parameters: mock_params)
32
+ mock_subject = double('catalog', resource: mock_resource)
33
+
34
+ expect(param_value(mock_subject, 'File', '/tmp/test', 'content')).to eq('hello')
35
+ end
36
+ end
37
+
38
+ describe 'verify_contents helper' do
39
+ # verify_contents is defined at the top level (lines 11-14).
40
+ # Calling it covers lines 12-13.
41
+ it 'passes when the resource content includes all expected lines' do
42
+ mock_resource = double('resource', parameters: { content: "line1\nline2\nline3" })
43
+ mock_subject = double('catalog', resource: mock_resource)
44
+
45
+ expect { verify_contents(mock_subject, '/tmp/test', %w[line1 line2]) }.not_to raise_error
46
+ end
47
+ end
48
+
49
+ describe 'RSpec configuration applied by module_spec_helper' do
50
+ # Running any example here triggers the before(:each) hook registered by
51
+ # module_spec_helper (lines 83-89), covering lines 84 and 85.
52
+ it 'configures the module_path setting' do
53
+ expect(RSpec.configuration.module_path).to include('spec/fixtures/modules')
54
+ end
55
+
56
+ it 'sets strict_variables' do
57
+ expect(RSpec.configuration.strict_variables).to be(true).or be(false)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'puppetlabs_spec_helper/puppetlabs_spec/files'
5
+
6
+ RSpec.describe PuppetlabsSpec::Files do
7
+ # Reset global tempfiles state around each example
8
+ around do |example|
9
+ saved = $global_tempfiles
10
+ $global_tempfiles = []
11
+ example.run
12
+ $global_tempfiles = saved
13
+ end
14
+
15
+ describe '.in_tmp' do
16
+ it 'returns true when path is inside the system tmpdir' do
17
+ path = File.join(Dir.tmpdir, 'some', 'nested', 'file')
18
+ expect(described_class.in_tmp(path)).to be true
19
+ end
20
+
21
+ it 'returns false when path is not inside the system tmpdir' do
22
+ expect(described_class.in_tmp('/etc/passwd')).to be false
23
+ end
24
+ end
25
+
26
+ describe '.cleanup' do
27
+ it 'removes files recorded in $global_tempfiles' do
28
+ path = Dir.mktmpdir
29
+ $global_tempfiles = [path]
30
+ described_class.cleanup
31
+ expect(File.exist?(path)).to be false
32
+ end
33
+
34
+ it 'does not raise when a recorded path no longer exists' do
35
+ $global_tempfiles = [File.join(Dir.tmpdir, "already_gone_#{rand}")]
36
+ expect { described_class.cleanup }.not_to raise_error
37
+ end
38
+
39
+ it 'raises when a recorded path is outside tmpdir' do
40
+ $global_tempfiles = ['/etc/some_file']
41
+ expect { described_class.cleanup }.to raise_error(/Not deleting tmpfile/)
42
+ end
43
+ end
44
+
45
+ describe '#tmpfilename' do
46
+ include described_class
47
+
48
+ it 'returns a path string' do
49
+ expect(tmpfilename('test')).to be_a(String)
50
+ end
51
+
52
+ it 'records the path in $global_tempfiles' do
53
+ path = tmpfilename('test')
54
+ expect($global_tempfiles).to include(File.expand_path(path))
55
+ end
56
+
57
+ it 'returns a path that does not exist (file is deleted after name generation)' do
58
+ path = tmpfilename('test')
59
+ expect(File.exist?(path)).to be false
60
+ end
61
+ end
62
+
63
+ describe '#tmpdir' do
64
+ include described_class
65
+
66
+ it 'returns a path to an existing directory' do
67
+ path = tmpdir('testdir')
68
+ expect(File.directory?(path)).to be true
69
+ end
70
+
71
+ it 'records the path in $global_tempfiles' do
72
+ path = tmpdir('testdir')
73
+ expect($global_tempfiles).to include(File.expand_path(path))
74
+ end
75
+ end
76
+
77
+ describe '#make_absolute' do
78
+ include described_class
79
+
80
+ it 'expands a relative path to an absolute path' do
81
+ result = make_absolute('some/relative/path')
82
+ expect(result).to eq(File.expand_path('some/relative/path'))
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'puppetlabs_spec_helper/puppetlabs_spec_helper'
5
+
6
+ RSpec.describe PuppetlabsSpec::Fixtures do
7
+ # fixture_helpers_spec.rb's describe '.fixtures' before block undefs all
8
+ # PuppetlabsSpec::Fixtures methods. Reload before each example to restore them.
9
+ # Use an isolated object to avoid method-resolution conflicts with
10
+ # PuppetlabsSpecHelper::Tasks::FixtureHelpers#fixtures (also available on Object).
11
+ subject(:obj) { Object.new.extend(described_class) }
12
+
13
+ around do |example|
14
+ load File.expand_path('../../../../lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb', __dir__)
15
+ example.run
16
+ end
17
+
18
+ let(:fixture_dir) { PuppetlabsSpec::FIXTURE_DIR }
19
+
20
+ describe '#fixtures' do
21
+ it 'returns a path joined under FIXTURE_DIR' do
22
+ expect(obj.fixtures('modules', 'stdlib')).to eq(File.join(fixture_dir, 'modules', 'stdlib'))
23
+ end
24
+
25
+ it 'returns FIXTURE_DIR itself with no extra segments' do
26
+ expect(obj.fixtures).to eq(fixture_dir)
27
+ end
28
+ end
29
+
30
+ describe '#my_fixture_dir' do
31
+ it 'returns a path derived from this spec file under FIXTURE_DIR' do
32
+ result = obj.my_fixture_dir
33
+ expect(result).to start_with(fixture_dir)
34
+ end
35
+
36
+ it 'raises when the caller stack has no spec file' do
37
+ allow(obj).to receive(:caller).and_return(['some/regular/file.rb:10'])
38
+ expect { obj.my_fixture_dir }.to raise_error(/couldn't work out/)
39
+ end
40
+ end
41
+
42
+ describe '#my_fixture' do
43
+ let(:dir) { obj.my_fixture_dir }
44
+
45
+ around do |example|
46
+ FileUtils.mkdir_p(dir)
47
+ File.write(File.join(dir, 'test.json'), '{}')
48
+ example.run
49
+ ensure
50
+ FileUtils.rm_rf(dir)
51
+ end
52
+
53
+ it 'returns the path to a readable fixture file' do
54
+ expect(obj.my_fixture('test.json')).to eq(File.join(dir, 'test.json'))
55
+ end
56
+
57
+ it 'raises when the fixture file does not exist' do
58
+ expect { obj.my_fixture('missing.json') }.to raise_error(/is not readable/)
59
+ end
60
+ end
61
+
62
+ describe '#my_fixture_read' do
63
+ let(:dir) { obj.my_fixture_dir }
64
+
65
+ around do |example|
66
+ FileUtils.mkdir_p(dir)
67
+ File.write(File.join(dir, 'data.txt'), 'hello')
68
+ example.run
69
+ ensure
70
+ FileUtils.rm_rf(dir)
71
+ end
72
+
73
+ it 'returns the contents of the fixture file' do
74
+ expect(obj.my_fixture_read('data.txt')).to eq('hello')
75
+ end
76
+ end
77
+
78
+ describe '#my_fixtures' do
79
+ let(:dir) { obj.my_fixture_dir }
80
+
81
+ around do |example|
82
+ FileUtils.mkdir_p(dir)
83
+ File.write(File.join(dir, 'a.json'), '{}')
84
+ File.write(File.join(dir, 'b.json'), '{}')
85
+ example.run
86
+ ensure
87
+ FileUtils.rm_rf(dir)
88
+ end
89
+
90
+ it 'returns all files matching the glob' do
91
+ expect(obj.my_fixtures('*.json').size).to eq(2)
92
+ end
93
+
94
+ it 'yields each matching file to a block' do
95
+ yielded = []
96
+ obj.my_fixtures('*.json') { |f| yielded << f }
97
+ expect(yielded.size).to eq(2)
98
+ end
99
+
100
+ it 'raises an error when no files match the glob' do
101
+ expect { obj.my_fixtures('*.xml') }.to raise_error(/had no files/)
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'puppetlabs_spec_helper/puppetlabs_spec/matchers'
5
+
6
+ RSpec.describe 'have_matching_element matcher' do
7
+ it 'matches when any element matches the pattern' do
8
+ expect(%w[foo bar baz]).to have_matching_element(/bar/)
9
+ end
10
+
11
+ it 'does not match when no element matches' do
12
+ expect(%w[foo bar]).not_to have_matching_element(/qux/)
13
+ end
14
+ end
15
+
16
+ RSpec.describe 'exit_with matcher' do
17
+ # exit_with takes a callable (proc) as the actual value, not a block
18
+ it 'matches a proc that exits with the expected code' do
19
+ expect(proc { exit(0) }).to exit_with(0)
20
+ end
21
+
22
+ it 'does not match when exit is not called' do
23
+ expect(proc { 1 + 1 }).not_to exit_with(0)
24
+ end
25
+
26
+ it 'does not match when a different exit code is used' do
27
+ expect(proc { exit(1) }).not_to exit_with(0)
28
+ end
29
+
30
+ it 'includes "exit was not called" in failure message when block does not exit' do
31
+ matcher = exit_with(0)
32
+ matcher.matches?(proc { 1 + 1 })
33
+ expect(matcher.failure_message).to include('exit was not called')
34
+ end
35
+
36
+ it 'includes the actual exit code in failure message when wrong code used' do
37
+ matcher = exit_with(0)
38
+ matcher.matches?(proc { exit(1) })
39
+ expect(matcher.failure_message).to include('exited with 1')
40
+ end
41
+
42
+ it 'has a failure_message_when_negated' do
43
+ matcher = exit_with(0)
44
+ expect(matcher.failure_message_when_negated).to include('0')
45
+ end
46
+
47
+ it 'has a description' do
48
+ expect(exit_with(42).description).to include('42')
49
+ end
50
+ end
51
+
52
+ RSpec.describe 'have_printed matcher' do
53
+ # have_printed takes a callable (proc) as the actual value, not a block
54
+ it 'matches when the proc prints the expected string' do
55
+ expect(proc { print 'hello world' }).to have_printed('hello world')
56
+ end
57
+
58
+ it 'matches when the proc prints output matching a regexp' do
59
+ expect(proc { print 'hello world' }).to have_printed(/world/)
60
+ end
61
+
62
+ it 'does not match when the proc prints something else' do
63
+ expect(proc { print 'goodbye' }).not_to have_printed('hello')
64
+ end
65
+
66
+ it 'raises ArgumentError for unsupported match types' do
67
+ matcher = have_printed(42)
68
+ expect { matcher.matches?(proc { print 'x' }) }.to raise_error(ArgumentError, /No idea how to match/)
69
+ end
70
+
71
+ it 'includes the expected string in the failure message when wrong output was printed' do
72
+ matcher = have_printed('expected_string')
73
+ matcher.matches?(proc { print 'something else' })
74
+ expect(matcher.failure_message).to include('expected_string')
75
+ end
76
+
77
+ it 'has a description including the expected value' do
78
+ expect(have_printed('hello').description).to include('hello')
79
+ end
80
+
81
+ it 'says "nothing was printed" in the failure message when not yet applied' do
82
+ matcher = have_printed('hello')
83
+ expect(matcher.failure_message).to include('nothing was printed')
84
+ end
85
+ end