fix(build): Allow _ref path argument to be a _var.

This commit is contained in:
SamTolmay 2021-02-08 16:57:17 +02:00
parent dafa6b3b31
commit a8bd287176
2 changed files with 100 additions and 8 deletions

View File

@ -23,8 +23,13 @@ import YAML from 'js-yaml';
import { v1 as uuid } from 'uuid';
function getRefPath(refDefinition) {
if (type.isObject(refDefinition) && refDefinition.path) {
return refDefinition.path;
if (type.isObject(refDefinition)) {
if (refDefinition.path) {
return refDefinition.path;
}
if (refDefinition._var) {
return refDefinition;
}
}
if (type.isString(refDefinition)) {
return refDefinition;
@ -144,16 +149,16 @@ class RefBuilder {
for (const refDef of foundRefs.values()) {
if (refDef.path === null) {
throw new Error(
`Invalid _ref definition ${JSON.stringify(refDef.original)} at file ${path}`
`Invalid _ref definition ${JSON.stringify({ _ref: refDef.original })} in file ${path}`
);
}
const parsedVars = JSON.parse(
JSON.stringify(refDef.vars),
const { path: parsedPath, vars: parsedVars } = JSON.parse(
JSON.stringify(refDef),
refReviver.bind({ parsedFiles, vars })
);
// eslint-disable-next-line no-await-in-loop
let parsedFile = await this.recursiveParseFile({
path: refDef.path,
path: parsedPath,
// Parse vars before passing down to parse new file
vars: parsedVars,
count: count + 1,

View File

@ -263,7 +263,7 @@ test('buildRefs null ref definition', async () => {
];
mockConfigLoader.mockImplementation(configLoaderMockImplementation(files));
await expect(buildRefs({ context })).rejects.toThrow(
'Invalid _ref definition null at file lowdefy.yaml'
'Invalid _ref definition {"_ref":null} in file lowdefy.yaml'
);
});
@ -278,7 +278,22 @@ test('buildRefs invalid ref definition', async () => {
];
mockConfigLoader.mockImplementation(configLoaderMockImplementation(files));
await expect(buildRefs({ context })).rejects.toThrow(
'Invalid _ref definition 1 at file lowdefy.yaml'
'Invalid _ref definition {"_ref":1} in file lowdefy.yaml'
);
});
test('buildRefs invalid ref definition 2', async () => {
const files = [
{
path: 'lowdefy.yaml',
content: {
invalid: { _ref: { a: 'b' } },
},
},
];
mockConfigLoader.mockImplementation(configLoaderMockImplementation(files));
await expect(buildRefs({ context })).rejects.toThrow(
'Invalid _ref definition {"_ref":{"a":"b"}} in file lowdefy.yaml'
);
});
@ -733,3 +748,75 @@ test('buildRefs _var receives invalid type', async () => {
'"_var" operator takes a string or object with name field as arguments. Received "{"_var":[1]}"'
);
});
test('buildRefs _ref path is a var, shorthand path', async () => {
const files = [
{
path: 'lowdefy.yaml',
content: {
_ref: {
path: 'file1.yaml',
vars: {
filePath: 'file2.md',
},
},
},
},
{
path: 'file1.yaml',
content: {
text: {
_ref: {
_var: 'filePath',
},
},
},
},
{
path: 'file2.md',
content: 'Hello',
},
];
mockConfigLoader.mockImplementation(configLoaderMockImplementation(files));
const res = await buildRefs({ context });
expect(res).toEqual({
text: 'Hello',
});
});
test('buildRefs _ref path is a var', async () => {
const files = [
{
path: 'lowdefy.yaml',
content: {
_ref: {
path: 'file1.yaml',
vars: {
filePath: 'file2.md',
},
},
},
},
{
path: 'file1.yaml',
content: {
text: {
_ref: {
path: {
_var: 'filePath',
},
},
},
},
},
{
path: 'file2.md',
content: 'Hello',
},
];
mockConfigLoader.mockImplementation(configLoaderMockImplementation(files));
const res = await buildRefs({ context });
expect(res).toEqual({
text: 'Hello',
});
});