Repository navigation
feat(validate): keep the file of each resource and show it in the results - #661
Conversation
The loaders return manifests, and a manifest does not say which file it came from. So a command that loads the files of a directory cannot tell the user which file a problem is in, and two resources with the same name in different files cannot be told apart. Add DecodeDocuments, which turns the documents that a document loader returns (NewDocumentLoader, NewExactDocumentLoader) into manifests the same way the other loaders do, and keeps the file of each one. A manifest and its file are returned together as a sourced.Resource, a type in a package of its own because pkg/load and pkg/validate both use it. Nothing uses it yet, and the existing loaders are unchanged. Signed-off-by: Theo Chatzimichos <tampakrap@gmail.com>
A validation result names a resource by its kind and its metadata.name. In a directory of many files that does not say which file to fix, and two resources with the same name look the same. Add SchemaValidateSourced, which is SchemaValidate for sourced.Resources, and put the file of each resource in its result, as a new source field (shown in the JSON and YAML output). SchemaValidate does not change, and neither does the output of anything that calls it: the field is left out when the file is not known. Signed-off-by: Theo Chatzimichos <tampakrap@gmail.com>
The text output names a resource by its kind and its metadata.name only. When the file of a resource is known (see SchemaValidateSourced), start its lines with it, after the marker: [x] dir/a.yaml: schema validation error ... Results without a file are printed as before, so the output of crossplane validate does not change. Signed-off-by: Theo Chatzimichos <tampakrap@gmail.com>
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the source-file tracking changes.
No open issues 🎉
What changed in this PR
Adds source-file tracking from manifest loading through validation output, laying groundwork for crossplane test lint without changing existing crossplane validate output.
Changes:
- Introduces sourced resources and document decoding that preserves file paths.
- Adds source-aware validation and optional source fields in structured output.
- Prefixes text results with known source paths and adds focused tests.
| File | Description |
|---|---|
pkg/validate/validate.go |
Adds source-aware validation entry point. |
pkg/validate/validate_test.go |
Tests source mapping and input order. |
pkg/validate/types.go |
Adds optional result source field. |
pkg/validate/output/render.go |
Prefixes text results with source paths. |
pkg/validate/output/render_test.go |
Tests output with and without sources. |
pkg/load/sourced/sourced.go |
Defines a resource paired with its source. |
pkg/load/loader.go |
Decodes documents while preserving sources. |
pkg/load/loader_test.go |
Tests decoding, pipeline inputs, and errors. |
Review effort: Balanced
Codecov Report❌ Patch coverage is 🚀 New features to boost your workflow:
|
|
|
||
| func TestDecodeDocumentsError(t *testing.T) { | ||
| got, err := DecodeDocuments([]Document{{Source: "bad.yaml", Raw: []byte("name: ci\njobs: {}\n")}}, false) | ||
| if err == nil || !strings.Contains(err.Error(), "cannot parse YAML manifest") { |
There was a problem hiding this comment.
we try to avoid string checks on errors in favor of sentinel errors; is that doable here?
that should also make this case slide into the table-driven test as a row vs. a separate test
There was a problem hiding this comment.
I pushed an alternative, still not a sentinel because the error comes from Unmarshal, but it is closer to what you propose so that we don't have a string check
bcd2155 to
e7eb45f
Compare
Description of your changes
A loaded manifest doesn't say which file it came from, and a validation result names a resource only by its kind and
metadata.name. In a directory of many files, that doesn't tell the user which file to fix, and two resources with the same name in different files look the same.Let the loaders and the validator carry the file that each resource came from, and let the text output show it. This is groundwork for
crossplane test lint, which reports the file of each test suite it checks. Nothing else calls this yet, so no output changes:sourceis left out of the JSON and YAML output, and the text lines have no prefix, when the file isn't known.crossplane validatenever sets it.Commits:
feat(load): add DecodeDocuments to keep the file of each manifest.DecodeDocumentsturns the documents of a document loader (NewDocumentLoader,NewExactDocumentLoader) into manifests the same way the other loaders do, and keeps the file of each one. A manifest and its file are returned together as asourced.Resource. That type is in a package of its own (pkg/load/sourced) becausepkg/loadandpkg/validateboth use it, and neither should import the other. The existing loaders are unchanged.feat(validate): record the file of each resource in its result.SchemaValidateSourcedisSchemaValidateforsourced.Resources, and puts the file in a newsourcefield of the result, shown in JSON and YAML output.SchemaValidateis unchanged.feat(validate): show the file of a resource in the text output. When a result has a file, its lines start with it after the marker, for example[x] dir/a.yaml: schema validation error .... Results without a file are printed as before.Related:
crossplane test#115crossplane test lint#116I have:
./nix.sh flake checkto ensure this PR is ready for review.Linked a PR or a docs tracking issue to document this change.Addedbackport release-x.ylabels to auto-backport this PR.