Skip to content

Commit

Permalink
(CAT-2164) Correct yaml_syntax_validator to error on empty file
Browse files Browse the repository at this point in the history
Previously empty yaml files would pass and be accepted as valid when PDK validate was run.
However this was contrasted by Puppet and the puppetserver requiring files to always contain a valid yaml and so we are updating the pdk to be in line with this.
  • Loading branch information
david22swan committed Dec 18, 2024
1 parent 1fb9df2 commit 37e28ff
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 37e28ff

Please sign in to comment.