Skip to content

Commit

Permalink
Added tests for variables. Simplifying variable regex.
Browse files Browse the repository at this point in the history
  • Loading branch information
nataliecarey committed Aug 17, 2023
1 parent 0cdc064 commit 1777523
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/plugins/plugin-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`))
})
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions lib/plugins/plugin-validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
})
})
})

0 comments on commit 1777523

Please sign in to comment.