Skip to content
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
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions src/utils/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,70 @@ export const includeType = new yaml.Type("!include", {
},
});

/**
* !flatten YAML tag for flattening arrays
* See the tests for example usage.
*/
export const flattenType = new yaml.Type("!flatten", {
kind: "sequence",
construct(data) {
try {
return data.flat();
} catch (e) {
return data;
}
},
});

/**
* !readFile YAML tag for including file contents as a string.
* See the tests for example usage.
*/
export const readFileType = new yaml.Type("!readFile", {
kind: "scalar",
construct(data) {
try {
return fs.readFileSync(path.resolve(data), "utf8");
} catch (e) {
return data;
}
},
});

/**
* !concatString YAML tag for concatenating strings.
* See the tests for example usage.
*/
export const concatStringType = new yaml.Type("!concatString", {
kind: "sequence",
resolve(data) {
return data.every((d) => lodash.isString(d));
},
construct(data) {
try {
return "".concat(...data);
} catch (e) {
return data;
}
},
});

export const JsYamlTypes = {
include: includeType,
parameter: parameterType,
combine: combineType,
esse: esseType,
flatten: flattenType,
readFile: readFileType,
concatString: concatStringType,
};

export const JsYamlAllSchemas = yaml.DEFAULT_SCHEMA.extend([
parameterType,
combineType,
esseType,
includeType,
flattenType,
readFileType,
concatStringType,
]);
3 changes: 3 additions & 0 deletions tests/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ export const YAML_COMBINE_FILE = path.resolve(FIXTURES_DIR, "yaml_combine_tag.ym
export const YAML_PARAMETER_FILE = path.resolve(FIXTURES_DIR, "yaml_parameter_tag.yml");
export const YAML_ESSE_FILE = path.resolve(FIXTURES_DIR, "yaml_esse_tag.yml");
export const YAML_INCLUDE_FILE = path.resolve(FIXTURES_DIR, "yaml_include_tag.yml");
export const YAML_FLATTEN_FILE = path.resolve(FIXTURES_DIR, "yaml_flatten_tag.yml");
export const YAML_READFILE_FILE = path.resolve(FIXTURES_DIR, "yaml_readFile_tag.yml");
export const YAML_CONCAT_STRING_FILE = path.resolve(FIXTURES_DIR, "yaml_concatString_tag.yml");
2 changes: 2 additions & 0 deletions tests/fixtures/test_content.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Example text
with linebreaks.
10 changes: 10 additions & 0 deletions tests/fixtures/yaml_concatString_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# concatenate three strings
case1: !concatString
- "Hello "
- world
- "!"
# return a string if there is only a single item
case2: !concatString
- Hello
# return an empty string if the array is empty
case3: !concatString []
5 changes: 5 additions & 0 deletions tests/fixtures/yaml_flatten_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# convert an array of arrays into a flattened array
case1: !flatten
- [a, b, c]
- d
- [e, f]
2 changes: 2 additions & 0 deletions tests/fixtures/yaml_readFile_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# read contents of a file into a string
case1: !readFile "tests/fixtures/test_content.txt"
7 changes: 6 additions & 1 deletion tests/utils/yaml.combine.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { YAML_COMBINE_FILE } from "../enums";
const combineSchema = yaml.DEFAULT_SCHEMA.extend([combineType, esseType]);

describe("YAML tag: !combine", () => {
const yamlFixture = fs.readFileSync(YAML_COMBINE_FILE, "utf8");
let yamlFixture;

before(() => {
yamlFixture = fs.readFileSync(YAML_COMBINE_FILE, "utf8");
});

it("should correctly parse a custom !combine tag with forEach and config keys", () => {
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
const expectedResult = [
Expand Down
30 changes: 30 additions & 0 deletions tests/utils/yaml.concatString.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from "chai";
import fs from "fs";
import yaml from "js-yaml";

import { concatStringType } from "../../src/utils/yaml";
import { YAML_CONCAT_STRING_FILE } from "../enums";

const concatStringSchema = yaml.DEFAULT_SCHEMA.extend([concatStringType]);

describe("YAML tag: !concatString", () => {
const yamlFixture = fs.readFileSync(YAML_CONCAT_STRING_FILE, "utf8");

it("should concatenate two strings", () => {
const parsed = yaml.load(yamlFixture, { schema: concatStringSchema });
const expected = "Hello world!";
expect(parsed.case1).to.be.eql(expected);
});

it("should return a string if there is only a single item", () => {
const parsed = yaml.load(yamlFixture, { schema: concatStringSchema });
const expected = "Hello";
expect(parsed.case2).to.be.eql(expected);
});

it("should return an empty string if the array is empty", () => {
const parsed = yaml.load(yamlFixture, { schema: concatStringSchema });
const expected = "";
expect(parsed.case3).to.be.eql(expected);
});
});
18 changes: 18 additions & 0 deletions tests/utils/yaml.flatten.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from "chai";
import fs from "fs";
import yaml from "js-yaml";

import { flattenType } from "../../src/utils/yaml";
import { YAML_FLATTEN_FILE } from "../enums";

const flattenSchema = yaml.DEFAULT_SCHEMA.extend([flattenType]);

describe("YAML tag: !flatten", () => {
const yamlFixture = fs.readFileSync(YAML_FLATTEN_FILE, "utf8");

it("should convert an array of arrays into a flattened array", () => {
const parsed = yaml.load(yamlFixture, { schema: flattenSchema });
const expected = ["a", "b", "c", "d", "e", "f"];
expect(parsed.case1).to.be.eql(expected);
});
});
19 changes: 19 additions & 0 deletions tests/utils/yaml.readFile.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect } from "chai";
import fs from "fs";
import yaml from "js-yaml";

import { readFileType } from "../../src/utils/yaml";
import { YAML_READFILE_FILE } from "../enums";

const readFileSchema = yaml.DEFAULT_SCHEMA.extend([readFileType]);

describe("YAML tag: !readFile", () => {
const yamlFixture = fs.readFileSync(YAML_READFILE_FILE, "utf8");

it("should read contents of a file into a string", () => {
const parsed = yaml.load(yamlFixture, { schema: readFileSchema });
const expected = "Example text\nwith linebreaks.\n";
expect(parsed.case1).to.be.a("string");
expect(parsed.case1).to.be.eql(expected);
});
});