From a417eec41b0700f5baa3099d713e1c273f5523f5 Mon Sep 17 00:00:00 2001 From: Steffen Neubauer Date: Tue, 10 Dec 2024 12:16:19 +0100 Subject: [PATCH] refactor: Use named parameters instead of positional in `loadAndValidateYaml` --- core/src/config/base.ts | 27 +++++++++++-------- .../kubernetes/kubernetes-type/common.ts | 2 +- core/test/unit/src/config/base.ts | 6 ++++- core/test/unit/src/config/validation.ts | 6 ++++- core/test/unit/src/template-string.ts | 10 +++---- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/core/src/config/base.ts b/core/src/config/base.ts index 1b9c4b11bd..597869e96a 100644 --- a/core/src/config/base.ts +++ b/core/src/config/base.ts @@ -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 { +export async function loadAndValidateYaml({ + content, + sourceDescription, + filename, + version = "1.2", +}: { + content: string + sourceDescription: string + filename: string | undefined + version?: DocumentOptions["version"] +}): Promise { try { return Array.from(parseAllDocuments(content, { merge: true, strict: false, version }) || []).map((doc) => { if (doc.errors.length > 0) { @@ -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) diff --git a/core/src/plugins/kubernetes/kubernetes-type/common.ts b/core/src/plugins/kubernetes/kubernetes-type/common.ts index e0a29ef51c..e455ac5cf3 100644 --- a/core/src/plugins/kubernetes/kubernetes-type/common.ts +++ b/core/src/plugins/kubernetes/kubernetes-type/common.ts @@ -461,7 +461,7 @@ async function parseKubernetesManifests( ): Promise { // 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[] diff --git a/core/test/unit/src/config/base.ts b/core/test/unit/src/config/base.ts index 2aa7efe624..4b3a72a9c8 100644 --- a/core/test/unit/src/config/base.ts +++ b/core/test/unit/src/config/base.ts @@ -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) diff --git a/core/test/unit/src/config/validation.ts b/core/test/unit/src/config/validation.ts index 907af96f38..cf33bc47ae 100644 --- a/core/test/unit/src/config/validation.ts +++ b/core/test/unit/src/config/validation.ts @@ -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 diff --git a/core/test/unit/src/template-string.ts b/core/test/unit/src/template-string.ts index fea3c72a48..d833944b8e 100644 --- a/core/test/unit/src/template-string.ts +++ b/core/test/unit/src/template-string.ts @@ -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({