lib/pdk/validate/yaml/yaml_syntax_validator.rb in pdk-2.4.0 vs lib/pdk/validate/yaml/yaml_syntax_validator.rb in pdk-2.5.0

- old
+ new

@@ -1,95 +1,95 @@ -require 'pdk' - -module PDK - module Validate - module YAML - class YAMLSyntaxValidator < InternalRubyValidator - YAML_ALLOWLISTED_CLASSES = [Symbol].freeze - - def ignore_dotfiles - false - end - - def name - 'yaml-syntax' - end - - def pattern - [ - '**/*.yaml', - '**/*.yml', - ].tap do |pat| - if context.is_a?(PDK::Context::ControlRepo) - pat.concat( - [ - '**/*.eyaml', - '**/*.eyml', - ], - ) - else - pat - end - end - end - - def spinner_text - _('Checking YAML syntax (%{patterns}).') % { - patterns: pattern.join(' '), - } - end - - def validate_target(report, target) - return 0 unless PDK::Util::Filesystem.file?(target) - - unless PDK::Util::Filesystem.readable?(target) - report.add_event( - file: target, - source: name, - state: :failure, - severity: 'error', - message: _('Could not be read.'), - ) - return 1 - end - - begin - ::YAML.safe_load(PDK::Util::Filesystem.read_file(target), YAML_ALLOWLISTED_CLASSES, [], true) - - report.add_event( - file: target, - source: name, - state: :passed, - severity: 'ok', - ) - return 0 - rescue Psych::SyntaxError => e - report.add_event( - file: target, - source: name, - state: :failure, - severity: 'error', - line: e.line, - column: e.column, - message: _('%{problem} %{context}') % { - problem: e.problem, - context: e.context, - }, - ) - return 1 - rescue Psych::DisallowedClass => e - report.add_event( - file: target, - source: name, - state: :failure, - severity: 'error', - message: _('Unsupported class: %{message}') % { - message: e.message, - }, - ) - return 1 - end - end - end - end - end -end +require 'pdk' + +module PDK + module Validate + module YAML + class YAMLSyntaxValidator < InternalRubyValidator + YAML_ALLOWLISTED_CLASSES = [Symbol].freeze + + def ignore_dotfiles + false + end + + def name + 'yaml-syntax' + end + + def pattern + [ + '**/*.yaml', + '**/*.yml', + ].tap do |pat| + if context.is_a?(PDK::Context::ControlRepo) + pat.concat( + [ + '**/*.eyaml', + '**/*.eyml', + ], + ) + else + pat + end + end + end + + def spinner_text + _('Checking YAML syntax (%{patterns}).') % { + patterns: pattern.join(' '), + } + end + + def validate_target(report, target) + return 0 unless PDK::Util::Filesystem.file?(target) + + unless PDK::Util::Filesystem.readable?(target) + report.add_event( + file: target, + source: name, + state: :failure, + severity: 'error', + message: _('Could not be read.'), + ) + return 1 + end + + begin + ::YAML.safe_load(PDK::Util::Filesystem.read_file(target), YAML_ALLOWLISTED_CLASSES, [], true) + + report.add_event( + file: target, + source: name, + state: :passed, + severity: 'ok', + ) + return 0 + rescue Psych::SyntaxError => e + report.add_event( + file: target, + source: name, + state: :failure, + severity: 'error', + line: e.line, + column: e.column, + message: _('%{problem} %{context}') % { + problem: e.problem, + context: e.context, + }, + ) + return 1 + rescue Psych::DisallowedClass => e + report.add_event( + file: target, + source: name, + state: :failure, + severity: 'error', + message: _('Unsupported class: %{message}') % { + message: e.message, + }, + ) + return 1 + end + end + end + end + end +end