diff --git a/packages/string-templates/manifest.json b/packages/string-templates/manifest.json index ee03d75aa3..9dd8260350 100644 --- a/packages/string-templates/manifest.json +++ b/packages/string-templates/manifest.json @@ -1196,7 +1196,7 @@ "durationType" ], "numArgs": 2, - "example": "{{duration timeLeft \"seconds\"}} -> a few seconds", + "example": "{{duration 8 \"seconds\"}} -> a few seconds", "description": "
Produce a humanized duration left/until given an amount of time and the type of time measurement.
\n" } } diff --git a/packages/string-templates/scripts/gen-collection-info.js b/packages/string-templates/scripts/gen-collection-info.js index 815f3b607f..b487c4dde4 100644 --- a/packages/string-templates/scripts/gen-collection-info.js +++ b/packages/string-templates/scripts/gen-collection-info.js @@ -36,7 +36,7 @@ const ADDED_HELPERS = { duration: { args: ["time", "durationType"], numArgs: 2, - example: '{{duration timeLeft "seconds"}} -> a few seconds', + example: '{{duration 8 "seconds"}} -> a few seconds', description: "Produce a humanized duration left/until given an amount of time and the type of time measurement.", }, diff --git a/packages/string-templates/test/manifest.spec.js b/packages/string-templates/test/manifest.spec.js new file mode 100644 index 0000000000..506f2eb6f7 --- /dev/null +++ b/packages/string-templates/test/manifest.spec.js @@ -0,0 +1,96 @@ +jest.mock("@budibase/handlebars-helpers/lib/math", () => { + const actual = jest.requireActual("@budibase/handlebars-helpers/lib/math") + + return { + ...actual, + random: () => 10, + } +}) +jest.mock("@budibase/handlebars-helpers/lib/uuid", () => { + const actual = jest.requireActual("@budibase/handlebars-helpers/lib/uuid") + + return { + ...actual, + uuid: () => "f34ebc66-93bd-4f7c-b79b-92b5569138bc", + } +}) + +const fs = require("fs") +const { processString } = require("../src/index.cjs") + +const tk = require("timekeeper") +tk.freeze("2021-01-21T12:00:00") + +const manifest = JSON.parse( + fs.readFileSync(require.resolve("../manifest.json"), "utf8") +) + +const collections = Object.keys(manifest) +const examples = collections.reduce((acc, collection) => { + const functions = Object.keys(manifest[collection]).filter( + fnc => manifest[collection][fnc].example + ) + if (functions.length) { + acc[collection] = functions + } + return acc +}, {}) + +function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string +} + +function tryParseJson(str) { + if (typeof str !== "string") { + return + } + + try { + return JSON.parse(str.replace(/\'/g, '"')) + } catch (e) { + return + } +} + +describe("manifest", () => { + describe("examples are valid", () => { + describe.each(Object.keys(examples))("%s", collection => { + it.each(examples[collection])("%s", async func => { + const example = manifest[collection][func].example + + let [hbs, js] = example.split("->").map(x => x.trim()) + + const context = { + double: i => i * 2, + isString: x => typeof x === "string", + } + + const arrays = hbs.match(/\[[^/\]]+\]/) + arrays?.forEach((arrayString, i) => { + hbs = hbs.replace(new RegExp(escapeRegExp(arrayString)), `array${i}`) + context[`array${i}`] = JSON.parse(arrayString.replace(/\'/g, '"')) + }) + + if (js === undefined) { + // The function has no return value + return + } + + let result = await processString(hbs, context) + // Trim 's + js = js.replace(/^\'|\'$/g, "") + if ((parsedExpected = tryParseJson(js))) { + if (Array.isArray(parsedExpected)) { + if (typeof parsedExpected[0] === "object") { + js = JSON.stringify(parsedExpected) + } else { + js = parsedExpected.join(",") + } + } + } + result = result.replace(/ /g, " ") + expect(result).toEqual(js) + }) + }) + }) +}) diff --git a/scripts/build-single-image.sh b/scripts/build-single-image.sh index ba64d6121b..16b668e033 100755 --- a/scripts/build-single-image.sh +++ b/scripts/build-single-image.sh @@ -1,4 +1,4 @@ #!/bin/bash yarn build --scope @budibase/server --scope @budibase/worker version=$(./scripts/getCurrentVersion.sh) -docker build -f hosting/single/Dockerfile -t budibase:latest --build-arg BUDIBASE_VERSION=$version . +docker build -f hosting/single/Dockerfile -t budibase:latest --build-arg BUDIBASE_VERSION=$version --build-arg TARGETBUILD=single .