Skip to content

Commit

Permalink
refactor(sdl): improve test yml interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrishajev committed May 9, 2024
1 parent 7540f0f commit 571960f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
32 changes: 5 additions & 27 deletions src/sdl/SDL/SDL.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from "@faker-js/faker";
import template from "lodash/template";
import omit from "lodash/omit";

import { readYml } from "../../../test/read-yml";
import { readYml, toYmlFragment } from "../../../test/yml";
import { SdlValidationError } from "../../error";
import { SDL } from "./SDL";
import { v2ServiceImageCredentials } from "../types";
Expand All @@ -18,7 +18,7 @@ describe("SDL", () => {
password: faker.internet.password()
};
const yml = createYML({
credentials: createCreds(credentials)
credentials: toYmlFragment({ credentials }, { nestingLevel: 2 })
});
const sdl = SDL.fromString(yml, "beta3", "sandbox");

Expand Down Expand Up @@ -53,7 +53,7 @@ describe("SDL", () => {

it.each(fields)('should throw an error when credentials are missing "%s"', field => {
const yml = createYML({
credentials: createCreds(omit(credentials, field))
credentials: toYmlFragment({ credentials: omit(credentials, field) }, { nestingLevel: 2 })
});

expect(() => {
Expand All @@ -64,7 +64,7 @@ describe("SDL", () => {
it.each(fields)('should throw an error when credentials "%s" is empty', field => {
credentials[field] = "";
const yml = createYML({
credentials: createCreds(omit(credentials, field))
credentials: toYmlFragment({ credentials: omit(credentials, field) }, { nestingLevel: 2 })
});

expect(() => {
Expand All @@ -75,7 +75,7 @@ describe("SDL", () => {
it.each(fields)('should throw an error when credentials "%s" contains spaces only', field => {
credentials[field] = " ";
const yml = createYML({
credentials: createCreds(omit(credentials, field))
credentials: toYmlFragment({ credentials: omit(credentials, field) }, { nestingLevel: 2 })
});

expect(() => {
Expand All @@ -85,25 +85,3 @@ describe("SDL", () => {
});
});
});

function createCreds({ host, username, password }: { host?: string; username?: string; password?: string }) {
let creds = "";

if (host) {
creds += ` host: "${host}"\n`;
}

if (username) {
creds += ` username: "${username}"\n`;
}

if (password) {
creds += ` password: "${password}"\n`;
}

if (creds) {
creds = ` credentials:\n${creds}`;
}

return creds;
}
6 changes: 0 additions & 6 deletions test/read-yml.ts

This file was deleted.

25 changes: 25 additions & 0 deletions test/yml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from "fs";
import path from "path";
import { dump } from "js-yaml";

export const readYml = (name: string): string => {
return fs.readFileSync(path.resolve(__dirname, `./fixtures/${name}.yml`), "utf-8");
};

type YmlInputObject = {
[key: string]: string | number | boolean | null | YmlInputObject;
};

export const toYmlFragment = (object: YmlInputObject, options?: { nestingLevel: number }): string => {
const yamlString = dump(object);

if (!options) {
return yamlString;
}

const indentation = " ".repeat(options.nestingLevel);
return yamlString
.split("\n")
.map(line => indentation + line)
.join("\n");
};

0 comments on commit 571960f

Please sign in to comment.