-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: refactor ignore config options tests (strings, regexp) (#3768)
- Loading branch information
1 parent
d919e8f
commit f291af2
Showing
4 changed files
with
157 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and other contributors where applicable. | ||
* Licensed under the BSD 2-Clause License; you may not use this file except in | ||
* compliance with the BSD 2-Clause License. | ||
*/ | ||
|
||
/** | ||
* Replaces some special values to be printed in stdout or env vars and | ||
* be revived by the test script or the fixture. These are | ||
* - Infinity which serializes to `null` by default | ||
* - RegExp serializes to `{}` by default | ||
* @param {string} key | ||
* @param {any} value | ||
* @returns {any} | ||
*/ | ||
function replacer(key, value) { | ||
if (value === Infinity) { | ||
return 'Infinity'; | ||
} | ||
if (Array.isArray(value)) { | ||
return value.map((item) => { | ||
if (item instanceof RegExp || typeof item === 'string') { | ||
return item.toString(); | ||
} | ||
return item; | ||
}); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
/** | ||
* Revives values from the serialized JSON with `replacer` | ||
* @param {string} key | ||
* @param {any} value | ||
* @returns {any} | ||
*/ | ||
function reviver(key, value) { | ||
if (value === 'Infinity') { | ||
return Infinity; | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
return value.map((item) => { | ||
if (typeof item === 'string' && /^\/.+\/i?$/.test(item)) { | ||
if (item.endsWith('i')) { | ||
return new RegExp(item.slice(1, item.length - 2), 'i'); | ||
} | ||
return new RegExp(item.slice(1, item.length - 1)); | ||
} | ||
return item; | ||
}); | ||
} | ||
return value; | ||
} | ||
|
||
module.exports = { | ||
replacer, | ||
reviver, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters