Adding jest test cases to string templating library.

This commit is contained in:
mike12345567 2021-01-19 17:29:38 +00:00
parent 759a106d2d
commit e8ef03bb1c
15 changed files with 3789 additions and 51 deletions

View File

@ -66,7 +66,6 @@
"electron-util": "^0.14.2", "electron-util": "^0.14.2",
"fix-path": "^3.0.0", "fix-path": "^3.0.0",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"handlebars": "^4.7.6",
"jimp": "^0.16.1", "jimp": "^0.16.1",
"joi": "^17.2.1", "joi": "^17.2.1",
"jsonschema": "^1.4.0", "jsonschema": "^1.4.0",

View File

@ -31,7 +31,7 @@ const {
createLoginScreen, createLoginScreen,
} = require("../../constants/screens") } = require("../../constants/screens")
const { cloneDeep } = require("lodash/fp") const { cloneDeep } = require("lodash/fp")
const { objectHandlebars } = require("../../utilities/handlebars") const { objectTemplate } = require("../../utilities/stringTemplating")
const { USERS_TABLE_SCHEMA } = require("../../constants") const { USERS_TABLE_SCHEMA } = require("../../constants")
const APP_PREFIX = DocumentTypes.APP + SEPARATOR const APP_PREFIX = DocumentTypes.APP + SEPARATOR
@ -213,7 +213,7 @@ const createEmptyAppPackage = async (ctx, app) => {
let screensAndLayouts = [] let screensAndLayouts = []
for (let layout of BASE_LAYOUTS) { for (let layout of BASE_LAYOUTS) {
const cloned = cloneDeep(layout) const cloned = cloneDeep(layout)
screensAndLayouts.push(objectHandlebars(cloned, app)) screensAndLayouts.push(objectTemplate(cloned, app))
} }
const homeScreen = createHomeScreen(app) const homeScreen = createHomeScreen(app)

View File

@ -7,7 +7,7 @@ const fs = require("fs-extra")
const uuid = require("uuid") const uuid = require("uuid")
const AWS = require("aws-sdk") const AWS = require("aws-sdk")
const { prepareUpload } = require("../deploy/utils") const { prepareUpload } = require("../deploy/utils")
const { stringHandlebars } = require("../../../utilities/handlebars") const { stringTemplate } = require("../../../utilities/stringTemplating")
const { const {
budibaseAppsDir, budibaseAppsDir,
budibaseTempDir, budibaseTempDir,
@ -161,7 +161,7 @@ exports.serveApp = async function(ctx) {
}) })
const appHbs = fs.readFileSync(`${__dirname}/templates/app.hbs`, "utf8") const appHbs = fs.readFileSync(`${__dirname}/templates/app.hbs`, "utf8")
ctx.body = stringHandlebars(appHbs, { ctx.body = stringTemplate(appHbs, {
head, head,
body: html, body: html,
style: css.code, style: css.code,

View File

@ -1,10 +1,11 @@
const CouchDB = require("../db") const CouchDB = require("../db")
/** /**
* When values are input to the system generally they will be of type string as this is required for handlebars. This can * When values are input to the system generally they will be of type string as this is required for template strings.
* generate some odd scenarios as the Schema of the automation requires a number but the builder might supply a string * This can generate some odd scenarios as the Schema of the automation requires a number but the builder might supply
* with handlebars syntax to get the number from the rest of the context. To support this the server has to make sure that * a string with template syntax to get the number from the rest of the context. To support this the server has to
* the post handlebars statement can be cast into the correct type, this function does this for numbers and booleans. * make sure that the post template statement can be cast into the correct type, this function does this for numbers
* and booleans.
* *
* @param {object} inputs An object of inputs, please note this will not recurse down into any objects within, it simply * @param {object} inputs An object of inputs, please note this will not recurse down into any objects within, it simply
* cleanses the top level inputs, however it can be used by recursively calling it deeper into the object structures if * cleanses the top level inputs, however it can be used by recursively calling it deeper into the object structures if
@ -54,7 +55,7 @@ module.exports.cleanInputValues = (inputs, schema) => {
* *
* @param {string} appId The instance which the Table/Table is contained under. * @param {string} appId The instance which the Table/Table is contained under.
* @param {string} tableId The ID of the Table/Table which the schema is to be retrieved for. * @param {string} tableId The ID of the Table/Table which the schema is to be retrieved for.
* @param {object} row The input row structure which requires clean-up after having been through handlebars statements. * @param {object} row The input row structure which requires clean-up after having been through template statements.
* @returns {Promise<Object>} The cleaned up rows object, will should now have all the required primitive types. * @returns {Promise<Object>} The cleaned up rows object, will should now have all the required primitive types.
*/ */
module.exports.cleanUpRow = async (appId, tableId, row) => { module.exports.cleanUpRow = async (appId, tableId, row) => {
@ -66,11 +67,11 @@ module.exports.cleanUpRow = async (appId, tableId, row) => {
/** /**
* A utility function for the cleanUpRow, which can be used if only the row ID is known (not the table ID) to clean * A utility function for the cleanUpRow, which can be used if only the row ID is known (not the table ID) to clean
* up a row after handlebars statements have been replaced. This is specifically useful for the update row action. * up a row after template statements have been replaced. This is specifically useful for the update row action.
* *
* @param {string} appId The instance which the Table/Table is contained under. * @param {string} appId The instance which the Table/Table is contained under.
* @param {string} rowId The ID of the row from which the tableId will be extracted, to get the Table/Table schema. * @param {string} rowId The ID of the row from which the tableId will be extracted, to get the Table/Table schema.
* @param {object} row The input row structure which requires clean-up after having been through handlebars statements. * @param {object} row The input row structure which requires clean-up after having been through template statements.
* @returns {Promise<Object>} The cleaned up rows object, which will now have all the required primitive types. * @returns {Promise<Object>} The cleaned up rows object, which will now have all the required primitive types.
*/ */
module.exports.cleanUpRowById = async (appId, rowId, row) => { module.exports.cleanUpRowById = async (appId, rowId, row) => {

View File

@ -1,13 +1,8 @@
const handlebars = require("handlebars")
const actions = require("./actions") const actions = require("./actions")
const logic = require("./logic") const logic = require("./logic")
const automationUtils = require("./automationUtils") const automationUtils = require("./automationUtils")
const AutomationEmitter = require("../events/AutomationEmitter") const AutomationEmitter = require("../events/AutomationEmitter")
const { objectHandlebars } = require("../utilities/handlebars") const { objectTemplate } = require("../utilities/stringTemplating")
handlebars.registerHelper("object", value => {
return new handlebars.SafeString(JSON.stringify(value))
})
const FILTER_STEP_ID = logic.BUILTIN_DEFINITIONS.FILTER.stepId const FILTER_STEP_ID = logic.BUILTIN_DEFINITIONS.FILTER.stepId
@ -24,7 +19,7 @@ class Orchestrator {
// remove from context // remove from context
delete triggerOutput.appId delete triggerOutput.appId
delete triggerOutput.metadata delete triggerOutput.metadata
// step zero is never used as the handlebars is zero indexed for customer facing // step zero is never used as the template string is zero indexed for customer facing
this._context = { steps: [{}], trigger: triggerOutput } this._context = { steps: [{}], trigger: triggerOutput }
this._automation = automation this._automation = automation
// create an emitter which has the chain count for this automation run in it, so it can block // create an emitter which has the chain count for this automation run in it, so it can block
@ -49,7 +44,7 @@ class Orchestrator {
let automation = this._automation let automation = this._automation
for (let step of automation.definition.steps) { for (let step of automation.definition.steps) {
let stepFn = await this.getStepFunctionality(step.type, step.stepId) let stepFn = await this.getStepFunctionality(step.type, step.stepId)
step.inputs = objectHandlebars(step.inputs, this._context) step.inputs = objectTemplate(step.inputs, this._context)
step.inputs = automationUtils.cleanInputValues( step.inputs = automationUtils.cleanInputValues(
step.inputs, step.inputs,
step.schema.inputs step.schema.inputs

View File

@ -9,7 +9,7 @@ const { rowEmission, tableEmission } = require("./utils")
/** /**
* Extending the standard emitter to some syntactic sugar and standardisation to the emitted event. * Extending the standard emitter to some syntactic sugar and standardisation to the emitted event.
* This is specifically quite important for handlebars used in automations. * This is specifically quite important for template strings used in automations.
*/ */
class BudibaseEmitter extends EventEmitter { class BudibaseEmitter extends EventEmitter {
emitRow(eventName, appId, row, table = null) { emitRow(eventName, appId, row, table = null) {

View File

@ -1,4 +0,0 @@
const stringTemplates = require("@budibase/string-templates")
exports.objectHandlebars = stringTemplates.processObject
exports.stringHandlebars = stringTemplates.processString

View File

@ -1,6 +1,6 @@
const { existsSync, readFile, writeFile, ensureDir } = require("fs-extra") const { existsSync, readFile, writeFile, ensureDir } = require("fs-extra")
const { join, resolve } = require("./centralPath") const { join, resolve } = require("./centralPath")
const handlebars = require("handlebars") const { stringTemplate } = require("./stringTemplating")
const uuid = require("uuid") const uuid = require("uuid")
module.exports = async opts => { module.exports = async opts => {
@ -31,8 +31,7 @@ const createDevEnvFile = async opts => {
} }
) )
opts.cookieKey1 = opts.cookieKey1 || uuid.v4() opts.cookieKey1 = opts.cookieKey1 || uuid.v4()
const envTemplate = handlebars.compile(template) const config = stringTemplate(template, opts)
const config = envTemplate(opts)
await writeFile(destConfigFile, config, { flag: "w+" }) await writeFile(destConfigFile, config, { flag: "w+" })
} }
} }

View File

@ -0,0 +1,4 @@
const stringTemplates = require("@budibase/string-templates")
exports.objectTemplate = stringTemplates.processObject
exports.stringTemplate = stringTemplates.processString

View File

@ -3522,18 +3522,6 @@ growly@^1.3.0:
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081" resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
integrity sha1-8QdIy+dq+WS3yWyTxrzCivEgwIE= integrity sha1-8QdIy+dq+WS3yWyTxrzCivEgwIE=
handlebars@^4.7.6:
version "4.7.6"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.6.tgz#d4c05c1baf90e9945f77aa68a7a219aa4a7df74e"
integrity sha512-1f2BACcBfiwAfStCKZNrUCgqNZkGsAT7UM3kkYtXuLo0KnaVfjKOyf7PRzB6++aK9STyT1Pd2ZCPe3EGOXleXA==
dependencies:
minimist "^1.2.5"
neo-async "^2.6.0"
source-map "^0.6.1"
wordwrap "^1.0.0"
optionalDependencies:
uglify-js "^3.1.4"
har-schema@^2.0.0: har-schema@^2.0.0:
version "2.0.0" version "2.0.0"
resolved "https://registry.yarnpkg.com/har-schema/-/har-schema-2.0.0.tgz#a94c2224ebcac04782a0d9035521f24735b7ec92" resolved "https://registry.yarnpkg.com/har-schema/-/har-schema-2.0.0.tgz#a94c2224ebcac04782a0d9035521f24735b7ec92"

View File

@ -0,0 +1,194 @@
/*
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/en/configuration.html
*/
module.exports = {
// All imported modules in your tests should be mocked automatically
// automock: false,
// Stop running tests after `n` failures
// bail: 0,
// The directory where Jest should store its cached dependency information
// cacheDirectory: "/tmp/jest_rs",
// Automatically clear mock calls and instances between every test
clearMocks: true,
// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,
// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,
// The directory where Jest should output its coverage files
coverageDirectory: "coverage",
// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],
// Indicates which provider should be used to instrument code for coverage
coverageProvider: "v8",
// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],
// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,
// A path to a custom dependency extractor
// dependencyExtractor: undefined,
// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,
// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],
// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,
// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,
// A set of global variables that need to be available in all test environments
// globals: {},
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",
// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],
// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "json",
// "jsx",
// "ts",
// "tsx",
// "node"
// ],
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
// Activates notifications for test results
// notify: false,
// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",
// A preset that is used as a base for Jest's configuration
// preset: undefined,
// Run tests from one or more projects
// projects: undefined,
// Use this configuration option to add custom reporters to Jest
// reporters: undefined,
// Automatically reset mock state between every test
// resetMocks: false,
// Reset the module registry before running each individual test
// resetModules: false,
// A path to a custom resolver
// resolver: undefined,
// Automatically restore mock state between every test
// restoreMocks: false,
// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,
// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],
// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",
// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],
// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],
// The test environment that will be used for testing
testEnvironment: "node",
// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},
// Adds a location field to test results
// testLocationInResults: false,
// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// "/node_modules/"
// ],
// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],
// This option allows the use of a custom results processor
// testResultsProcessor: undefined,
// This option allows use of a custom test runner
// testRunner: "jasmine2",
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
// testURL: "http://localhost",
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
// timers: "real",
// A map from regular expressions to paths to transformers
// transform: undefined,
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// "/node_modules/",
// "\\.pnp\\.[^\\/]+$"
// ],
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,
// Indicates whether each individual test should be reported during the run
// verbose: undefined,
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],
// Whether to use watchman for file crawling
// watchman: true,
};

View File

@ -9,17 +9,19 @@
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
"scripts": { "scripts": {
"build": "rollup -c", "build": "rollup -c",
"dev:builder": "tsc && rollup -cw" "dev:builder": "tsc && rollup -cw",
"test": "jest"
}, },
"dependencies": { "dependencies": {
"handlebars": "^4.7.6" "handlebars": "^4.7.6"
}, },
"devDependencies": { "devDependencies": {
"typescript": "^4.1.3",
"rollup": "^2.36.2", "rollup": "^2.36.2",
"rollup-plugin-commonjs": "^10.1.0", "rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-builtins": "^2.1.2", "rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.4.0", "rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-node-resolve": "^5.2.0" "rollup-plugin-node-resolve": "^5.2.0",
"typescript": "^4.1.3",
"jest": "^26.6.3"
} }
} }

View File

@ -82,6 +82,9 @@ module.exports.processObject = (object, context) => {
* @returns {string} The enriched string, all templates should have been replaced if they can be. * @returns {string} The enriched string, all templates should have been replaced if they can be.
*/ */
module.exports.processString = (string, context) => { module.exports.processString = (string, context) => {
if (typeof string !== "string") {
throw "Cannot process non-string types."
}
let template let template
try { try {
string = cleanHandlebars(string) string = cleanHandlebars(string)

View File

@ -0,0 +1,69 @@
const {
processObject,
processString,
} = require("../src/index")
describe("Test that the string processing works correctly", () => {
it("should process a basic template string", () => {
const output = processString("templating is {{ adjective }}", {
adjective: "easy"
})
expect(output).toBe("templating is easy")
})
it("should fail gracefully when wrong type passed in", () => {
let error = null
try {
processString(null, null)
} catch (err) {
error = err
}
expect(error).not.toBeNull()
})
})
describe("Test that the object processing works correctly", () => {
it("should be able to process an object with some template strings", () => {
const output = processObject({
first: "thing is {{ adjective }}",
second: "thing is bad",
third: "we are {{ adjective }} {{ noun }}",
}, {
adjective: "easy",
noun: "people",
})
expect(output.first).toBe("thing is easy")
expect(output.second).toBe("thing is bad")
expect(output.third).toBe("we are easy people")
})
it("should be able to handle arrays of string templates", () => {
const output = processObject(["first {{ noun }}", "second {{ noun }}"], {
noun: "person"
})
expect(output[0]).toBe("first person")
expect(output[1]).toBe("second person")
})
it("should fail gracefully when object passed in has cycles", () => {
let error = null
try {
const innerObj = { a: "thing {{ a }}" }
innerObj.b = innerObj
processObject(innerObj, { a: 1 })
} catch (err) {
error = err
}
expect(error).not.toBeNull()
})
it("should fail gracefully when wrong type is passed in", () => {
let error = null
try {
processObject(null, null)
} catch (err) {
error = err
}
expect(error).not.toBeNull()
})
})

File diff suppressed because it is too large Load Diff