Merge pull request #4 from stephenrjohnson/miscfixes
Updated Section/ Setting regex, fixed bug with empty files and updated provider to not care about variable type
This commit is contained in:
commit
1740608847
3 changed files with 67 additions and 8 deletions
|
@ -2,7 +2,7 @@ require File.expand_path('../../../util/ini_file', __FILE__)
|
||||||
|
|
||||||
Puppet::Type.type(:ini_setting).provide(:ruby) do
|
Puppet::Type.type(:ini_setting).provide(:ruby) do
|
||||||
def exists?
|
def exists?
|
||||||
ini_file.get_value(resource[:section], resource[:setting]) == resource[:value]
|
ini_file.get_value(resource[:section], resource[:setting]) == resource[:value].to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|
|
@ -5,14 +5,13 @@ module Puppet
|
||||||
module Util
|
module Util
|
||||||
class IniFile
|
class IniFile
|
||||||
|
|
||||||
SECTION_REGEX = /^\s*\[([\w\d\.]+)\]\s*$/
|
SECTION_REGEX = /^\s*\[([\w\d\.\\\/\-]+)\]\s*$/
|
||||||
SETTING_REGEX = /^\s*([\w\d\.]+)\s*=\s*([\w\d\.]+)\s*$/
|
SETTING_REGEX = /^\s*([\w\d\.\\\/\-]+)\s*=\s*([\S]+)\s*$/
|
||||||
|
|
||||||
def initialize(path)
|
def initialize(path)
|
||||||
@path = path
|
@path = path
|
||||||
@section_names = []
|
@section_names = []
|
||||||
@sections_hash = {}
|
@sections_hash = {}
|
||||||
|
|
||||||
parse_file
|
parse_file
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -43,7 +42,8 @@ module Util
|
||||||
def save
|
def save
|
||||||
File.open(@path, 'w') do |fh|
|
File.open(@path, 'w') do |fh|
|
||||||
first_section = @sections_hash[@section_names[0]]
|
first_section = @sections_hash[@section_names[0]]
|
||||||
(0..first_section.start_line - 1).each do |line_num|
|
first_section.start_line == nil ? start_line = 0 : start_line = first_section.start_line
|
||||||
|
(0..start_line - 1).each do |line_num|
|
||||||
fh.puts(lines[line_num])
|
fh.puts(lines[line_num])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -93,7 +93,6 @@ module Util
|
||||||
elsif (match = SETTING_REGEX.match(line))
|
elsif (match = SETTING_REGEX.match(line))
|
||||||
settings[match[1]] = match[2]
|
settings[match[1]] = match[2]
|
||||||
end
|
end
|
||||||
|
|
||||||
line_iter.next
|
line_iter.next
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,6 +6,7 @@ describe provider_class do
|
||||||
include PuppetlabsSpec::Files
|
include PuppetlabsSpec::Files
|
||||||
|
|
||||||
let(:tmpfile) { tmpfilename("ini_setting_test") }
|
let(:tmpfile) { tmpfilename("ini_setting_test") }
|
||||||
|
let(:emptyfile) { tmpfilename("ini_setting_test_empty") }
|
||||||
let(:orig_content) {
|
let(:orig_content) {
|
||||||
<<-EOS
|
<<-EOS
|
||||||
# This is a comment
|
# This is a comment
|
||||||
|
@ -14,16 +15,18 @@ describe provider_class do
|
||||||
foo=foovalue
|
foo=foovalue
|
||||||
|
|
||||||
bar = barvalue
|
bar = barvalue
|
||||||
|
master = true
|
||||||
[section2]
|
[section2]
|
||||||
|
|
||||||
foo= foovalue2
|
foo= foovalue2
|
||||||
baz=bazvalue
|
baz=bazvalue
|
||||||
|
url = http://192.168.1.1:8080
|
||||||
#another comment
|
#another comment
|
||||||
; yet another comment
|
; yet another comment
|
||||||
EOS
|
EOS
|
||||||
}
|
}
|
||||||
|
|
||||||
def validate_file(expected_content)
|
def validate_file(expected_content,tmpfile = tmpfile)
|
||||||
File.read(tmpfile).should == expected_content
|
File.read(tmpfile).should == expected_content
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -32,6 +35,9 @@ baz=bazvalue
|
||||||
File.open(tmpfile, 'w') do |fh|
|
File.open(tmpfile, 'w') do |fh|
|
||||||
fh.write(orig_content)
|
fh.write(orig_content)
|
||||||
end
|
end
|
||||||
|
File.open(emptyfile, 'w') do |fh|
|
||||||
|
fh.write("")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when ensuring that a setting is present" do
|
context "when ensuring that a setting is present" do
|
||||||
|
@ -54,10 +60,12 @@ baz=bazvalue
|
||||||
foo=foovalue
|
foo=foovalue
|
||||||
|
|
||||||
bar = barvalue
|
bar = barvalue
|
||||||
|
master = true
|
||||||
[section2]
|
[section2]
|
||||||
|
|
||||||
foo= foovalue2
|
foo= foovalue2
|
||||||
baz=bazvalue
|
baz=bazvalue
|
||||||
|
url = http://192.168.1.1:8080
|
||||||
#another comment
|
#another comment
|
||||||
; yet another comment
|
; yet another comment
|
||||||
yahoo = yippee
|
yahoo = yippee
|
||||||
|
@ -78,10 +86,38 @@ yahoo = yippee
|
||||||
foo=foovalue
|
foo=foovalue
|
||||||
|
|
||||||
bar = barvalue
|
bar = barvalue
|
||||||
|
master = true
|
||||||
[section2]
|
[section2]
|
||||||
|
|
||||||
foo= foovalue2
|
foo= foovalue2
|
||||||
baz = bazvalue2
|
baz = bazvalue2
|
||||||
|
url = http://192.168.1.1:8080
|
||||||
|
#another comment
|
||||||
|
; yet another comment
|
||||||
|
EOS
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be able to handle settings with non alphanumbering settings " do
|
||||||
|
resource = Puppet::Type::Ini_setting.new(common_params.merge(
|
||||||
|
:setting => 'url', :value => 'http://192.168.0.1:8080'))
|
||||||
|
provider = described_class.new(resource)
|
||||||
|
provider.exists?.should == false
|
||||||
|
provider.create
|
||||||
|
|
||||||
|
validate_file( <<-EOS
|
||||||
|
# This is a comment
|
||||||
|
[section1]
|
||||||
|
; This is also a comment
|
||||||
|
foo=foovalue
|
||||||
|
|
||||||
|
bar = barvalue
|
||||||
|
master = true
|
||||||
|
[section2]
|
||||||
|
|
||||||
|
foo= foovalue2
|
||||||
|
baz=bazvalue
|
||||||
|
url = http://192.168.0.1:8080
|
||||||
#another comment
|
#another comment
|
||||||
; yet another comment
|
; yet another comment
|
||||||
EOS
|
EOS
|
||||||
|
@ -108,10 +144,12 @@ baz = bazvalue2
|
||||||
foo=foovalue
|
foo=foovalue
|
||||||
|
|
||||||
bar = barvalue
|
bar = barvalue
|
||||||
|
master = true
|
||||||
[section2]
|
[section2]
|
||||||
|
|
||||||
foo= foovalue2
|
foo= foovalue2
|
||||||
baz=bazvalue
|
baz=bazvalue
|
||||||
|
url = http://192.168.1.1:8080
|
||||||
#another comment
|
#another comment
|
||||||
; yet another comment
|
; yet another comment
|
||||||
|
|
||||||
|
@ -120,5 +158,27 @@ huzzah = shazaam
|
||||||
EOS
|
EOS
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should add a new section if no sections exists" do
|
||||||
|
resource = Puppet::Type::Ini_setting.new(common_params.merge(
|
||||||
|
:section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
|
||||||
|
provider = described_class.new(resource)
|
||||||
|
provider.exists?.should == false
|
||||||
|
provider.create
|
||||||
|
validate_file("
|
||||||
|
[section1]
|
||||||
|
setting1 = hellowworld
|
||||||
|
", emptyfile)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be able to handle variables of any type" do
|
||||||
|
resource = Puppet::Type::Ini_setting.new(common_params.merge(
|
||||||
|
:section => "section1", :setting => 'master', :value => true))
|
||||||
|
provider = described_class.new(resource)
|
||||||
|
provider.exists?.should == true
|
||||||
|
provider.create
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue