File: fixture_helpers_spec.rb

package info (click to toggle)
ruby-puppetlabs-spec-helper 8.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 344 kB
  • sloc: ruby: 1,526; sh: 8; makefile: 6
file content (285 lines) | stat: -rw-r--r-- 10,331 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# frozen_string_literal: true

require 'spec_helper'
require 'puppetlabs_spec_helper/tasks/fixtures'

describe PuppetlabsSpecHelper::Tasks::FixtureHelpers do
  describe '.module_name' do
    subject(:module_name) { described_class.module_name }

    before do
      allow(Dir).to receive(:pwd).and_return(File.join('path', 'to', 'my-awsome-module_from_pwd'))
    end

    shared_examples 'module name from working directory' do
      it 'determines the module name from the working directory name' do
        expect(module_name).to eq('module_from_pwd')
      end
    end

    shared_examples 'module name from metadata' do
      it 'determines the module name from the module metadata' do
        expect(module_name).to eq('module_from_metadata')
      end
    end

    context 'when metadata.json does not exist' do
      before do
        allow(File).to receive(:file?).with('metadata.json').and_return(false)
      end

      it_behaves_like 'module name from working directory'
    end

    context 'when metadata.json does exist' do
      before do
        allow(File).to receive(:file?).with('metadata.json').and_return(true)
      end

      context 'when it is not readable' do
        before do
          allow(File).to receive(:readable?).with('metadata.json').and_return(false)
        end

        it_behaves_like 'module name from working directory'
      end

      context 'when it is readable' do
        before do
          allow(File).to receive(:readable?).with('metadata.json').and_return(true)
          allow(File).to receive(:read).with('metadata.json').and_return(metadata_content)
        end

        context 'when it contains invalid JSON' do
          let(:metadata_content) { '{ "name": "my-awesome-module_from_metadata", }' }

          it_behaves_like 'module name from working directory'
        end

        context 'when it contains a name value' do
          let(:metadata_content) { '{ "name": "my-awesome-module_from_metadata" }' }

          it_behaves_like 'module name from metadata'
        end

        context 'when it does not contain a name value' do
          let(:metadata_content) { '{}' }

          it_behaves_like 'module name from working directory'
        end

        context 'when the name has a null value' do
          let(:metadata_content) { '{ "name": null }' }

          it_behaves_like 'module name from working directory'
        end

        context 'when the name is blank' do
          let(:metadata_content) { '{ "name": "" }' }

          it_behaves_like 'module name from working directory'
        end
      end
    end
  end

  describe '.fixtures' do
    subject(:helper) { described_class }

    before do
      # Unstub the fixtures "helpers"
      PuppetlabsSpec::Fixtures.instance_methods.each do |m|
        PuppetlabsSpec::Fixtures.send(:undef_method, m)
      end
      allow(File).to receive(:exist?).with('.fixtures.yml').and_return false
      allow(File).to receive(:exist?).with('.fixtures.yaml').and_return false
      allow(ENV).to receive(:[]).and_call_original
      allow(ENV).to receive(:[]).with('FIXTURES_YML').and_return(nil)
      allow(described_class).to receive(:auto_symlink).and_return('project' => source_dir.to_s)
    end

    context 'when file is missing' do
      it 'returns basic directories per category' do
        expect(helper.fixtures('forge_modules')).to eq({})
        expect(helper.fixtures('repositories')).to eq({})
      end
    end

    context 'when file is empty' do
      it 'returns basic directories per category' do
        allow(File).to receive(:exist?).with('.fixtures.yml').and_return true
        allow(YAML).to receive(:load_file).with('.fixtures.yml').and_return false
        expect(helper.fixtures('forge_modules')).to eq({})
        expect(helper.fixtures('repositories')).to eq({})
      end
    end

    context 'when file is malformed' do
      it 'raises an error' do
        expect(File).to receive(:exist?).with('.fixtures.yml').and_return true
        expect(YAML).to receive(:load_file).with('.fixtures.yml').and_raise(Psych::SyntaxError.new('/file', '123', '0', '0', 'spec message', 'spec context'))
        expect { helper.fixtures('forge_modules') }.to raise_error(RuntimeError, /malformed YAML/)
      end
    end

    context 'when file contains no fixtures' do
      it 'raises an error' do
        allow(File).to receive(:exist?).with('.fixtures.yml').and_return true
        allow(YAML).to receive(:load_file).with('.fixtures.yml').and_return('some' => 'key')
        expect { helper.fixtures('forge_modules') }.to raise_error(RuntimeError, /No 'fixtures'/)
      end
    end

    context 'when file specifies fixtures' do
      it 'returns the hash' do
        allow(File).to receive(:exist?).with('.fixtures.yml').and_return true
        allow(YAML).to receive(:load_file).with('.fixtures.yml').and_return('fixtures' => { 'forge_modules' => { 'stdlib' => 'puppetlabs-stdlib' } })
        expect(helper.fixtures('forge_modules')).to eq(
          'puppetlabs-stdlib' => {
            'target' => 'spec/fixtures/modules/stdlib',
            'ref' => nil,
            'branch' => nil,
            'scm' => nil,
            'flags' => nil,
            'subdir' => nil,
          },
        )
      end
    end

    context 'when file specifies defaults' do
      it 'returns the hash' do
        allow(File).to receive(:exist?).with('.fixtures.yml').and_return true
        allow(YAML).to receive(:load_file).with('.fixtures.yml').and_return('defaults' => { 'forge_modules' => { 'flags' => '--module_repository=https://myforge.example.com/' } },
                                                                            'fixtures' => { 'forge_modules' => { 'stdlib' => 'puppetlabs-stdlib' } })
        expect(helper.fixtures('forge_modules')).to eq(
          'puppetlabs-stdlib' => {
            'target' => 'spec/fixtures/modules/stdlib',
            'ref' => nil,
            'branch' => nil,
            'scm' => nil,
            'flags' => '--module_repository=https://myforge.example.com/',
            'subdir' => nil,
          },
        )
      end
    end

    context 'when forge_api_key env variable is set' do
      before do
        # required to prevent unwanted output on stub of $CHILD_STATUS
        RSpec::Mocks.configuration.allow_message_expectations_on_nil = true
      end

      after do
        RSpec::Mocks.configuration.allow_message_expectations_on_nil = false
      end

      it 'correctly sets --forge_authorization' do
        allow(ENV).to receive(:fetch).with('FORGE_API_KEY', nil).and_return('myforgeapikey')
        # Mock the system call to prevent actual execution
        allow_any_instance_of(Kernel).to receive(:system) do |command| # rubocop:disable RSpec/AnyInstance
          expect(command).to include('--forge_authorization "Bearer myforgeapikey"')
          # Simulate setting $CHILD_STATUS to a successful status
          allow($CHILD_STATUS).to receive(:success?).and_return(true)
          true
        end
        helper.download_module('puppetlabs-stdlib', 'target' => 'spec/fixtures/modules/stdlib')
      end
    end

    context 'when file specifies repository fixtures' do
      before do
        allow(File).to receive(:exist?).with('.fixtures.yml').and_return true
        allow(YAML).to receive(:load_file).with('.fixtures.yml').and_return(
          'fixtures' => {
            'repositories' => { 'stdlib' => 'https://github.com/puppetlabs/puppetlabs-stdlib.git' },
          },
        )
      end

      it 'returns the hash' do
        expect(helper.repositories).to eq(
          'https://github.com/puppetlabs/puppetlabs-stdlib.git' => {
            'target' => 'spec/fixtures/modules/stdlib',
            'ref' => nil,
            'branch' => nil,
            'scm' => nil,
            'flags' => nil,
            'subdir' => nil,
          },
        )
      end
    end

    context 'when file specifies repository fixtures with an invalid git ref' do
      before do
        allow(File).to receive(:exist?).with('.fixtures.yml').and_return true
        allow(YAML).to receive(:load_file).with('.fixtures.yml').and_return(
          'fixtures' => {
            'repositories' => {
              'stdlib' => {
                'scm' => 'git',
                'repo' => 'https://github.com/puppetlabs/puppetlabs-stdlib.git',
                'ref' => 'this/is/a/branch',
              },
            },
          },
        )
      end

      it 'raises an ArgumentError' do
        expect { helper.fixtures('repositories') }.to raise_error(ArgumentError)
      end
    end

    context 'when file specifies puppet version' do
      def stub_fixtures(data)
        allow(File).to receive(:exist?).with('.fixtures.yml').and_return true
        allow(YAML).to receive(:load_file).with('.fixtures.yml').and_return(data)
      end

      it 'includes the fixture if the puppet version matches', if: Gem::Version.new(Puppet::PUPPETVERSION) > Gem::Version.new('4') do
        stub_fixtures(
          'fixtures' => {
            'forge_modules' => {
              'stdlib' => {
                'repo' => 'puppetlabs-stdlib',
                'puppet_version' => Puppet::PUPPETVERSION,
              },
            },
          },
        )
        expect(helper.fixtures('forge_modules')).to include('puppetlabs-stdlib')
      end

      it 'excludes the fixture if the puppet version does not match', if: Gem::Version.new(Puppet::PUPPETVERSION) > Gem::Version.new('4') do
        stub_fixtures(
          'fixtures' => {
            'forge_modules' => {
              'stdlib' => {
                'repo' => 'puppetlabs-stdlib',
                'puppet_version' => '>= 999.9.9',
              },
            },
          },
        )
        expect(helper.fixtures('forge_modules')).to eq({})
      end

      it 'includes the fixture on obsolete puppet versions', if: Gem::Version.new(Puppet::PUPPETVERSION) <= Gem::Version.new('4') do
        stub_fixtures(
          'fixtures' => {
            'forge_modules' => {
              'stdlib' => {
                'repo' => 'puppetlabs-stdlib',
                'puppet_version' => Puppet::PUPPETVERSION,
              },
            },
          },
        )
        expect(helper.fixtures('forge_modules')).to include('puppetlabs-stdlib')
      end
    end
  end
end