Skip to content

Commit 0d631dc

Browse files
committed
Modify in_position? method to fix inconsistencies
in_position? method wasn't working right due to issues with using following-sibling when before and preceding-sibling when after, this corrects it with new, hopefully proper assumptions. The only item that small change didn't fix was last() which was not working for either because of the way siblings treat the last() command as it will always resolve to true on the first match in those cases. This patch assumes that you'll never have more than one [last()] in your xpath for positioning and swaps it out with the correct final array value of augeas matches. We use an invert boolean on 'after' positions as we will always have an empty? resolving as true if the match exists before the item we're looking for. Whereas we'll always have empty? as false if we do a 'before' and the actual item currently exists after the line we're trying to match.
1 parent 7d2931f commit 0d631dc

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/puppet/provider/pam/augeas.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,23 @@ def in_position?
6464
unless resource[:position].nil?
6565
path, before = self.class.position_path(resource[:position], resource[:type])
6666

67-
if before == 'before'
68-
mpath = "#{resource_path}[following-sibling::#{path}]"
69-
else
70-
mpath = "#{resource_path}[preceding-sibling::#{path}]"
67+
# Would someone ever have an xpath for positioning that would include more than one [last()] ?
68+
# This section banks on there only ever being one [last()]...
69+
# We are unable to use [last()] inside the preceding-sibling block as it would always result in [1] so we do an
70+
# extra call to augeas to find the last value of the matches and subtract 1 to match the proper lookup/emulate last()
71+
if path.include?('[last()]')
72+
last_value = augopen.match(path)
73+
return false if last_value.empty?
74+
last_value = last_value[0].split('/').last.to_i - 1
75+
path.sub!(/\[last\(\)\]/, "[#{last_value}]")
7176
end
77+
mpath = "#{resource_path}[preceding-sibling::#{path}]"
7278

79+
augmatch = false
7380
augopen do |aug|
74-
!aug.match(mpath).empty?
81+
augmatch = aug.match(mpath).empty?
82+
# Since we use preceding-sibling for everything, we invert on the result if 'after' instead as it's more consistent
83+
before == 'before' ? augmatch : !augmatch
7584
end
7685
end
7786
end

0 commit comments

Comments
 (0)