summaryrefslogtreecommitdiff
path: root/spec/unit/module_tool/metadata_spec.rb
blob: fce9c8f8dab8d0a97e1d94b8e83d45235a8302a1 (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
require 'spec_helper'
require 'puppet/module_tool'

describe Puppet::ModuleTool::Metadata do
  let(:data) { {} }
  let(:metadata) { Puppet::ModuleTool::Metadata.new }

  describe 'property lookups' do
    subject { metadata }

    %w[ name version author summary license source project_page issues_url
    dependencies dashed_name release_name description ].each do |prop|
      describe "##{prop}" do
        it "responds to the property" do
          subject.send(prop)
        end
      end
    end
  end

  describe "#update" do
    subject { metadata.update(data) }

    context "with a valid name" do
      let(:data) { { 'name' => 'billgates-mymodule' } }

      it "extracts the author name from the name field" do
        subject.to_hash['author'].should == 'billgates'
      end

      it "extracts a module name from the name field" do
        subject.module_name.should == 'mymodule'
      end

      context "and existing author" do
        before { metadata.update('author' => 'foo') }

        it "avoids overwriting the existing author" do
          subject.to_hash['author'].should == 'foo'
        end
      end
    end

    context "with a valid name and author" do
      let(:data) { { 'name' => 'billgates-mymodule', 'author' => 'foo' } }

      it "use the author name from the author field" do
        subject.to_hash['author'].should == 'foo'
      end

      context "and preexisting author" do
        before { metadata.update('author' => 'bar') }

        it "avoids overwriting the existing author" do
          subject.to_hash['author'].should == 'foo'
        end
      end
    end

    context "with an invalid name" do
      context "(short module name)" do
        let(:data) { { 'name' => 'mymodule' } }

        it "raises an exception" do
          expect { subject }.to raise_error(ArgumentError, "Invalid 'name' field in metadata.json: the field must be a namespaced module name")
        end
      end

      context "(missing namespace)" do
        let(:data) { { 'name' => '/mymodule' } }

        it "raises an exception" do
          expect { subject }.to raise_error(ArgumentError, "Invalid 'name' field in metadata.json: the field must be a namespaced module name")
        end
      end

      context "(missing module name)" do
        let(:data) { { 'name' => 'namespace/' } }

        it "raises an exception" do
          expect { subject }.to raise_error(ArgumentError, "Invalid 'name' field in metadata.json: the field must be a namespaced module name")
        end
      end

      context "(invalid namespace)" do
        let(:data) { { 'name' => "dolla'bill$-mymodule" } }

        it "raises an exception" do
          expect { subject }.to raise_error(ArgumentError, "Invalid 'name' field in metadata.json: the namespace contains non-alphanumeric characters")
        end
      end

      context "(non-alphanumeric module name)" do
        let(:data) { { 'name' => "dollabils-fivedolla'" } }

        it "raises an exception" do
          expect { subject }.to raise_error(ArgumentError, "Invalid 'name' field in metadata.json: the module name contains non-alphanumeric (or underscore) characters")
        end
      end

      context "(module name starts with a number)" do
        let(:data) { { 'name' => "dollabills-5dollars" } }

        it "raises an exception" do
          expect { subject }.to raise_error(ArgumentError, "Invalid 'name' field in metadata.json: the module name must begin with a letter")
        end
      end
    end


    context "with an invalid version" do
      let(:data) { { 'version' => '3.0' } }

      it "raises an exception" do
        expect { subject }.to raise_error(ArgumentError, "Invalid 'version' field in metadata.json: version string cannot be parsed as a valid Semantic Version")
      end
    end

    context "with a valid source" do
      context "which is a GitHub URL" do
        context "with a scheme" do
          before { metadata.update('source' => 'https://github.com/billgates/amazingness') }

          it "predicts a default project_page" do
            subject.to_hash['project_page'].should == 'https://github.com/billgates/amazingness'
          end

          it "predicts a default issues_url" do
            subject.to_hash['issues_url'].should == 'https://github.com/billgates/amazingness/issues'
          end
        end

        context "without a scheme" do
          before { metadata.update('source' => 'github.com/billgates/amazingness') }

          it "predicts a default project_page" do
            subject.to_hash['project_page'].should == 'https://github.com/billgates/amazingness'
          end

          it "predicts a default issues_url" do
            subject.to_hash['issues_url'].should == 'https://github.com/billgates/amazingness/issues'
          end
        end
      end

      context "which is not a GitHub URL" do
        before { metadata.update('source' => 'https://notgithub.com/billgates/amazingness') }

        it "does not predict a default project_page" do
          subject.to_hash['project_page'].should be nil
        end

        it "does not predict a default issues_url" do
          subject.to_hash['issues_url'].should be nil
        end
      end

      context "which is not a URL" do
        before { metadata.update('source' => 'my brain') }

        it "does not predict a default project_page" do
          subject.to_hash['project_page'].should be nil
        end

        it "does not predict a default issues_url" do
          subject.to_hash['issues_url'].should be nil
        end
      end

    end

    context "with a valid dependency" do
      let(:data) { {'dependencies' => [{'name' => 'puppetlabs-goodmodule'}] }}

      it "adds the dependency" do
        subject.dependencies.size.should == 1
      end
    end

    context "with a invalid dependency name" do
      let(:data) { {'dependencies' => [{'name' => 'puppetlabsbadmodule'}] }}

      it "raises an exception" do
        expect { subject }.to raise_error(ArgumentError)
      end
    end

    context "with a valid dependency version range" do
      let(:data) { {'dependencies' => [{'name' => 'puppetlabs-badmodule', 'version_requirement' => '>= 2.0.0'}] }}

      it "adds the dependency" do
        subject.dependencies.size.should == 1
      end
    end

    context "with a invalid version range" do
      let(:data) { {'dependencies' => [{'name' => 'puppetlabsbadmodule', 'version_requirement' => '>= banana'}] }}

      it "raises an exception" do
        expect { subject }.to raise_error(ArgumentError)
      end
    end

    context "with duplicate dependencies" do
      let(:data) { {'dependencies' => [{'name' => 'puppetlabs-dupmodule', 'version_requirement' => '1.0.0'},
        {'name' => 'puppetlabs-dupmodule', 'version_requirement' => '0.0.1'}] }
      }

      it "raises an exception" do
        expect { subject }.to raise_error(ArgumentError)
      end
    end

    context "adding a duplicate dependency" do
      let(:data) { {'dependencies' => [{'name' => 'puppetlabs-origmodule', 'version_requirement' => '1.0.0'}] }}

      it "with a different version raises an exception" do
        metadata.add_dependency('puppetlabs-origmodule', '>= 0.0.1')
        expect { subject }.to raise_error(ArgumentError)
      end

      it "with the same version does not add another dependency" do
        metadata.add_dependency('puppetlabs-origmodule', '1.0.0')
        subject.dependencies.size.should == 1
      end
    end
  end

  describe '#dashed_name' do
    it 'returns nil in the absence of a module name' do
      expect(metadata.update('version' => '1.0.0').release_name).to be_nil
    end

    it 'returns a hyphenated string containing namespace and module name' do
      data = metadata.update('name' => 'foo-bar')
      data.dashed_name.should == 'foo-bar'
    end

    it 'properly handles slash-separated names' do
      data = metadata.update('name' => 'foo/bar')
      data.dashed_name.should == 'foo-bar'
    end

    it 'is unaffected by author name' do
      data = metadata.update('name' => 'foo/bar', 'author' => 'me')
      data.dashed_name.should == 'foo-bar'
    end
  end

  describe '#release_name' do
    it 'returns nil in the absence of a module name' do
      expect(metadata.update('version' => '1.0.0').release_name).to be_nil
    end

    it 'returns nil in the absence of a version' do
      expect(metadata.update('name' => 'foo/bar').release_name).to be_nil
    end

    it 'returns a hyphenated string containing module name and version' do
      data = metadata.update('name' => 'foo/bar', 'version' => '1.0.0')
      data.release_name.should == 'foo-bar-1.0.0'
    end

    it 'is unaffected by author name' do
      data = metadata.update('name' => 'foo/bar', 'version' => '1.0.0', 'author' => 'me')
      data.release_name.should == 'foo-bar-1.0.0'
    end
  end

  describe "#to_hash" do
    subject { metadata.to_hash }

    it "contains the default set of keys" do
      subject.keys.sort.should == %w[ name version author summary license source issues_url project_page dependencies ].sort
    end

    describe "['license']" do
      it "defaults to Apache 2" do
        subject['license'].should == "Apache 2.0"
      end
    end

    describe "['dependencies']" do
      it "defaults to an empty set" do
        subject['dependencies'].should == Set.new
      end
    end

    context "when updated with non-default data" do
      subject { metadata.update('license' => 'MIT', 'non-standard' => 'yup').to_hash }

      it "overrides the defaults" do
        subject['license'].should == 'MIT'
      end

      it 'contains unanticipated values' do
        subject['non-standard'].should == 'yup'
      end
    end
  end
end