Skip to content

Commit

Permalink
Merge pull request #2236 from alphagov/issue_2235-nunjucksMacros-vali…
Browse files Browse the repository at this point in the history
…dation-error

Issue 2235 nunjucks macros validation error
  • Loading branch information
HannahJMWood authored Jun 28, 2023
2 parents 8bec566 + 87e5480 commit 0f5db6b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"/sass/_step-by-step-navigation-header.scss",
"/sass/_step-by-step-navigation-related.scss"
],
"nunjucksPaths": [
"/macros"
],
"scripts": [
"javascripts/step-by-step-navigation.js",
"javascripts/step-by-step-polyfills.js",
Expand All @@ -23,5 +26,15 @@
"path": "/templates/start-with-step-by-step.html",
"type": "nunjucks"
}
]
],
"nunjucksMacros": [
{
"importFrom": "foo-bar.njk",
"macroName": "fooBar"
},
{
"importFrom": "za-bar.njk",
"macroName": "zaBar"
}
]
}
Empty file.
1 change: 1 addition & 0 deletions __tests__/spec/plugin-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ${ansiColors.red('Error: In section scripts, the path \'javascripts/step-by-step
${ansiColors.red('Error: In section scripts, the path \'javascripts/modules/foo-module-one.js\' does not start with a \'/\'')}
${ansiColors.red('Error: In section templates, the path \'/templates/step-by-step-navigation.html\' does not exist')}
${ansiColors.red('Error: In section templates, the path \'/templates/start-with-step-by-step.html\' does not exist')}
${ansiColors.red('Error: The nunjucks file \'za-bar.njk\' does not exist')}
`)
})
Expand Down
23 changes: 22 additions & 1 deletion lib/plugins/plugin-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ function checkPathExists (executionPath, pathToValidate, key) {
if (!fse.existsSync(absolutePathToValidate)) errors.push(`In section ${key}, the path '${pathToValidate}' does not exist`)
}

function checkNunjucksMacroExists (executionPath, nunjucksFileName, nunjucksPaths) {
// set up a flag for the existance of a nunjucks path
let nunjucksPathExists = false

if (nunjucksPaths === undefined) {
// Check if the nunjucksMacros are at the root level of the project
if (fse.existsSync(path.join(executionPath, nunjucksFileName))) nunjucksPathExists = true
} else {
// Combine the file path name for each nunjucks path and check if any one of them exists
for (const nunjucksPath of nunjucksPaths) {
const pathToCheck = path.join(executionPath, nunjucksPath, nunjucksFileName)
if (fse.existsSync(pathToCheck)) {
nunjucksPathExists = true
break
}
}
}

if (!nunjucksPathExists) errors.push(`The nunjucks file '${nunjucksFileName}' does not exist`)
}

function validateConfigKeys (pluginConfig) {
console.log('Config file exists, validating contents.')
const knownKeys = getKnownKeys()
Expand Down Expand Up @@ -64,7 +85,7 @@ function validateConfigPaths (pluginConfig, executionPath) {
(key === 'scripts' && configEntry.path !== undefined && configEntry.path[0] === '/')) {
checkPathExists(executionPath, configEntry.path, key)
} else if ((key === 'nunjucksMacros')) {
checkPathExists(executionPath, configEntry.importFrom, key)
checkNunjucksMacroExists(executionPath, configEntry.importFrom, pluginConfig.nunjucksPaths)
} else {
// Find the path for the ones that can be objects
const invalidPath = (key === 'templates' || (key === 'scripts' && configEntry.path !== undefined))
Expand Down

0 comments on commit 0f5db6b

Please sign in to comment.