Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: consolidate file parsing implementation #1663

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integration/cli/build.noembed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe("build", () => {
expect(build.stderr.join("").trim()).toContain("Error: Cannot find module");
expect(build.stdout.join("").trim()).toContain("");

packageJson = await resource.oneFromFile(`${testModule}/package.json`);
packageJson = await resource.resourcesFromFile(`${testModule}/package.json`);
uuid = packageJson.pepr.uuid;
},
time.toMs("1m"),
Expand Down
26 changes: 18 additions & 8 deletions integration/cli/build.nonconflict.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe("build", () => {
expect(build.stderr.join("").trim()).toBe("");
expect(build.stdout.join("").trim()).toContain("K8s resource for the module saved");

packageJson = await resource.oneFromFile(`${testModule}/package.json`);
packageJson = await resource.resourcesFromFile(`${testModule}/package.json`);
uuid = packageJson.pepr.uuid;
},
time.toMs("1m"),
Expand All @@ -93,7 +93,9 @@ describe("build", () => {
});

it("--custom-image, works", async () => {
const moduleYaml = await resource.manyFromFile(`${outputDir}/pepr-module-${uuid}.yaml`);
const moduleYaml = await resource.resourcesFromFile(
`${outputDir}/pepr-module-${uuid}.yaml`,
);
{
const admission = resource.select(moduleYaml, kind.Deployment, `pepr-${uuid}`);
const admissionImage = getDepConImg(admission, "server");
Expand All @@ -104,13 +106,15 @@ describe("build", () => {
expect(watcherImage).toBe(customImage);
}

const zarfYaml = await resource.oneFromFile(`${outputDir}/zarf.yaml`);
const zarfYaml = await resource.resourcesFromFile(`${outputDir}/zarf.yaml`);
{
const componentImage = zarfYaml.components.at(0).images.at(0);
expect(componentImage).toBe(customImage);
}

const valuesYaml = await resource.oneFromFile(`${outputDir}/${uuid}-chart/values.yaml`);
const valuesYaml = await resource.resourcesFromFile(
`${outputDir}/${uuid}-chart/values.yaml`,
);
{
const admissionImage = valuesYaml.admission.image;
expect(admissionImage).toBe(customImage);
Expand All @@ -121,7 +125,9 @@ describe("build", () => {
});

it("--timeout, works", async () => {
const moduleYaml = await resource.manyFromFile(`${outputDir}/pepr-module-${uuid}.yaml`);
const moduleYaml = await resource.resourcesFromFile(
`${outputDir}/pepr-module-${uuid}.yaml`,
);
{
const mwc = resource.select(
moduleYaml,
Expand All @@ -145,7 +151,9 @@ describe("build", () => {
expect(webhook.timeoutSeconds).toBe(timeout);
}

const valuesYaml = await resource.oneFromFile(`${outputDir}/${uuid}-chart/values.yaml`);
const valuesYaml = await resource.resourcesFromFile(
`${outputDir}/${uuid}-chart/values.yaml`,
);
expect(valuesYaml.admission.webhookTimeout).toBe(timeout);
});

Expand All @@ -156,7 +164,9 @@ describe("build", () => {
);
};

const moduleYaml = await resource.manyFromFile(`${outputDir}/pepr-module-${uuid}.yaml`);
const moduleYaml = await resource.resourcesFromFile(
`${outputDir}/pepr-module-${uuid}.yaml`,
);
const admission = resource.select(moduleYaml, kind.Deployment, `pepr-${uuid}`);
const admissionSecrets = getDepImgPull(admission);
expect(admissionSecrets).toEqual([withPullSecret]);
Expand All @@ -174,7 +184,7 @@ describe("build", () => {
localPath: `${uuid}-chart`,
};

const zarfYaml = await resource.oneFromFile(`${outputDir}/zarf.yaml`);
const zarfYaml = await resource.resourcesFromFile(`${outputDir}/zarf.yaml`);
const component = zarfYaml.components
.filter((component: { name: string }) => component.name === "module")
.at(0);
Expand Down
8 changes: 4 additions & 4 deletions integration/cli/build.registryinfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("build", () => {
expect(build.stderr.join("").trim()).toBe("");
expect(build.stdout.join("").trim()).toContain("K8s resource for the module saved");

packageJson = await resource.oneFromFile(`${testModule}/package.json`);
packageJson = await resource.resourcesFromFile(`${testModule}/package.json`);
uuid = packageJson.pepr.uuid;
},
time.toMs("1m"),
Expand All @@ -69,7 +69,7 @@ describe("build", () => {
const image = `${registryInfo}/custom-pepr-controller:0.0.0-development`;

{
const moduleYaml = await resource.manyFromFile(
const moduleYaml = await resource.resourcesFromFile(
`${testModule}/dist/pepr-module-${uuid}.yaml`,
);
const admission = resource.select(moduleYaml, kind.Deployment, `pepr-${uuid}`);
Expand All @@ -81,12 +81,12 @@ describe("build", () => {
expect(watcherImage).toBe(image);
}
{
const zarfYaml = await resource.oneFromFile(`${testModule}/dist/zarf.yaml`);
const zarfYaml = await resource.resourcesFromFile(`${testModule}/dist/zarf.yaml`);
const componentImage = zarfYaml.components.at(0).images.at(0);
expect(componentImage).toBe(image);
}
{
const valuesYaml = await resource.oneFromFile(
const valuesYaml = await resource.resourcesFromFile(
`${testModule}/dist/${uuid}-chart/values.yaml`,
);
const admissionImage = valuesYaml.admission.image;
Expand Down
14 changes: 7 additions & 7 deletions integration/helpers/resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ beforeAll(async () => {
await workdir.recreate();
});

describe("oneFromFile", () => {
describe("resourcesFromFile", () => {
it("can load one resource from .json file", async () => {
const oneJson = `${workdir.path()}/one.json`;
await fs.writeFile(
Expand All @@ -28,7 +28,7 @@ describe("oneFromFile", () => {
}
`,
);
const result = await sut.oneFromFile(oneJson);
const result = await sut.resourcesFromFile(oneJson);
expect(result.one).toBe("json");
});

Expand All @@ -41,12 +41,12 @@ describe("oneFromFile", () => {
one: yaml
`,
);
const result = await sut.oneFromFile(oneYaml);
const result = await sut.resourcesFromFile(oneYaml);
expect(result.one).toBe("yaml");
});
});

describe("manyFromFile", () => {
describe("resourcesFromFile", () => {
it("can load many resources from .json file", async () => {
const manyJson = `${workdir.path()}/many.json`;
await fs.writeFile(
Expand All @@ -65,7 +65,7 @@ describe("manyFromFile", () => {
]
`,
);
const result = await sut.manyFromFile(manyJson);
const result = await sut.resourcesFromFile(manyJson);
expect(result.at(0).one).toBe("json");
expect(result.at(1).two).toBe("json");
expect(result.at(2).three).toBe("json");
Expand All @@ -84,7 +84,7 @@ describe("manyFromFile", () => {
three: yaml
`,
);
const result = await sut.manyFromFile(manyYaml);
const result = await sut.resourcesFromFile(manyYaml);
expect(result.at(0).one).toBe("yaml");
expect(result.at(1).two).toBe("yaml");
expect(result.at(2).three).toBe("yaml");
Expand Down Expand Up @@ -115,7 +115,7 @@ describe("select", () => {
fake: news
`,
);
const many = await sut.manyFromFile(manyYaml);
const many = await sut.resourcesFromFile(manyYaml);

const sec = sut.select(many, kind.Secret, "sec");
const cm = sut.select(many, kind.ConfigMap, "cm");
Expand Down
64 changes: 18 additions & 46 deletions integration/helpers/resource.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,37 @@
import { readFile } from "node:fs/promises";
import { kind, KubernetesObject } from "kubernetes-fluent-client";
import { parseDocument, parseAllDocuments } from "yaml";
import { parseAllDocuments } from "yaml";

/**
* Read one resource from file, rehydrated as a JS object
* Read resources from a file and return them as JS objects.
*
* @param path Path to file holding one JSON (*.json) object / YAML (*.yaml) document
* @returns JS object
* @param path Path to the file (supports JSON (*.json) or YAML (*.yaml))
* @returns JS object or array of JS objects.
*/
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export async function oneFromFile(path: string): Promise<any> {
const ext = path.split(".").at(-1);
export async function resourcesFromFile(path: string): Promise<any | any[]> {
const extension = path.split(".").at(-1);

let ret: object;
switch (ext) {
case "json": {
const all = JSON.parse(await readFile(path, { encoding: "utf8" }));
ret = Array.isArray(all) ? all.at(0) : all;
break;
}

case "yaml":
ret = parseDocument(await readFile(path, { encoding: "utf8" })).contents!.toJSON();
break;

default:
throw `oops: don't recognize file of type ".${ext}"`;
}

return ret;
}
let result: object | object[];
const content = await readFile(path, { encoding: "utf8" });

/**
* Read many resources from file, rehydrated as an array of JS objects
*
* @param path Path to file holding an array of JSON (*.json) objects / multiple concatinated YAML (*.yaml) documents
* @returns Array of JS objects
*/
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export async function manyFromFile(path: string): Promise<any[]> {
const ext = path.split(".").at(-1);

let ret: object[];
switch (ext) {
switch (extension) {
case "json": {
const all = JSON.parse(await readFile(path, { encoding: "utf8" }));
ret = Array.isArray(all) ? all : [all];
const parsed = JSON.parse(content);
result = Array.isArray(parsed) ? parsed : [parsed];
break;
}

case "yaml":
ret = parseAllDocuments(await readFile(path, { encoding: "utf8" })).map(yamlDoc =>
yamlDoc.contents!.toJSON(),
);
case "yaml": {
const documents = parseAllDocuments(content).map(doc => doc.contents!.toJSON());
result = documents.length === 1 ? documents[0] : documents;
break;

}
default:
throw `oops: don't recognize file of type ".${ext}"`;
throw new Error(`Unsupported file type ".${extension}"`);
}

return ret;
// If the result is an array with one element, return the single element
return Array.isArray(result) && result.length === 1 ? result[0] : result;
}

/**
Expand Down
Loading