Skip to content

Commit 1684b44

Browse files
committed
update: add mock for listToString yaml custom tag
1 parent 38db2b2 commit 1684b44

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/utils/yaml.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,21 @@ export const includeType = new yaml.Type("!include", {
198198
},
199199
});
200200

201+
/**
202+
* !listToString YAML tag which concatenate each list item into single string value.
203+
* See the tests for example usage.
204+
*/
205+
export const listToStringType = new yaml.Type("!listToString", {
206+
kind: "scalar",
207+
construct(data) {
208+
try {
209+
return readFromYaml(data);
210+
} catch (e) {
211+
return data;
212+
}
213+
},
214+
});
215+
201216
/**
202217
* !flatten YAML tag for flattening arrays
203218
* See the tests for example usage.
@@ -248,6 +263,7 @@ export const concatStringType = new yaml.Type("!concatString", {
248263

249264
export const JsYamlTypes = {
250265
include: includeType,
266+
listToString: listToStringType,
251267
parameter: parameterType,
252268
combine: combineType,
253269
esse: esseType,
@@ -261,6 +277,7 @@ export const JsYamlAllSchemas = yaml.DEFAULT_SCHEMA.extend([
261277
combineType,
262278
esseType,
263279
includeType,
280+
listToStringType,
264281
flattenType,
265282
readFileType,
266283
concatStringType,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# listToString content from another Yaml file
2+
case1:
3+
here: original content
4+
there: !listToString "tests/fixtures/yaml_parameter_ref.yml#/job/workflow"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @ts-nocheck
2+
import { expect } from "chai";
3+
import fs from "fs";
4+
import yaml from "js-yaml";
5+
6+
import { includeType } from "../../src/utils/yaml";
7+
import { YAML_INCLUDE_FILE } from "../enums";
8+
9+
const includeSchema = yaml.DEFAULT_SCHEMA.extend([includeType]);
10+
11+
describe.only("YAML tag: !listToString", () => {
12+
const yamlFixture = fs.readFileSync(YAML_INCLUDE_FILE, "utf8");
13+
14+
it("should correctly concatenate content from list to string", () => {
15+
const parsed = yaml.load(yamlFixture, { schema: includeSchema });
16+
const expected = {
17+
here: "original content",
18+
there: ["run", "stop", "pause"],
19+
};
20+
expect(parsed.case1).to.be.eql(expected);
21+
});
22+
});

0 commit comments

Comments
 (0)