Skip to content

Commit

Permalink
Merge 48a0c28 into 9e8696f
Browse files Browse the repository at this point in the history
  • Loading branch information
raphink committed Dec 2, 2014
2 parents 9e8696f + 48a0c28 commit 869db0e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .fixtures.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fixtures:
repositories:
"augeasproviders_core":
repo: "git://github.com/hercules-team/augeasproviders_core.git"
repo: "git://github.com/raphink/augeasproviders_core.git"
branch: "dev/whichquote"
symlinks:
augeasproviders_shellvar: "#{source_dir}"
27 changes: 25 additions & 2 deletions lib/puppet/provider/shellvar/augeas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def get_values(path, aug)
aug.match('$resource/*').map { |p| aug.get(p) }
else
value = aug.get('$resource')
if value =~ /^(["'])(.*)(\1)$/
if value =~ /^(["'])(.*)(\1)$/m
value = $2
end
[value]
Expand All @@ -153,7 +153,30 @@ def set_values(path, aug, values)
case my_array_type
when :string
oldvalue = aug.get("#{path}/#{resource[:variable]}")
aug.set("#{path}/#{resource[:variable]}", quoteit(values.join(' '), oldvalue))

# When dealing with array entries
# try hard to keep spacing by using Shellvars_list
aug.rm('/parsed')
aug.set('/input', "#{resource[:variable]}=#{oldvalue}\n")
if values.size > 1 \
and aug.respond_to? :text_store \
and aug.text_store('Shellvars_list.lns', '/input', '/parsed')

# Replace all values
aug.rm("/parsed/#{resource[:variable]}/value")
# Set automatic quoting
aug.set("/parsed/#{resource[:variable]}/quote", whichquote(values.join(' '), oldvalue))
values.each do |v|
aug.set("/parsed/#{resource[:variable]}/value[last()+1]", v)
end

# Transform back into Shellvars format
aug.text_retrieve('Shellvars_list.lns', '/input', '/parsed', '/newvalue')
newvalue = aug.get('/newvalue').sub("#{resource[:variable]}=", '').chomp
aug.set("#{path}/#{resource[:variable]}", newvalue)
else
aug.set("#{path}/#{resource[:variable]}", quoteit(values.join(' '), oldvalue))
end
when :array
values.each_with_index do |v, i|
aug.set("#{path}/#{resource[:variable]}/#{i}", quoteit(v))
Expand Down
20 changes: 9 additions & 11 deletions lib/puppet/type/shellvar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,18 @@ def sync
end

def insync?(is)
if provider.resource[:array_append]
should_arr = Array(should)
should_arr = Array(should)

# Join and split to ensure all elements are parsed
is_str = is.is_a?(Array) ? is.join(' ') : is
is_arr = is_str.split(' ')
# Join and split to ensure all elements are parsed
is_str = is.is_a?(Array) ? is.join(' ') : is
is_arr = is_str.split(' ')

if provider.resource[:array_append]
(should_arr - is_arr).empty?
elsif should.size > 1
should_arr == is_arr
else
case provider.array_type
when :string
is == Array(should.join(' '))
when :array
is == should
end
should == is
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/unit/puppet/provider/shellvar/augeas/full
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ export EXAMPLE_E=baz

STR_LIST="foo bar baz"
LST_LIST=(foo "bar baz" 123)

ML_LIST="foo
bar
baz"
38 changes: 38 additions & 0 deletions spec/unit/puppet/provider/shellvar/augeas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@
{ "2" = "\"bar baz\"" }
{ "3" = "123" }
}
{ "ML_LIST" = "\"foo
bar
baz\"" }
')
else
augparse_filter(target, "Shellvars.lns", '*[preceding-sibling::#comment[.=~regexp(".*sync hw clock.*")]]', '
Expand Down Expand Up @@ -234,6 +237,9 @@
{ "2" = "\"bar baz\"" }
{ "3" = "123" }
}
{ "ML_LIST" = "\"foo
bar
baz\"" }
')
else
augparse_filter(target, "Shellvars.lns", '*[preceding-sibling::#comment[.=~regexp(".*sync hw clock.*")]]', '
Expand Down Expand Up @@ -451,6 +457,22 @@
{ "LST_LIST" = "\"foo baz\"" }
')
end

it "should update value in multiline string" do
apply!(Puppet::Type.type(:shellvar).new(
:name => "ML_LIST",
:value => ["foo", "123", "baz"],
:array_type => 'string',
:target => target,
:provider => "augeas"
))

augparse_filter(target, "Shellvars.lns", "ML_LIST", '
{ "ML_LIST" = "\"foo
123
baz\"" }
')
end
end

describe "when using array_append" do
Expand All @@ -467,6 +489,22 @@
{ "STR_LIST" = "\"foo bar baz fooz\"" }
')
end

it "should not remove existing values in multiline entry" do
apply!(Puppet::Type.type(:shellvar).new(
:name => "ML_LIST",
:value => ["foo", "fooz"],
:array_append => true,
:target => target,
:provider => "augeas"
))

augparse_filter(target, "Shellvars.lns", "ML_LIST", '
{ "ML_LIST" = "\"foo
bar
baz fooz\"" }
')
end
end

describe "when updating comment" do
Expand Down

0 comments on commit 869db0e

Please sign in to comment.