Skip to content

Commit

Permalink
Support requireReason
Browse files Browse the repository at this point in the history
  • Loading branch information
ronjouch committed Jun 29, 2022
1 parent 957724e commit 5050606
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 0 deletions.
12 changes: 12 additions & 0 deletions auditFile/fileHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ function readAndValidate (pathOverride) {
if (!versions[version]) {
throw Error(`Unrecognized ${FILE.FILENAME} content version ${version}`)
}
if (data.version > 0 && data.requireReason && data.requireReason !== "false") {
versions[version].schema.properties.decisions.additionalProperties.required.push('reason')
versions[version].schema.properties.decisions.additionalProperties.properties.reason.minLength = 1
if (data.requireReason !== true && data.requireReason !== "true") {
try {
new RegExp(data.requireReason)
versions[version].schema.properties.decisions.additionalProperties.properties.reason.pattern = data.requireReason
} catch (err) {
throw Error(`audit-resolve "requireReason" must be a boolean or a valid regex. Got ${data.requireReason}`)
}
}
}
validate(versions[version].schema, data)
return data
}
Expand Down
16 changes: 16 additions & 0 deletions test/auditFile/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ const auditFile = require('../../auditFile/fileHandle')

auditFile.load(path.resolve(__dirname, './v1ok.json'))

auditFile.load(path.resolve(__dirname, './v1reasonOk.json'))

auditFile.load(path.resolve(__dirname, './v1reasonOkRegex.json'))

assert.throws(() => {
auditFile.load(path.resolve(__dirname, './v1reasonKoEmptyString.json'))
})

assert.throws(() => {
auditFile.load(path.resolve(__dirname, './v1reasonKoLacking.json'))
})

assert.throws(() => {
auditFile.load(path.resolve(__dirname, './v1reasonKoRegex.json'))
})

assert.throws(() => {
auditFile.load(path.resolve(__dirname, './v1wrong.json'))
})
Expand Down
16 changes: 16 additions & 0 deletions test/auditFile/v1reasonKoEmptyString.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": 1,
"requireReason": true,
"decisions": {
"123|unnecessary>lodash": {
"decision": "none",
"madeAt": 1552676146555,
"reason": "package was removed"
},
"123|other": {
"decision": "postpone",
"madeAt": 1552676146555,
"reason": ""
}
}
}
15 changes: 15 additions & 0 deletions test/auditFile/v1reasonKoLacking.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": 1,
"requireReason": true,
"decisions": {
"123|unnecessary>lodash": {
"decision": "none",
"madeAt": 1552676146555,
"reason": "package was removed"
},
"123|other": {
"decision": "postpone",
"madeAt": 1552676146555
}
}
}
27 changes: 27 additions & 0 deletions test/auditFile/v1reasonKoRegex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": 1,
"requireReason": "^.+. I promise.$",
"decisions": {
"123|lodash": {
"decision": "fix",
"madeAt": 1552676146555,
"reason": "I did the thing. I promise."
},
"123|unnecessary>lodash": {
"decision": "none",
"madeAt": 1552676146555,
"reason": "package was removed. I promise."
},
"123|other": {
"decision": "postpone",
"madeAt": 1552676146555,
"reason": "Imcareless. I promise."
},
"123|somethingelse": {
"decision": "ignore",
"madeAt": 1552676146555,
"expiresAt": 2552676146555,
"reason": "tomorrow is a better day"
}
}
}
27 changes: 27 additions & 0 deletions test/auditFile/v1reasonOk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": 1,
"requireReason": true,
"decisions": {
"123|lodash": {
"decision": "fix",
"madeAt": 1552676146555,
"reason": "I did the thing"
},
"123|unnecessary>lodash": {
"decision": "none",
"madeAt": 1552676146555,
"reason": "package was removed"
},
"123|other": {
"decision": "postpone",
"madeAt": 1552676146555,
"reason": "Imcareless"
},
"123|somethingelse": {
"decision": "ignore",
"madeAt": 1552676146555,
"expiresAt": 2552676146555,
"reason": "tomorrow is a better day"
}
}
}
27 changes: 27 additions & 0 deletions test/auditFile/v1reasonOkRegex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": 1,
"requireReason": "^.+. I promise.$",
"decisions": {
"123|lodash": {
"decision": "fix",
"madeAt": 1552676146555,
"reason": "I did the thing. I promise."
},
"123|unnecessary>lodash": {
"decision": "none",
"madeAt": 1552676146555,
"reason": "package was removed. I promise."
},
"123|other": {
"decision": "postpone",
"madeAt": 1552676146555,
"reason": "Imcareless. I promise."
},
"123|somethingelse": {
"decision": "ignore",
"madeAt": 1552676146555,
"expiresAt": 2552676146555,
"reason": "tomorrow is a better day. I promise."
}
}
}

0 comments on commit 5050606

Please sign in to comment.