diff options
author | lutter <lutter@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-01-04 02:21:27 +0000 |
---|---|---|
committer | lutter <lutter@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-01-04 02:21:27 +0000 |
commit | 5292e4eb4dd57c0db3f2ab216216f9e21301f53b (patch) | |
tree | ce3070fdedd6796b465507f51ead04d88032fa27 | |
parent | 2366c95a00570c074ebfbeb5f0150dfc994b7b23 (diff) | |
download | puppet-5292e4eb4dd57c0db3f2ab216216f9e21301f53b.tar.gz puppet-5292e4eb4dd57c0db3f2ab216216f9e21301f53b.tar.xz puppet-5292e4eb4dd57c0db3f2ab216216f9e21301f53b.zip |
Handle continuation lines in inifiles properly; stick a little closer to how python's ConfigParser parses
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2032 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r-- | lib/puppet/inifile.rb | 26 | ||||
-rw-r--r-- | test/data/types/yumrepos/fedora.repo | 3 | ||||
-rwxr-xr-x | test/other/inifile.rb | 15 | ||||
-rwxr-xr-x | test/types/yumrepo.rb | 1 |
4 files changed, 36 insertions, 9 deletions
diff --git a/lib/puppet/inifile.rb b/lib/puppet/inifile.rb index 9201bf3f4..3ffd6d137 100644 --- a/lib/puppet/inifile.rb +++ b/lib/puppet/inifile.rb @@ -5,6 +5,8 @@ # something has changed are written back to disk # Great care is taken to preserve comments and blank lines from the original # files +# +# The parsing tries to stay close to python's ConfigParser require 'puppet/filetype' @@ -106,20 +108,29 @@ module Puppet raise "Could not find #{file}" end - section = nil + section = nil # The name of the current section + optname = nil # The name of the last option in section line = 0 @files[file] = [] text.each_line do |l| line += 1 - if l =~ /^\[(.+)\]$/ - section.mark_clean unless section.nil? - section = add_section($1, file) - elsif l =~ /^(\s*\#|\s*$)/ + if l.strip.empty? || "#;".include?(l[0,1]) || + (l.split(nil, 2)[0].downcase == "rem" && + l[0,1].downcase == "r") + # Whitespace or comment if section.nil? @files[file] << l else section.add_line(l) end + elsif " \t\r\n\f".include?(l[0,1]) && section && optname + # continuation line + section[optname] += "\n" + l.chomp + elsif l =~ /^\[([^\]]+)\]/ + # section heading + section.mark_clean unless section.nil? + section = add_section($1, file) + optname = nil elsif l =~ /^\s*([^\s=]+)\s*\=(.+)$/ # We allow space around the keys, but not the values # For the values, we don't know if space is significant @@ -127,12 +138,9 @@ module Puppet raise "#{file}:#{line}:Key/value pair outside of a section for key #{$1}" else section[$1] = $2 + optname = $1 end else - # FIXME: We can't deal with continuation lines - # that at least yum allows (lines that start with - # whitespace, and that should really be appended - # to the value of the previous key) raise "#{file}:#{line}: Can't parse '#{l.chomp}'" end end diff --git a/test/data/types/yumrepos/fedora.repo b/test/data/types/yumrepos/fedora.repo index ec3a1b3a8..995fa68ca 100644 --- a/test/data/types/yumrepos/fedora.repo +++ b/test/data/types/yumrepos/fedora.repo @@ -4,3 +4,6 @@ mirrorlist=http://fedora.redhat.com/download/mirrors/fedora-core-$releasever enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora +exclude=foo + bar + baz diff --git a/test/other/inifile.rb b/test/other/inifile.rb index 1bb20282b..5b1dfe0ae 100755 --- a/test/other/inifile.rb +++ b/test/other/inifile.rb @@ -80,6 +80,7 @@ class TestFileType < Test::Unit::TestCase end def test_whitespace + # FIXME: Should we really accept keys preceded by whitespace ? fname = mkfile("[main]\n key1=v1\nkey2 =v2\n") assert_nothing_raised { @file.read(fname) @@ -87,6 +88,20 @@ class TestFileType < Test::Unit::TestCase s = get_section('main') assert_equal('v1', s['key1']) assert_equal('v2', s['key2']) + # FIXME: We are losing whitespace around keys + assert_equal("[main]\nkey1=v1\nkey2=v2\n", s.format) + end + + def test_continuation + cont = "[main]\nkey1=v1\nkey2=v2a\n v2b\n" + fname = mkfile(cont) + assert_nothing_raised { + @file.read(fname) + } + s = get_section('main') + assert_equal('v1', s['key1']) + assert_equal("v2a\n v2b", s['key2']) + assert_equal(cont, s.format) end def assert_entries(section, hash) diff --git a/test/types/yumrepo.rb b/test/types/yumrepo.rb index 72749a6fa..940fe1738 100755 --- a/test/types/yumrepo.rb +++ b/test/types/yumrepo.rb @@ -37,6 +37,7 @@ class TestYumRepo < Test::Unit::TestCase assert_equal('New description', inifile['development']['name']) assert_equal('Fedora Core $releasever - $basearch - Base', inifile['base']['name']) + assert_equal("foo\n bar\n baz", inifile['base']['exclude']) assert_equal(['base', 'development', 'main'], all_sections(inifile)) end |