inheritance_spec.rb 1.8 KB

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