summaryrefslogtreecommitdiff
path: root/spec/integration/recipes/recipe_dsl_spec.rb
blob: 3f4bf9fd5f4bcc0ea643244eaec097b0b6e0ae30 (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
require 'support/shared/integration/integration_helper'

describe "Recipe DSL methods" do
  include IntegrationSupport

  module Namer
    extend self
    attr_accessor :current_index
  end

  before(:all) { Namer.current_index = 1 }
  before { Namer.current_index += 1 }

  context "With resource 'base_thingy' declared as BaseThingy" do
    before(:context) {

      class BaseThingy < Chef::Resource
        def initialize(*args, &block)
          super
          @allowed_actions = [ :create ]
          @action = :create
        end

        resource_name 'base_thingy'

        class<<self
          attr_accessor :created_resource
          attr_accessor :created_provider
        end

        def provider
          Provider
        end
        class Provider < Chef::Provider
          def load_current_resource
          end
          def action_create
            BaseThingy.created_resource = new_resource.class
            BaseThingy.created_provider = self.class
          end
        end
      end

      # Modules to put stuff in
      module RecipeDSLSpecNamespace; end
      module RecipeDSLSpecNamespace::Bar; end

    }

    before :each do
      BaseThingy.created_resource = nil
      BaseThingy.created_provider = nil
    end

    context "Deprecated automatic resource DSL" do
      before do
        Chef::Config[:treat_deprecation_warnings_as_errors] = false
      end

      context "With a resource 'backcompat_thingy' declared in Chef::Resource and Chef::Provider" do
        before(:context) {

          class Chef::Resource::BackcompatThingy < Chef::Resource
            def initialize(*args, &block)
              super
              @allowed_actions = [ :create ]
              @action = :create
            end
          end
          class Chef::Provider::BackcompatThingy < Chef::Provider
            def load_current_resource
            end
            def action_create
              BaseThingy.created_resource = new_resource.class
              BaseThingy.created_provider = self.class
            end
          end

        }

        it "backcompat_thingy creates a Chef::Resource::BackcompatThingy" do
          recipe = converge {
            backcompat_thingy 'blah' do; end
          }
          expect(BaseThingy.created_resource).to eq Chef::Resource::BackcompatThingy
          expect(BaseThingy.created_provider).to eq Chef::Provider::BackcompatThingy
        end

        context "and another resource 'backcompat_thingy' in BackcompatThingy with 'provides'" do
          before(:context) {

            class RecipeDSLSpecNamespace::BackcompatThingy < BaseThingy
              provides :backcompat_thingy
              resource_name :backcompat_thingy
            end

          }

          it "backcompat_thingy creates a BackcompatThingy" do
            recipe = converge {
              backcompat_thingy 'blah' do; end
            }
            expect(recipe.logged_warnings).to eq ''
            expect(BaseThingy.created_resource).not_to be_nil
          end
        end
      end

      context "With a resource named RecipeDSLSpecNamespace::Bar::BarThingy" do
        before(:context) {

          class RecipeDSLSpecNamespace::Bar::BarThingy < BaseThingy
          end

        }

        it "bar_thingy works" do
          recipe = converge {
            bar_thingy 'blah' do; end
          }
          expect(recipe.logged_warnings).to eq ''
          expect(BaseThingy.created_resource).to eq(RecipeDSLSpecNamespace::Bar::BarThingy)
        end
      end

      context "With a resource named NoNameThingy with resource_name nil" do
        before(:context) {

          class NoNameThingy < BaseThingy
            resource_name nil
          end

        }

        it "no_name_thingy does not work" do
          expect_converge {
            thingy 'blah' do; end
          }.to raise_error(NoMethodError)
        end
      end

      context "With a resource named AnotherNoNameThingy with resource_name :another_thingy_name" do
        before(:context) {

          class AnotherNoNameThingy < BaseThingy
            resource_name :another_thingy_name
          end

        }

        it "another_no_name_thingy does not work" do
          expect_converge {
            another_no_name_thingy 'blah' do; end
          }.to raise_error(NoMethodError)
        end

        it "another_thingy_name works" do
          recipe = converge {
            another_thingy_name 'blah' do; end
          }
          expect(recipe.logged_warnings).to eq ''
          expect(BaseThingy.created_resource).to eq(AnotherNoNameThingy)
        end
      end

      context "With a resource named AnotherNoNameThingy2 with resource_name :another_thingy_name2; resource_name :another_thingy_name3" do
        before(:context) {

          class AnotherNoNameThingy2 < BaseThingy
            resource_name :another_thingy_name2
            resource_name :another_thingy_name3
          end

        }

        it "another_no_name_thingy does not work" do
          expect_converge {
            another_no_name_thingy2 'blah' do; end
          }.to raise_error(NoMethodError)
        end

        it "another_thingy_name2 does not work" do
          expect_converge {
            another_thingy_name2 'blah' do; end
          }.to raise_error(NoMethodError)
        end

        it "yet_another_thingy_name3 works" do
          recipe = converge {
            another_thingy_name3 'blah' do; end
          }
          expect(recipe.logged_warnings).to eq ''
          expect(BaseThingy.created_resource).to eq(AnotherNoNameThingy2)
        end
      end

      context "provides overriding resource_name" do
        context "With a resource named AnotherNoNameThingy3 with provides :another_no_name_thingy3, os: 'blarghle'" do
          before(:context) {

            class AnotherNoNameThingy3 < BaseThingy
              provides :another_no_name_thingy3, os: 'blarghle'
            end

          }

          it "and os = linux, another_no_name_thingy3 does not work" do
            expect_converge {
              # TODO this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'linux'
              another_no_name_thingy3 'blah' do; end
            }.to raise_error(Chef::Exceptions::NoSuchResourceType)
          end

          it "and os = blarghle, another_no_name_thingy3 works" do
            recipe = converge {
              # TODO this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'blarghle'
              another_no_name_thingy3 'blah' do; end
            }
            expect(recipe.logged_warnings).to eq ''
            expect(BaseThingy.created_resource).to eq (AnotherNoNameThingy3)
          end
        end

        context "With a resource named AnotherNoNameThingy4 with two provides" do
          before(:context) {

            class AnotherNoNameThingy4 < BaseThingy
              provides :another_no_name_thingy4, os: 'blarghle'
              provides :another_no_name_thingy4, platform_family: 'foo'
            end

          }

          it "and os = linux, another_no_name_thingy4 does not work" do
            expect_converge {
              # TODO this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'linux'
              another_no_name_thingy4 'blah' do; end
            }.to raise_error(Chef::Exceptions::NoSuchResourceType)
          end

          it "and os = blarghle, another_no_name_thingy4 works" do
            recipe = converge {
              # TODO this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'blarghle'
              another_no_name_thingy4 'blah' do; end
            }
            expect(recipe.logged_warnings).to eq ''
            expect(BaseThingy.created_resource).to eq (AnotherNoNameThingy4)
          end

          it "and platform_family = foo, another_no_name_thingy4 works" do
            recipe = converge {
              # TODO this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:platform_family] = 'foo'
              another_no_name_thingy4 'blah' do; end
            }
            expect(recipe.logged_warnings).to eq ''
            expect(BaseThingy.created_resource).to eq (AnotherNoNameThingy4)
          end
        end

        context "With a resource named AnotherNoNameThingy5, a different resource_name, and a provides with the original resource_name" do
          before(:context) {

            class AnotherNoNameThingy5 < BaseThingy
              resource_name :another_thingy_name_for_another_no_name_thingy5
              provides :another_no_name_thingy5, os: 'blarghle'
            end

          }

          it "and os = linux, another_no_name_thingy5 does not work" do
            expect_converge {
              # this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'linux'
              another_no_name_thingy5 'blah' do; end
            }.to raise_error(Chef::Exceptions::NoSuchResourceType)
          end

          it "and os = blarghle, another_no_name_thingy5 works" do
            recipe = converge {
              # this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'blarghle'
              another_no_name_thingy5 'blah' do; end
            }
            expect(recipe.logged_warnings).to eq ''
            expect(BaseThingy.created_resource).to eq (AnotherNoNameThingy5)
          end

          it "the new resource name can be used in a recipe" do
            recipe = converge {
              another_thingy_name_for_another_no_name_thingy5 'blah' do; end
            }
            expect(recipe.logged_warnings).to eq ''
            expect(BaseThingy.created_resource).to eq (AnotherNoNameThingy5)
          end
        end

        context "With a resource named AnotherNoNameThingy6, a provides with the original resource name, and a different resource_name" do
          before(:context) {

            class AnotherNoNameThingy6 < BaseThingy
              provides :another_no_name_thingy6, os: 'blarghle'
              resource_name :another_thingy_name_for_another_no_name_thingy6
            end

          }

          it "and os = linux, another_no_name_thingy6 does not work" do
            expect_converge {
              # this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'linux'
              another_no_name_thingy6 'blah' do; end
            }.to raise_error(Chef::Exceptions::NoSuchResourceType)
          end

          it "and os = blarghle, another_no_name_thingy6 works" do
            recipe = converge {
              # this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'blarghle'
              another_no_name_thingy6 'blah' do; end
            }
            expect(recipe.logged_warnings).to eq ''
            expect(BaseThingy.created_resource).to eq (AnotherNoNameThingy6)
          end

          it "the new resource name can be used in a recipe" do
            recipe = converge {
              another_thingy_name_for_another_no_name_thingy6 'blah' do; end
            }
            expect(recipe.logged_warnings).to eq ''
            expect(BaseThingy.created_resource).to eq (AnotherNoNameThingy6)
          end
        end

        context "With a resource named AnotherNoNameThingy7, a new resource_name, and provides with that new resource name" do
          before(:context) {

            class AnotherNoNameThingy7 < BaseThingy
              resource_name :another_thingy_name_for_another_no_name_thingy7
              provides :another_thingy_name_for_another_no_name_thingy7, os: 'blarghle'
            end

          }

          it "and os = linux, another_thingy_name_for_another_no_name_thingy7 does not work" do
            expect_converge {
              # this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'linux'
              another_thingy_name_for_another_no_name_thingy7 'blah' do; end
            }.to raise_error(Chef::Exceptions::NoSuchResourceType)
          end

          it "and os = blarghle, another_thingy_name_for_another_no_name_thingy7 works" do
            recipe = converge {
              # this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'blarghle'
              another_thingy_name_for_another_no_name_thingy7 'blah' do; end
            }
            expect(recipe.logged_warnings).to eq ''
            expect(BaseThingy.created_resource).to eq (AnotherNoNameThingy7)
          end

          it "the old resource name does not work" do
            expect_converge {
              # this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'linux'
              another_no_name_thingy_7 'blah' do; end
            }.to raise_error(NoMethodError)
          end
        end

        # opposite order from the previous test (provides, then resource_name)
        context "With a resource named AnotherNoNameThingy8, a provides with a new resource name, and resource_name with that new resource name" do
          before(:context) {

            class AnotherNoNameThingy8 < BaseThingy
              provides :another_thingy_name_for_another_no_name_thingy8, os: 'blarghle'
              resource_name :another_thingy_name_for_another_no_name_thingy8
            end

          }

          it "and os = linux, another_thingy_name_for_another_no_name_thingy8 does not work" do
            expect_converge {
              # this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'linux'
              another_thingy_name_for_another_no_name_thingy8 'blah' do; end
            }.to raise_error(Chef::Exceptions::NoSuchResourceType)
          end

          it "and os = blarghle, another_thingy_name_for_another_no_name_thingy8 works" do
            recipe = converge {
              # this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'blarghle'
              another_thingy_name_for_another_no_name_thingy8 'blah' do; end
            }
            expect(recipe.logged_warnings).to eq ''
            expect(BaseThingy.created_resource).to eq (AnotherNoNameThingy8)
          end

          it "the old resource name does not work" do
            expect_converge {
              # this is an ugly way to test, make Cheffish expose node attrs
              run_context.node.automatic[:os] = 'linux'
              another_thingy_name8 'blah' do; end
            }.to raise_error(NoMethodError)
          end
        end

        context "With a resource TwoClassesOneDsl" do
          let(:class_name) { "TwoClassesOneDsl#{Namer.current_index}" }
          let(:dsl_method) { :"two_classes_one_dsl#{Namer.current_index}" }

          before {
            eval <<-EOM, nil, __FILE__, __LINE__+1
              class #{class_name} < BaseThingy
              end
            EOM
          }
          context "and resource BlahModule::TwoClassesOneDsl" do
            before {
              eval <<-EOM, nil, __FILE__, __LINE__+1
                module BlahModule
                  class #{class_name} < BaseThingy
                  end
                end
              EOM
            }
            it "two_classes_one_dsl resolves to BlahModule::TwoClassesOneDsl (last declared)" do
              dsl_method = self.dsl_method
              recipe = converge {
                instance_eval("#{dsl_method} 'blah' do; end")
              }
              expect(recipe.logged_warnings).to eq ''
              expect(BaseThingy.created_resource).to eq eval("BlahModule::#{class_name}")
            end
            it "resource_matching_short_name returns BlahModule::TwoClassesOneDsl" do
              expect(Chef::Resource.resource_matching_short_name(dsl_method)).to eq eval("BlahModule::#{class_name}")
            end
          end
          context "and resource BlahModule::TwoClassesOneDsl with resource_name nil" do
            before {
              eval <<-EOM, nil, __FILE__, __LINE__+1
                module BlahModule
                  class BlahModule::#{class_name} < BaseThingy
                    resource_name nil
                  end
                end
              EOM
            }
            it "two_classes_one_dsl resolves to ::TwoClassesOneDsl" do
              dsl_method = self.dsl_method
              recipe = converge {
                instance_eval("#{dsl_method} 'blah' do; end")
              }
              expect(recipe.logged_warnings).to eq ''
              expect(BaseThingy.created_resource).to eq eval("::#{class_name}")
            end
            it "resource_matching_short_name returns ::TwoClassesOneDsl" do
              expect(Chef::Resource.resource_matching_short_name(dsl_method)).to eq eval("::#{class_name}")
            end
          end
          context "and resource BlahModule::TwoClassesOneDsl with resource_name :argh" do
            before {
              eval <<-EOM, nil, __FILE__, __LINE__+1
                module BlahModule
                  class BlahModule::#{class_name} < BaseThingy
                    resource_name :argh
                  end
                end
              EOM
            }
            it "two_classes_one_dsl resolves to ::TwoClassesOneDsl" do
              dsl_method = self.dsl_method
              recipe = converge {
                instance_eval("#{dsl_method} 'blah' do; end")
              }
              expect(recipe.logged_warnings).to eq ''
              expect(BaseThingy.created_resource).to eq eval("::#{class_name}")
            end
            it "resource_matching_short_name returns ::TwoClassesOneDsl" do
              expect(Chef::Resource.resource_matching_short_name(dsl_method)).to eq eval("::#{class_name}")
            end
          end
          context "and resource BlahModule::TwoClassesOneDsl with provides :two_classes_one_dsl, os: 'blarghle'" do
            before {
              eval <<-EOM, nil, __FILE__, __LINE__+1
                module BlahModule
                  class BlahModule::#{class_name} < BaseThingy
                    provides #{dsl_method.inspect}, os: 'blarghle'
                  end
                end
              EOM
            }

            it "and os = blarghle, two_classes_one_dsl resolves to BlahModule::TwoClassesOneDsl" do
              dsl_method = self.dsl_method
              recipe = converge {
                # this is an ugly way to test, make Cheffish expose node attrs
                run_context.node.automatic[:os] = 'blarghle'
                instance_eval("#{dsl_method} 'blah' do; end")
              }
              expect(recipe.logged_warnings).to eq ''
              expect(BaseThingy.created_resource).to eq eval("BlahModule::#{class_name}")
            end

            it "and os = linux, two_classes_one_dsl resolves to ::TwoClassesOneDsl" do
              dsl_method = self.dsl_method
              recipe = converge {
                # this is an ugly way to test, make Cheffish expose node attrs
                run_context.node.automatic[:os] = 'linux'
                instance_eval("#{dsl_method} 'blah' do; end")
              }
              expect(recipe.logged_warnings).to eq ''
              expect(BaseThingy.created_resource).to eq eval("::#{class_name}")
            end
          end
        end
      end
    end

    context "provides" do
      context "when MySupplier provides :hemlock" do
        before(:context) {

          class RecipeDSLSpecNamespace::MySupplier < BaseThingy
            resource_name :hemlock
          end

        }

        it "my_supplier does not work in a recipe" do
          expect_converge {
            my_supplier 'blah' do; end
          }.to raise_error(NoMethodError)
        end

        it "hemlock works in a recipe" do
          expect_recipe {
            hemlock 'blah' do; end
          }.to emit_no_warnings_or_errors
          expect(BaseThingy.created_resource).to eq RecipeDSLSpecNamespace::MySupplier
        end
      end

      context "when Thingy3 has resource_name :thingy3" do
        before(:context) {

          class RecipeDSLSpecNamespace::Thingy3 < BaseThingy
            resource_name :thingy3
          end

        }

        it "thingy3 works in a recipe" do
          expect_recipe {
            thingy3 'blah' do; end
          }.to emit_no_warnings_or_errors
          expect(BaseThingy.created_resource).to eq RecipeDSLSpecNamespace::Thingy3
        end

        context "and Thingy4 has resource_name :thingy3" do
          before(:context) {

            class RecipeDSLSpecNamespace::Thingy4 < BaseThingy
              resource_name :thingy3
            end

          }

          it "thingy3 works in a recipe and yields Foo::Thingy4 (the explicit one)" do
            recipe = converge {
              thingy3 'blah' do; end
            }
            expect(BaseThingy.created_resource).to eq RecipeDSLSpecNamespace::Thingy4
          end

          it "thingy4 does not work in a recipe" do
            expect_converge {
              thingy4 'blah' do; end
            }.to raise_error(NoMethodError)
          end

          it "resource_matching_short_name returns Thingy4" do
            expect(Chef::Resource.resource_matching_short_name(:thingy3)).to eq RecipeDSLSpecNamespace::Thingy4
          end
        end
      end

      context "when Thingy5 has resource_name :thingy5" do
        before(:context) {

          class RecipeDSLSpecNamespace::Thingy5 < BaseThingy
            resource_name :thingy5
          end

        }

        it "thingy5 works in a recipe" do
          expect_recipe {
            thingy5 'blah' do; end
          }.to emit_no_warnings_or_errors
          expect(BaseThingy.created_resource).to eq RecipeDSLSpecNamespace::Thingy5
        end

        context "and Thingy6 provides :thingy5" do
          before(:context) {

            class RecipeDSLSpecNamespace::Thingy6 < BaseThingy
              provides :thingy5
            end

          }

          it "thingy6 works in a recipe and yields Thingy6" do
            recipe = converge {
              thingy6 'blah' do; end
            }
            expect(BaseThingy.created_resource).to eq RecipeDSLSpecNamespace::Thingy6
          end

          it "thingy5 works in a recipe and yields Foo::Thingy6 (the later one)" do
            recipe = converge {
              thingy5 'blah' do; end
            }
            expect(BaseThingy.created_resource).to eq RecipeDSLSpecNamespace::Thingy6
          end

          it "resource_matching_short_name returns Thingy5" do
            expect(Chef::Resource.resource_matching_short_name(:thingy5)).to eq RecipeDSLSpecNamespace::Thingy5
          end
        end
      end

      context "when Thingy7 provides :thingy8" do
        before(:context) {

          class RecipeDSLSpecNamespace::Thingy7 < BaseThingy
            provides :thingy8
          end

        }

        context "and Thingy8 has resource_name :thingy8" do
          before(:context) {

            class RecipeDSLSpecNamespace::Thingy8 < BaseThingy
              resource_name :thingy8
            end

          }

          it "thingy7 works in a recipe and yields Thingy7" do
            recipe = converge {
              thingy7 'blah' do; end
            }
            expect(BaseThingy.created_resource).to eq RecipeDSLSpecNamespace::Thingy7
          end

          it "thingy8 works in a recipe and yields Thingy8 (the later one)" do
            recipe = converge {
              thingy8 'blah' do; end
            }
            expect(BaseThingy.created_resource).to eq RecipeDSLSpecNamespace::Thingy8
          end

          it "resource_matching_short_name returns Thingy8" do
            expect(Chef::Resource.resource_matching_short_name(:thingy8)).to eq RecipeDSLSpecNamespace::Thingy8
          end
        end
      end

      context "when Thingy5 provides :thingy5, :twizzle and :twizzle2" do
        before(:context) {

          class RecipeDSLSpecNamespace::Thingy5 < BaseThingy
            resource_name :thingy5
            provides :twizzle
            provides :twizzle2
          end

        }

        it "thingy5 works in a recipe and yields Thingy5" do
          expect_recipe {
            thingy5 'blah' do; end
          }.to emit_no_warnings_or_errors
          expect(BaseThingy.created_resource).to eq RecipeDSLSpecNamespace::Thingy5
        end

        it "twizzle works in a recipe and yields Thingy5" do
          expect_recipe {
            twizzle 'blah' do; end
          }.to emit_no_warnings_or_errors
          expect(BaseThingy.created_resource).to eq RecipeDSLSpecNamespace::Thingy5
        end

        it "twizzle2 works in a recipe and yields Thingy5" do
          expect_recipe {
            twizzle2 'blah' do; end
          }.to emit_no_warnings_or_errors
          expect(BaseThingy.created_resource).to eq RecipeDSLSpecNamespace::Thingy5
        end
      end

      context "With platform-specific resources 'my_super_thingy_foo' and 'my_super_thingy_bar'" do
        before(:context) {
          class MySuperThingyFoo < BaseThingy
            resource_name :my_super_thingy_foo
            provides :my_super_thingy, platform: 'foo'
          end

          class MySuperThingyBar < BaseThingy
            resource_name :my_super_thingy_bar
            provides :my_super_thingy, platform: 'bar'
          end
        }

        it "A run with platform 'foo' uses MySuperThingyFoo" do
          r = Cheffish::ChefRun.new(chef_config)
          r.client.run_context.node.automatic['platform'] = 'foo'
          r.compile_recipe {
            my_super_thingy 'blah' do; end
          }
          r.converge
          expect(r).to emit_no_warnings_or_errors
          expect(BaseThingy.created_resource).to eq MySuperThingyFoo
        end

        it "A run with platform 'bar' uses MySuperThingyBar" do
          r = Cheffish::ChefRun.new(chef_config)
          r.client.run_context.node.automatic['platform'] = 'bar'
          r.compile_recipe {
            my_super_thingy 'blah' do; end
          }
          r.converge
          expect(r).to emit_no_warnings_or_errors
          expect(BaseThingy.created_resource).to eq MySuperThingyBar
        end

        it "A run with platform 'x' reports that my_super_thingy is not supported" do
          r = Cheffish::ChefRun.new(chef_config)
          r.client.run_context.node.automatic['platform'] = 'x'
          expect {
            r.compile_recipe {
              my_super_thingy 'blah' do; end
            }
          }.to raise_error(Chef::Exceptions::NoSuchResourceType)
        end
      end
    end
  end
end