Options fixes (#962)

* Fix default options not having the correct versions

* Move schema validation

* Remove legacy version number

* Add tests for OptionsUtil.getDefault()

* Remove unused getValidValueOrDefault
This commit is contained in:
toasted-nutbread 2020-10-27 19:26:30 -04:00 committed by GitHub
parent 45627bd6e6
commit dfdefc15d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 15 deletions

View File

@ -62,7 +62,6 @@
"options": { "options": {
"type": "object", "type": "object",
"required": [ "required": [
"version",
"general", "general",
"audio", "audio",
"scanning", "scanning",
@ -72,10 +71,6 @@
"anki" "anki"
], ],
"properties": { "properties": {
"version": {
"type": "integer",
"minimum": 0
},
"general": { "general": {
"type": "object", "type": "object",
"required": [ "required": [

View File

@ -82,7 +82,13 @@ class OptionsUtil {
} }
// Generic updates // Generic updates
return await this._applyUpdates(options, this._getVersionUpdates()); options = await this._applyUpdates(options, this._getVersionUpdates());
// Validation
options = this._schemaValidator.getValidValueOrDefault(this._optionsSchema, options);
// Result
return options;
} }
async load() { async load() {
@ -105,10 +111,10 @@ class OptionsUtil {
if (typeof options !== 'undefined') { if (typeof options !== 'undefined') {
options = await this.update(options); options = await this.update(options);
} else {
options = this.getDefault();
} }
options = this._schemaValidator.getValidValueOrDefault(this._optionsSchema, options);
return options; return options;
} }
@ -126,11 +132,10 @@ class OptionsUtil {
} }
getDefault() { getDefault() {
return this._schemaValidator.getValidValueOrDefault(this._optionsSchema); const optionsVersion = this._getVersionUpdates().length;
} const options = this._schemaValidator.getValidValueOrDefault(this._optionsSchema);
options.version = optionsVersion;
getValidValueOrDefault(options) { return options;
return this._schemaValidator.getValidValueOrDefault(this._optionsSchema, options);
} }
createValidatingProxy(options) { createValidatingProxy(options) {
@ -468,6 +473,10 @@ class OptionsUtil {
{ {
async: true, async: true,
update: this._updateVersion4.bind(this) update: this._updateVersion4.bind(this)
},
{
async: false,
update: this._updateVersion5.bind(this)
} }
]; ];
} }
@ -586,4 +595,13 @@ class OptionsUtil {
await this._addFieldTemplatesToOptions(options, '/bg/data/anki-field-templates-upgrade-v4.handlebars'); await this._addFieldTemplatesToOptions(options, '/bg/data/anki-field-templates-upgrade-v4.handlebars');
return options; return options;
} }
_updateVersion5(options) {
// Version 5 changes:
// Removed legacy version number from profile options.
for (const profile of options.profiles) {
delete profile.options.version;
}
return options;
}
} }

View File

@ -336,7 +336,6 @@ class BackupController {
// Upgrade options // Upgrade options
optionsFull = await this._optionsUtil.update(optionsFull); optionsFull = await this._optionsUtil.update(optionsFull);
optionsFull = this._optionsUtil.getValidValueOrDefault(optionsFull);
// Check for warnings // Check for warnings
const sanitizationWarnings = this._settingsImportSanitizeOptions(optionsFull, true); const sanitizationWarnings = this._settingsImportSanitizeOptions(optionsFull, true);

View File

@ -63,6 +63,7 @@ function clone(value) {
function createProfileOptionsTestData1() { function createProfileOptionsTestData1() {
return { return {
version: 14,
general: { general: {
enable: true, enable: true,
enableClipboardPopups: false, enableClipboardPopups: false,
@ -495,7 +496,7 @@ function createOptionsUpdatedTestData1() {
} }
], ],
profileCurrent: 0, profileCurrent: 0,
version: 4, version: 5,
global: { global: {
database: { database: {
prefixWildcardsSupported: false prefixWildcardsSupported: false
@ -517,6 +518,32 @@ async function testUpdate(extDir) {
assert.deepStrictEqual(optionsUpdated, optionsExpected); assert.deepStrictEqual(optionsUpdated, optionsExpected);
} }
async function testDefault(extDir) {
const data = [
(options) => options,
(options) => {
delete options.profiles[0].options.audio.autoPlay;
},
(options) => {
options.profiles[0].options.audio.autoPlay = void 0;
}
];
const vm = createVM(extDir);
const [OptionsUtil] = vm.get(['OptionsUtil']);
const optionsUtil = new OptionsUtil();
await optionsUtil.prepare();
for (const modify of data) {
const options = optionsUtil.getDefault();
const optionsModified = clone(options);
modify(optionsModified);
const optionsUpdated = await optionsUtil.update(clone(optionsModified));
assert.deepStrictEqual(clone(optionsUpdated), clone(options));
}
}
async function testFieldTemplatesUpdate(extDir) { async function testFieldTemplatesUpdate(extDir) {
const vm = createVM(extDir); const vm = createVM(extDir);
@ -590,6 +617,7 @@ ${update4}
async function main() { async function main() {
const extDir = path.join(__dirname, '..', 'ext'); const extDir = path.join(__dirname, '..', 'ext');
await testUpdate(extDir); await testUpdate(extDir);
await testDefault(extDir);
await testFieldTemplatesUpdate(extDir); await testFieldTemplatesUpdate(extDir);
} }