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 8, 2024
1 parent 7540f0f commit 56059cb
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions src/sdl/SDL/SDL.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { faker } from "@faker-js/faker";
import template from "lodash/template";
import omit from "lodash/omit";
import { dump } from "js-yaml";

import { readYml } from "../../../test/read-yml";
import { SdlValidationError } from "../../error";
Expand All @@ -18,7 +19,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 +54,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 +65,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 +76,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 @@ -86,24 +87,20 @@ describe("SDL", () => {
});
});

function createCreds({ host, username, password }: { host?: string; username?: string; password?: string }) {
let creds = "";
type YmlInputObject = {
[key: string]: string | number | boolean | null | YmlInputObject;
};

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

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

if (password) {
creds += ` password: "${password}"\n`;
}
function toYmlFragment(object: YmlInputObject, options?: { nestingLevel: number }): string {
const yamlString = dump(object);

if (creds) {
creds = ` credentials:\n${creds}`;
if (!options) {
return yamlString;
}

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

0 comments on commit 56059cb

Please sign in to comment.