Skip to content

Commit

Permalink
refactor: Use named parameters instead of positional in `loadAndValid…
Browse files Browse the repository at this point in the history
…ateYaml`
  • Loading branch information
stefreak committed Dec 10, 2024
1 parent bc2812e commit e36fa6f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 29 deletions.
27 changes: 16 additions & 11 deletions core/src/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,17 @@ export const allConfigKinds = ["Module", "Workflow", "Project", configTemplateKi
* @param sourceDescription - A description of the location of the yaml file, e.g. "bar.yaml at directory /foo/".
* @param version - YAML standard version. Defaults to "1.2"
*/
export async function loadAndValidateYaml(
content: string,
sourceDescription: string,
filename: string | undefined,
version: DocumentOptions["version"] = "1.2"
): Promise<YamlDocumentWithSource[]> {
export async function loadAndValidateYaml({
content,
sourceDescription,
filename,
version = "1.2",
}: {
content: string
sourceDescription: string
filename: string | undefined
version?: DocumentOptions["version"]
}): Promise<YamlDocumentWithSource[]> {
try {
return Array.from(parseAllDocuments(content, { merge: true, strict: false, version }) || []).map((doc) => {
if (doc.errors.length > 0) {
Expand Down Expand Up @@ -200,11 +205,11 @@ export async function validateRawConfig({
projectRoot: string
allowInvalid?: boolean
}) {
let rawSpecs = await loadAndValidateYaml(
rawConfig,
`${basename(configPath)} in directory ${dirname(configPath)}`,
configPath
)
let rawSpecs = await loadAndValidateYaml({
content: rawConfig,
sourceDescription: `${basename(configPath)} in directory ${dirname(configPath)}`,
filename: configPath,
})

// Ignore empty resources
rawSpecs = rawSpecs.filter(Boolean)
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/kubernetes-type/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ async function parseKubernetesManifests(
): Promise<KubernetesResource[]> {
// parse yaml with version 1.1 by default, as Kubernetes still uses this version.
// See also https://github.com/kubernetes/kubernetes/issues/34146
const docs = await loadAndValidateYaml(str, sourceDescription, "1.1")
const docs = await loadAndValidateYaml({ content: str, sourceDescription, filename, version: "1.1" })

// TODO: We should use schema validation to make sure that apiVersion, kind and metadata are always defined as required by the type.
const manifests = docs.map((d) => d.toJS()) as KubernetesResource[]
Expand Down
38 changes: 32 additions & 6 deletions core/test/unit/src/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,11 @@ describe("loadAndValidateYaml", () => {
name: foo
`

const yamlDocs = await loadAndValidateYaml(yaml, "foo.yaml in directory bar", "bar/foo.yaml")
const yamlDocs = await loadAndValidateYaml({
content: yaml,
sourceDescription: "foo.yaml in directory bar",
filename: "bar/foo.yaml",
})

expect(yamlDocs).to.have.length(1)
expect(yamlDocs[0].source).to.equal(yaml)
Expand All @@ -633,7 +637,11 @@ describe("loadAndValidateYaml", () => {
name: doc3
`

const yamlDocs = await loadAndValidateYaml(yaml, "foo.yaml in directory bar", "bar/foo.yaml")
const yamlDocs = await loadAndValidateYaml({
content: yaml,
sourceDescription: "foo.yaml in directory bar",
filename: "bar/foo.yaml",
})

expect(yamlDocs).to.have.length(3)

Expand Down Expand Up @@ -662,7 +670,11 @@ describe("loadAndValidateYaml", () => {
newYamlOctalNumber: 0o777
`

const yamlDocs = await loadAndValidateYaml(yaml, "foo.yaml in directory bar", "bar/foo.yaml")
const yamlDocs = await loadAndValidateYaml({
content: yaml,
sourceDescription: "foo.yaml in directory bar",
filename: "bar/foo.yaml",
})

expect(yamlDocs).to.have.length(1)
expect(yamlDocs[0].source).to.equal(yaml)
Expand All @@ -684,7 +696,11 @@ describe("loadAndValidateYaml", () => {
newYamlOctalNumber: 0o777
`

const yamlDocs = await loadAndValidateYaml(yaml, "foo.yaml in directory bar", "bar/foo.yaml")
const yamlDocs = await loadAndValidateYaml({
content: yaml,
sourceDescription: "foo.yaml in directory bar",
filename: "bar/foo.yaml",
})

expect(yamlDocs).to.have.length(1)
expect(yamlDocs[0].source).to.equal(yaml)
Expand All @@ -704,7 +720,12 @@ describe("loadAndValidateYaml", () => {
`

// we use the version parameter to force the yaml 1.1 standard
const yamlDocs = await loadAndValidateYaml(yaml, "foo.yaml in directory bar", "bar/foo.yaml", "1.1")
const yamlDocs = await loadAndValidateYaml({
content: yaml,
sourceDescription: "foo.yaml in directory bar",
filename: "bar/foo.yaml",
version: "1.1",
})

expect(yamlDocs).to.have.length(1)
expect(yamlDocs[0].source).to.equal(yaml)
Expand All @@ -720,7 +741,12 @@ describe("loadAndValidateYaml", () => {
`

await expectError(
() => loadAndValidateYaml(yaml, "foo.yaml in directory bar", "bar/foo.yaml"),
() =>
loadAndValidateYaml({
content: yaml,
sourceDescription: "foo.yaml in directory bar",
filename: "bar/foo.yaml",
}),
(err) => {
expect(err.message).to.eql(dedent`
Could not parse foo.yaml in directory bar as valid YAML: YAMLException: unidentified alias "bar" (1:10)
Expand Down
6 changes: 5 additions & 1 deletion core/test/unit/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ describe("validateSchema", () => {
name: bar
`

const yamlDocs = await loadAndValidateYaml(yaml, "foo.yaml in directory bar", "bar/foo.yaml")
const yamlDocs = await loadAndValidateYaml({
content: yaml,
sourceDescription: "foo.yaml in directory bar",
filename: "bar/foo.yaml",
})
const yamlDoc = yamlDocs[1]

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
10 changes: 5 additions & 5 deletions core/test/unit/src/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,16 @@ describe("resolveTemplateString", () => {

it("if available, should include yaml context in error message", async () => {
const command = "${resol${part}ed}"
const yamlDoc = await loadAndValidateYaml(
dedent`
const yamlDoc = await loadAndValidateYaml({
content: dedent`
name: test,
kind: Build
spec:
command: '${command}'
`,
"test",
"bar/foo.yaml"
)
sourceDescription: "test",
filename: "bar/foo.yaml",
})
void expectError(
() =>
resolveTemplateString({
Expand Down
10 changes: 5 additions & 5 deletions plugins/pulumi/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ export async function applyConfig(params: PulumiParams & { previewDirPath?: stri
let stackConfigFileExists: boolean
try {
const fileData = await readFile(stackConfigPath)
const stackConfigDocs = await loadAndValidateYaml(
fileData.toString(),
`${basename(stackConfigPath)} in directory ${dirname(stackConfigPath)}`,
stackConfigPath
)
const stackConfigDocs = await loadAndValidateYaml({
content: fileData.toString(),
sourceDescription: `${basename(stackConfigPath)} in directory ${dirname(stackConfigPath)}`,
filename: stackConfigPath,
})
stackConfig = stackConfigDocs[0].toJS()
stackConfigFileExists = true
} catch (err) {
Expand Down

0 comments on commit e36fa6f

Please sign in to comment.