Skip to content

Commit be1ff3f

Browse files
committed
Merge pull request #585 from jearls/MODULES-2370-update-validate-to-not-require-line-when-matching-for-absence
[MODULES-2370] file_line.rb: Fix `line` attribute validation
2 parents 6248d24 + 7e408ca commit be1ff3f

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/puppet/type/file_line.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,13 @@
111111
end
112112

113113
validate do
114-
unless self[:line] and self[:path]
115-
raise(Puppet::Error, "Both line and path are required attributes")
114+
unless self[:line]
115+
unless (self[:ensure].to_s == 'absent') and (self[:match_for_absence].to_s == 'true') and self[:match]
116+
raise(Puppet::Error, "line is a required attribute")
117+
end
118+
end
119+
unless self[:path]
120+
raise(Puppet::Error, "path is a required attribute")
116121
end
117122
end
118123
end

spec/unit/puppet/type/file_line_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@
4141
expect { file_line[:path] = 'file' }.to raise_error(Puppet::Error, /File paths must be fully qualified/)
4242
end
4343
it 'should require that a line is specified' do
44-
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => '/tmp/file') }.to raise_error(Puppet::Error, /Both line and path are required attributes/)
44+
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => '/tmp/file') }.to raise_error(Puppet::Error, /line is a required attribute/)
45+
end
46+
it 'should not require that a line is specified when matching for absence' do
47+
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => '/tmp/file', :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error
4548
end
4649
it 'should require that a file is specified' do
47-
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.to raise_error(Puppet::Error, /Both line and path are required attributes/)
50+
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.to raise_error(Puppet::Error, /path is a required attribute/)
4851
end
4952
it 'should default to ensure => present' do
5053
expect(file_line[:ensure]).to eq :present

0 commit comments

Comments
 (0)