]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/inifile/spec/unit/puppet/provider/ini_setting/inheritance_spec.rb
add puppetlabs/inifile to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / inifile / spec / unit / puppet / provider / ini_setting / inheritance_spec.rb
1 require 'spec_helper'
2
3 # This is a reduced version of ruby_spec.rb just to ensure we can subclass as
4 # documented
5 $: << 'spec/fixtures/modules/inherit_ini_setting/lib'
6 provider_class = Puppet::Type.type(:inherit_ini_setting).provider(:ini_setting)
7 describe provider_class do
8   include PuppetlabsSpec::Files
9
10   let(:tmpfile) { tmpfilename('inherit_ini_setting_test') }
11
12   def validate_file(expected_content,tmpfile = tmpfile)
13     File.read(tmpfile).should == expected_content
14   end
15
16
17   before :each do
18     File.open(tmpfile, 'w') do |fh|
19       fh.write(orig_content)
20     end
21   end
22
23   context 'when calling instances' do
24     let(:orig_content) { '' }
25
26     it 'should parse nothing when the file is empty' do
27       provider_class.stubs(:file_path).returns(tmpfile)
28       provider_class.instances.should == []
29     end
30
31     context 'when the file has contents' do
32       let(:orig_content) {
33         <<-EOS
34 # A comment
35 red = blue
36 green = purple
37         EOS
38       }
39
40       it 'should parse the results' do
41         provider_class.stubs(:file_path).returns(tmpfile)
42         instances = provider_class.instances
43         instances.size.should == 2
44         # inherited version of namevar flattens the names
45         names = instances.map do |instance|
46           instance.instance_variable_get(:@property_hash)[:name]
47         end
48         names.sort.should == [ 'green', 'red' ]
49       end
50     end
51   end
52
53   context 'when ensuring that a setting is present' do
54     let(:orig_content) { '' }
55
56     it 'should add a value to the file' do
57       provider_class.stubs(:file_path).returns(tmpfile)
58       resource = Puppet::Type::Inherit_ini_setting.new({
59         :setting => 'set_this',
60         :value   => 'to_that',
61       })
62       provider = described_class.new(resource)
63       provider.create
64       validate_file("set_this=to_that\n")
65     end
66   end
67 end