Skip to content

Commit

Permalink
Merge pull request #1425 from puppetlabs/CAT-2164
Browse files Browse the repository at this point in the history
(CAT-2164) Correct `yaml_syntax_validator` to error on empty file
  • Loading branch information
danadoherty639 authored Dec 18, 2024
2 parents 1fb9df2 + 37e28ff commit 7a1d7ef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/pdk/validate/yaml/yaml_syntax_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,25 @@ def validate_target(report, target)
end

begin
::YAML.safe_load(PDK::Util::Filesystem.read_file(target), permitted_classes: YAML_ALLOWLISTED_CLASSES, permitted_symbols: [], aliases: true)

report.add_event(
file: target,
source: name,
state: :passed,
severity: 'ok'
)
0
data = ::YAML.safe_load(PDK::Util::Filesystem.read_file(target), permitted_classes: YAML_ALLOWLISTED_CLASSES, permitted_symbols: [], aliases: true)
if data.is_a?(Hash)
report.add_event(
file: target,
source: name,
state: :passed,
severity: 'ok'
)
0
else
report.add_event(
file: target,
source: name,
state: :failure,
severity: 'error',
message: format('File does not contain a valid YAML hash.')
)
1
end
rescue Psych::SyntaxError => e
report.add_event(
file: target,
Expand Down
15 changes: 15 additions & 0 deletions spec/unit/pdk/validate/yaml/yaml_syntax_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,20 @@
expect(return_value).to eq(1)
end
end

context 'when a target is provided that contains no YAML' do
let(:target) { { name: '.sync.yaml', content: '' } }

it 'adds a failure event to the report' do
expect(report).to receive(:add_event).with({
file: target[:name],
source: 'yaml-syntax',
state: :failure,
severity: 'error',
message: a_string_matching(/\AFile does not contain a valid YAML hash/)
})
expect(return_value).to eq(1)
end
end
end
end

0 comments on commit 7a1d7ef

Please sign in to comment.