Skip to content

Commit

Permalink
test: add test for error in doc in multi-doc YAML (TBS)
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Jul 26, 2023
1 parent 0c7f5d1 commit ec166f2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
4 changes: 2 additions & 2 deletions core/src/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ export async function loadAndValidateYaml(content: string, path: string): Promis
await lint(content)
} catch (linterErr) {
throw new ConfigurationError({
message: `Could not parse ${basename(path)} in directory ${path} as valid YAML: ${err.message}`,
message: `Could not parse ${path} as valid YAML: ${err.message}`,
detail: linterErr,
})
}
// ... but default to throwing a generic error, in case the error wasn't caught by yaml-lint.
throw new ConfigurationError({
message: `Could not parse ${basename(path)} in directory ${path} as valid YAML.`,
message: `Could not parse ${path} as valid YAML.`,
detail: err,
})
}
Expand Down
58 changes: 57 additions & 1 deletion core/test/unit/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { expect } from "chai"
import { joi } from "../../../../src/config/common"
import { validateSchema } from "../../../../src/config/validation"
import { expectError } from "../../../helpers"
import { BaseGardenResource, YamlDocumentWithSource, baseInternalFieldsSchema } from "../../../../src/config/base"
import {
BaseGardenResource,
YamlDocumentWithSource,
baseInternalFieldsSchema,
loadAndValidateYaml,
} from "../../../../src/config/base"
import { GardenApiVersion } from "../../../../src/constants"
import { parseDocument } from "yaml"
import { dedent } from "../../../../src/util/string"
Expand Down Expand Up @@ -230,6 +235,57 @@ describe("validateSchema", () => {
)
})

it("shows correct position of error if yamlDoc with multiple configs is attached to config", async () => {
const schema = joi.object().keys({
apiVersion: joi.string(),
kind: joi.string(),
name: joi.string(),
internal: baseInternalFieldsSchema(),
spec: joi.object().keys({
foo: joi.string(),
}),
})

const yaml = dedent`
apiVersion: v1
kind: Test
spec:
foo: 123
name: foo
---
apiVersion: v1
kind: Test
spec:
foo: 456
name: bar
`

const yamlDocs = await loadAndValidateYaml(yaml, "/foo")
const yamlDoc = yamlDocs[1]

const config: any = {
...yamlDoc.toJS(),
internal: {
basePath: "/foo",
yamlDoc,
},
}

void expectError(
() => validateSchema(config, schema, { yamlDoc, yamlDocBasePath: [] }),
(err) =>
expect(stripAnsi(err.message)).to.equal(dedent`
Validation error:
...
9 | spec:
10 | foo: 456
-------------^
spec.foo must be a string
`)
)
})

it("shows correct position of error if yamlDoc is attached to config and yamlDocBasePath is set", () => {
const schema = joi.object().keys({
foo: joi.string(),
Expand Down

0 comments on commit ec166f2

Please sign in to comment.