Skip to content

Commit 762497d

Browse files
(CAT-2484): Stop yaml file from being overwritten
This change is simple in that it moves some of the logic to the PuppetLint. Every file gets its own PuppetLint object with its own list of problems, checks, etc. Therefore, each PuppetLint should know itself (*.pp? *.yml? *.yaml?) and whether it is a valid candidate for fixing. Keep all validation and fix logic on the PuppetLint instead of in the bin.rb executable. Signed-off-by: Gavin Didrichsen <[email protected]>
1 parent b69990a commit 762497d

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

lib/puppet-lint.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,42 @@ def print_problems
235235
report(@problems)
236236
end
237237

238+
# Public: Write fixes back to the file if this file type supports fixes.
239+
#
240+
# Returns nothing.
241+
def write_fixes
242+
return unless should_write_fixes?
243+
244+
File.binwrite(@path, @manifest)
245+
end
246+
247+
# Internal: Determine if fixes should be written for this file.
248+
#
249+
# Returns true if all conditions are met for writing fixes, false otherwise.
250+
def should_write_fixes?
251+
# Don't write if file type doesn't support fixes
252+
return false unless supports_fixes?
253+
254+
# Don't write if there are syntax errors (can't safely fix)
255+
return false if @problems&.any? { |r| r[:check] == :syntax }
256+
257+
# Don't write if there's no manifest content
258+
return false if @manifest.nil? || @manifest.empty?
259+
260+
true
261+
end
262+
263+
# Public: Determine if this file type supports automatic fixes.
264+
#
265+
# Returns true if fixes are supported for this file type, false otherwise.
266+
def supports_fixes?
267+
return false if @path.nil?
268+
269+
# Only .pp files support fixes currently
270+
# YAML files and other types may support fixes in the future
271+
File.extname(@path).match?(%r{\.pp$}i)
272+
end
273+
238274
# Public: Define a new check.
239275
#
240276
# name - A unique name for the check as a Symbol.

lib/puppet-lint/bin.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ def run
9090

9191
return_val = 1 if l.errors? || (l.warnings? && PuppetLint.configuration.fail_on_warnings)
9292

93-
next unless PuppetLint.configuration.fix && l.problems.none? { |r| r[:check] == :syntax }
94-
95-
File.binwrite(f, l.manifest)
93+
l.write_fixes if PuppetLint.configuration.fix
9694
end
9795

9896
if PuppetLint.configuration.sarif

0 commit comments

Comments
 (0)