From bcab71ded8507de4fd9f89b4dcf798b3d98ace59 Mon Sep 17 00:00:00 2001 From: Hailee Kenney Date: Thu, 6 Oct 2016 15:23:35 -0700 Subject: (MODULES-3590) Fix match_for_absence parameter Prior to this commit, due to a bug in the exists? method in the file_line provider, match_for_absence didn't work as described (or at all really). Update the exists? logic so that match_for_absence works as described. Additionally add a unit test to prevent regressions and update the documentation for the parameter to reflect the fact that it is ignored when `ensure => present`. --- README.markdown | 2 +- lib/puppet/provider/file_line/ruby.rb | 6 +++++- lib/puppet/type/file_line.rb | 3 ++- spec/unit/puppet/provider/file_line/ruby_spec.rb | 7 +++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index d27668b..34d26a8 100644 --- a/README.markdown +++ b/README.markdown @@ -128,7 +128,7 @@ All parameters are optional, unless otherwise noted. * `ensure`: Ensures whether the resource is present. Valid options: 'present', 'absent'. Default: 'present'. * `line`: **Required.** Sets the line to be added to the file located by the `path` parameter. Valid options: String. Default: Undefined. * `match`: Specifies a regular expression to run against existing lines in the file; if a match is found, it is replaced rather than adding a new line. A regex comparison is performed against the line value, and if it does not match, an exception is raised. Valid options: String containing a regex. Default: Undefined. -* `match_for_absence`: An optional value to determine if match should be applied when `ensure => absent`. If set to true and match is set, the line that matches match will be deleted. If set to false (the default), match is ignored when `ensure => absent` and the value of `line` is used instead. Default: false. +* `match_for_absence`: An optional value to determine if match should be applied when `ensure => absent`. If set to true and match is set, the line that matches match will be deleted. If set to false (the default), match is ignored when `ensure => absent` and the value of `line` is used instead. Ignored when `ensure => present`. Default: false. * `multiple`: Determines if `match` and/or `after` can change multiple lines. If set to false, an exception will be raised if more than one line matches. Valid options: 'true', 'false'. Default: Undefined. * `name`: Sets the name to use as the identity of the resource. This is necessary if you want the resource namevar to differ from the supplied `title` of the resource. Valid options: String. Default: Undefined. * `path`: **Required.** Defines the file in which Puppet will ensure the line specified by `line`. Must be an absolute path to the file. diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index aab6fe2..beeb430 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -4,7 +4,11 @@ Puppet::Type.type(:file_line).provide(:ruby) do true else lines.find do |line| - line.chomp == resource[:line].chomp + if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true' + line.chomp =~ Regexp.new(resource[:match]) + else + line.chomp == resource[:line].chomp + end end end end diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 6f5c188..7b7d44e 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -71,7 +71,8 @@ Puppet::Type.newtype(:file_line) do newparam(:match_for_absence) do desc 'An optional value to determine if match should be applied when ensure => absent.' + ' If set to true and match is set, the line that matches match will be deleted.' + - ' If set to false (the default), match is ignored when ensure => absent.' + ' If set to false (the default), match is ignored when ensure => absent.' + + ' When `ensure => present`, match_for_absence is ignored.' newvalues(true, false) defaultto false end diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index fdeaf1a..1f41f62 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -363,6 +363,13 @@ describe provider_class do @provider = provider_class.new(@resource) end + it 'should find a line to match' do + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo\nfoo2") + end + expect(@provider.exists?).to be_truthy + end + it 'should remove one line if it matches' do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo\nfoo2") -- cgit v1.2.3