ini_file_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. require 'spec_helper'
  2. require 'puppet/util/ini_file'
  3. describe Puppet::Util::IniFile do
  4. let(:subject) { Puppet::Util::IniFile.new("/my/ini/file/path") }
  5. before :each do
  6. File.should_receive(:file?).with("/my/ini/file/path") { true }
  7. described_class.should_receive(:readlines).once.with("/my/ini/file/path") do
  8. sample_content
  9. end
  10. end
  11. context "when parsing a file" do
  12. let(:sample_content) {
  13. template = <<-EOS
  14. # This is a comment
  15. [section1]
  16. ; This is also a comment
  17. foo=foovalue
  18. bar = barvalue
  19. [section2]
  20. foo= foovalue2
  21. baz=bazvalue
  22. #another comment
  23. ; yet another comment
  24. zot = multi word value
  25. EOS
  26. template.split("\n")
  27. }
  28. it "should parse the correct number of sections" do
  29. # there is always a "global" section, so our count should be 3.
  30. subject.section_names.length.should == 3
  31. end
  32. it "should parse the correct section_names" do
  33. # there should always be a "global" section named "" at the beginning of the list
  34. subject.section_names.should == ["", "section1", "section2"]
  35. end
  36. it "should expose settings for sections" do
  37. subject.get_value("section1", "foo").should == "foovalue"
  38. subject.get_value("section1", "bar").should == "barvalue"
  39. subject.get_value("section2", "foo").should == "foovalue2"
  40. subject.get_value("section2", "baz").should == "bazvalue"
  41. subject.get_value("section2", "zot").should == "multi word value"
  42. end
  43. end
  44. context "when parsing a file whose first line is a section" do
  45. let(:sample_content) {
  46. template = <<-EOS
  47. [section1]
  48. ; This is a comment
  49. foo=foovalue
  50. EOS
  51. template.split("\n")
  52. }
  53. it "should parse the correct number of sections" do
  54. # there is always a "global" section, so our count should be 2.
  55. subject.section_names.length.should == 2
  56. end
  57. it "should parse the correct section_names" do
  58. # there should always be a "global" section named "" at the beginning of the list
  59. subject.section_names.should == ["", "section1"]
  60. end
  61. it "should expose settings for sections" do
  62. subject.get_value("section1", "foo").should == "foovalue"
  63. end
  64. end
  65. context "when parsing a file with a 'global' section" do
  66. let(:sample_content) {
  67. template = <<-EOS
  68. foo = bar
  69. [section1]
  70. ; This is a comment
  71. foo=foovalue
  72. EOS
  73. template.split("\n")
  74. }
  75. it "should parse the correct number of sections" do
  76. # there is always a "global" section, so our count should be 2.
  77. subject.section_names.length.should == 2
  78. end
  79. it "should parse the correct section_names" do
  80. # there should always be a "global" section named "" at the beginning of the list
  81. subject.section_names.should == ["", "section1"]
  82. end
  83. it "should expose settings for sections" do
  84. subject.get_value("", "foo").should == "bar"
  85. subject.get_value("section1", "foo").should == "foovalue"
  86. end
  87. end
  88. end