From 1777523ed995832412f53c3b661bc526176a1649 Mon Sep 17 00:00:00 2001 From: Natalie Carey Date: Thu, 17 Aug 2023 13:48:07 +0100 Subject: [PATCH] Added tests for variables. Simplifying variable regex. --- lib/plugins/plugin-validator.js | 9 ++++++--- lib/plugins/plugin-validator.spec.js | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/plugins/plugin-validator.js b/lib/plugins/plugin-validator.js index f4ea5da15a..2d43c17ef8 100644 --- a/lib/plugins/plugin-validator.js +++ b/lib/plugins/plugin-validator.js @@ -110,7 +110,7 @@ function validateMetaUrls (metaUrls) { errors.push(`meta.urls.${key} doesn't appear to be a public URL`) } - const unknownVariables = (url.match(/\{\{(\w+)\}\}/g) || []).map(x => `${x}`).filter(variable => !allowedVariables.includes(variable)) + const unknownVariables = (url.match(/{{(\w+)}}/g) || []).filter(variable => !allowedVariables.includes(variable)) unknownVariables.forEach(variable => errors.push(`The URL ${keyPath}${key} contains an unknown variable ${variable}`)) }) @@ -213,14 +213,17 @@ async function validatePlugin (executionPath) { // Check if the json has contents let isConfigEmpty = false - if (JSON.stringify(pluginConfig) === '{}') { + const configClone = Object.assign({}, pluginConfig) + delete configClone.meta + if (JSON.stringify(configClone) === '{}') { isConfigEmpty = true } // Continue with the validation if there are no syntax errors in the config if (pluginConfig) { if (isConfigEmpty) { - errors.push('There are no contents in your govuk-prototype.config file!') + const caveat = pluginConfig.meta ? ' other than the metadata' : '' + errors.push(`There are no contents in your govuk-prototype.config file${caveat}!`) } else { // Perform the rest of the checks if the config file has contents const validKeysPluginConfig = validateConfigKeys(pluginConfig) diff --git a/lib/plugins/plugin-validator.spec.js b/lib/plugins/plugin-validator.spec.js index b3d92d9d2f..57f07ce702 100644 --- a/lib/plugins/plugin-validator.spec.js +++ b/lib/plugins/plugin-validator.spec.js @@ -125,5 +125,32 @@ describe('plugin-validator', () => { }) expect(getErrors()).toEqual(['The meta.description must be a string if entered']) }) + it('should allow known variables in URLs', () => { + validateMeta({ + urls: { + documentation: 'https://example.com/{{version}}?kitVersion={{kitVersion}}' + } + }) + expect(getErrors()).toEqual([]) + }) + it('should fail if unknown variables are present', () => { + validateMeta({ + urls: { + documentation: 'https://example.com/{{versions}}?kitVersion={{kitVersions}}' + } + }) + expect(getErrors()).toEqual([ + 'The URL meta.urls.documentation contains an unknown variable {{versions}}', + 'The URL meta.urls.documentation contains an unknown variable {{kitVersions}}' + ]) + }) + it('should allow spaces in variables', () => { + validateMeta({ + urls: { + documentation: 'https://example.com/{{ version }}?kitVersion={{ kitVersions }}' + } + }) + expect(getErrors()).toEqual([]) + }) }) })