forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds deprecator (parse-community#7303)
* adds deprecator * un-fit * added changelog entry * some fixes * un-fit * removed deprecation definition * changed deprecation log syntax according to Nodejs
- Loading branch information
Showing
6 changed files
with
131 additions
and
1 deletion.
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,36 @@ | ||
'use strict'; | ||
|
||
const Deprecator = require('../lib/Deprecator/Deprecator'); | ||
|
||
describe('Deprecator', () => { | ||
let deprecations = []; | ||
|
||
beforeEach(async () => { | ||
deprecations = [{ optionKey: 'exampleKey', changeNewDefault: 'exampleNewDefault' }]; | ||
}); | ||
|
||
it('deprecations are an array', async () => { | ||
expect(Deprecator._getDeprecations()).toBeInstanceOf(Array); | ||
}); | ||
|
||
it('logs deprecation for new default', async () => { | ||
deprecations = [{ optionKey: 'exampleKey', changeNewDefault: 'exampleNewDefault' }]; | ||
|
||
spyOn(Deprecator, '_getDeprecations').and.callFake(() => deprecations); | ||
const logger = require('../lib/logger').logger; | ||
const logSpy = spyOn(logger, 'warn').and.callFake(() => {}); | ||
|
||
await reconfigureServer(); | ||
expect(logSpy.calls.all()[0].args[0]).toContain(deprecations[0].optionKey); | ||
expect(logSpy.calls.all()[0].args[0]).toContain(deprecations[0].changeNewDefault); | ||
}); | ||
|
||
it('does not log deprecation for new default if option is set manually', async () => { | ||
deprecations = [{ optionKey: 'exampleKey', changeNewDefault: 'exampleNewDefault' }]; | ||
|
||
spyOn(Deprecator, '_getDeprecations').and.callFake(() => deprecations); | ||
const logSpy = spyOn(Deprecator, '_log').and.callFake(() => {}); | ||
await reconfigureServer({ [deprecations[0].optionKey]: 'manuallySet' }); | ||
expect(logSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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,14 @@ | ||
/** | ||
* The deprecations. | ||
* | ||
* Add deprecations to the array using the following keys: | ||
* - `optionKey`: The option key incl. its path, e.g. `security.enableCheck`. | ||
* - `envKey`: The environment key, e.g. `PARSE_SERVER_SECURITY`. | ||
* - `changeNewKey`: Set the new key name if the current key will be replaced, | ||
* or set to an empty string if the current key will be removed without replacement. | ||
* - `changeNewDefault`: Set the new default value if the key's default value | ||
* will change in a future version. | ||
* | ||
* If there are no deprecations this must return an empty array anyway. | ||
*/ | ||
module.exports = []; |
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,71 @@ | ||
import logger from '../logger'; | ||
import Deprecations from './Deprecations'; | ||
|
||
/** | ||
* The deprecator class. | ||
*/ | ||
class Deprecator { | ||
/** | ||
* Scans the Parse Server for deprecated options. | ||
* This needs to be called before setting option defaults, otherwise it | ||
* becomes indistinguishable whether an option has been set manually or | ||
* by default. | ||
* @param {any} options The Parse Server options. | ||
*/ | ||
static scanParseServerOptions(options) { | ||
// Scan for deprecations | ||
for (const deprecation of Deprecator._getDeprecations()) { | ||
// Get deprecation properties | ||
const optionKey = deprecation.optionKey; | ||
const changeNewDefault = deprecation.changeNewDefault; | ||
|
||
// If default will change, only throw a warning if option is not set | ||
if (changeNewDefault != null && options[optionKey] == null) { | ||
Deprecator._log({ optionKey, changeNewDefault }); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the deprecation definitions. | ||
* @returns {Array<Object>} The deprecations. | ||
*/ | ||
static _getDeprecations() { | ||
return Deprecations; | ||
} | ||
|
||
/** | ||
* Logs a deprecation warning for a Parse Server option. | ||
* @param {String} optionKey The option key incl. its path, e.g. `security.enableCheck`. | ||
* @param {String} envKey The environment key, e.g. `PARSE_SERVER_SECURITY`. | ||
* @param {String} changeNewKey Set the new key name if the current key will be replaced, | ||
* or set to an empty string if the current key will be removed without replacement. | ||
* @param {String} changeNewDefault Set the new default value if the key's default value | ||
* will change in a future version. | ||
* @param {String} [solution] The instruction to resolve this deprecation warning. This | ||
* message must not include the warning that the parameter is deprecated, that is | ||
* automatically added to the message. It should only contain the instruction on how | ||
* to resolve this warning. | ||
*/ | ||
static _log({ optionKey, envKey, changeNewKey, changeNewDefault, solution }) { | ||
const type = optionKey ? 'option' : 'environment key'; | ||
const key = optionKey ? optionKey : envKey; | ||
const keyAction = | ||
changeNewKey == null | ||
? undefined | ||
: changeNewKey.length > 0 | ||
? `renamed to '${changeNewKey}'` | ||
: `removed`; | ||
|
||
// Compose message | ||
let output = `DeprecationWarning: The Parse Server ${type} '${key}' `; | ||
output += changeNewKey ? `is deprecated and will be ${keyAction} in a future version.` : ''; | ||
output += changeNewDefault | ||
? `default will change to '${changeNewDefault}' in a future version.` | ||
: ''; | ||
output += solution ? ` ${solution}` : ''; | ||
logger.warn(output); | ||
} | ||
} | ||
|
||
module.exports = Deprecator; |
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