OptionsUtil and tests updates (#1801)

* Update OptionsUtil.update to support an optional targetVersion param

* Update Anki template updates to have an explicit target version
This commit is contained in:
toasted-nutbread 2021-07-05 23:24:06 -04:00 committed by GitHub
parent 54808d4a2d
commit e155132085
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 20 deletions

View File

@ -31,7 +31,7 @@ class OptionsUtil {
this._optionsSchema = new JsonSchema(schema); this._optionsSchema = new JsonSchema(schema);
} }
async update(options) { async update(options, targetVersion=null) {
// Invalid options // Invalid options
if (!isObject(options)) { if (!isObject(options)) {
options = {}; options = {};
@ -84,7 +84,7 @@ class OptionsUtil {
} }
// Generic updates // Generic updates
options = await this._applyUpdates(options, this._getVersionUpdates()); options = await this._applyUpdates(options, this._getVersionUpdates(targetVersion));
// Validation // Validation
options = this._optionsSchema.getValidValueOrDefault(options); options = this._optionsSchema.getValidValueOrDefault(options);
@ -448,8 +448,8 @@ class OptionsUtil {
return options; return options;
} }
_getVersionUpdates() { _getVersionUpdates(targetVersion) {
return [ const result = [
{async: false, update: this._updateVersion1.bind(this)}, {async: false, update: this._updateVersion1.bind(this)},
{async: false, update: this._updateVersion2.bind(this)}, {async: false, update: this._updateVersion2.bind(this)},
{async: true, update: this._updateVersion3.bind(this)}, {async: true, update: this._updateVersion3.bind(this)},
@ -464,6 +464,10 @@ class OptionsUtil {
{async: true, update: this._updateVersion12.bind(this)}, {async: true, update: this._updateVersion12.bind(this)},
{async: true, update: this._updateVersion13.bind(this)} {async: true, update: this._updateVersion13.bind(this)}
]; ];
if (typeof targetVersion === 'number' && targetVersion < result.length) {
result.splice(targetVersion);
}
return result;
} }
_updateVersion1(options) { _updateVersion1(options) {

View File

@ -658,10 +658,10 @@ async function testFieldTemplatesUpdate(extDir) {
{version: 12, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v12.handlebars')}, {version: 12, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v12.handlebars')},
{version: 13, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v13.handlebars')} {version: 13, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v13.handlebars')}
]; ];
const getUpdateAdditions = (startVersion=0) => { const getUpdateAdditions = (startVersion, targetVersion) => {
let value = ''; let value = '';
for (const {version, changes} of updates) { for (const {version, changes} of updates) {
if (version < startVersion || changes.length === 0) { continue; } if (version < startVersion || version > targetVersion || changes.length === 0) { continue; }
if (value.length > 0) { value += '\n'; } if (value.length > 0) { value += '\n'; }
value += changes; value += changes;
} }
@ -671,6 +671,8 @@ async function testFieldTemplatesUpdate(extDir) {
const data = [ const data = [
// Standard format // Standard format
{ {
oldVersion: 0,
newVersion: 12,
old: ` old: `
{{#*inline "character"}} {{#*inline "character"}}
{{~definition.character~}} {{~definition.character~}}
@ -683,11 +685,13 @@ async function testFieldTemplatesUpdate(extDir) {
{{~definition.character~}} {{~definition.character~}}
{{/inline}} {{/inline}}
${getUpdateAdditions()} <<<UPDATE-ADDITIONS>>>
{{~> (lookup . "marker") ~}}`.trimStart() {{~> (lookup . "marker") ~}}`.trimStart()
}, },
// Non-standard marker format // Non-standard marker format
{ {
oldVersion: 0,
newVersion: 12,
old: ` old: `
{{#*inline "character"}} {{#*inline "character"}}
{{~definition.character~}} {{~definition.character~}}
@ -701,19 +705,23 @@ ${getUpdateAdditions()}
{{/inline}} {{/inline}}
{{~> (lookup . "marker2") ~}} {{~> (lookup . "marker2") ~}}
${getUpdateAdditions()}`.trimStart() <<<UPDATE-ADDITIONS>>>`.trimStart()
}, },
// Empty test // Empty test
{ {
oldVersion: 0,
newVersion: 12,
old: ` old: `
{{~> (lookup . "marker") ~}}`.trimStart(), {{~> (lookup . "marker") ~}}`.trimStart(),
expected: ` expected: `
${getUpdateAdditions()} <<<UPDATE-ADDITIONS>>>
{{~> (lookup . "marker") ~}}`.trimStart() {{~> (lookup . "marker") ~}}`.trimStart()
}, },
// Definition tags update // Definition tags update
{ {
oldVersion: 0,
newVersion: 12,
old: ` old: `
{{#*inline "glossary-single"}} {{#*inline "glossary-single"}}
{{~#unless brief~}} {{~#unless brief~}}
@ -779,13 +787,14 @@ ${getUpdateAdditions()}
{{~> glossary-single definition brief=brief compactGlossaries=../compactGlossaries data=../.~}} {{~> glossary-single definition brief=brief compactGlossaries=../compactGlossaries data=../.~}}
{{/inline}} {{/inline}}
${getUpdateAdditions()} <<<UPDATE-ADDITIONS>>>
{{~> (lookup . "marker") ~}} {{~> (lookup . "marker") ~}}
`.trimStart() `.trimStart()
}, },
// glossary and glossary-brief update // glossary and glossary-brief update
{ {
oldVersion: 7, oldVersion: 7,
newVersion: 12,
old: ` old: `
{{#*inline "glossary-single"}} {{#*inline "glossary-single"}}
{{~#unless brief~}} {{~#unless brief~}}
@ -876,11 +885,11 @@ ${getUpdateAdditions()}
{{~#if only~}}({{#each only}}{{.}}{{#unless @last}}, {{/unless}}{{/each}} only) {{/if~}} {{~#if only~}}({{#each only}}{{.}}{{#unless @last}}, {{/unless}}{{/each}} only) {{/if~}}
{{~/unless~}} {{~/unless~}}
{{~#if (op "<=" glossary.length 1)~}} {{~#if (op "<=" glossary.length 1)~}}
{{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{/each}} {{#each glossary}}{{#multiLine}}{{.}}{{/multiLine}}{{/each}}
{{~else if @root.compactGlossaries~}} {{~else if @root.compactGlossaries~}}
{{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{#unless @last}} | {{/unless}}{{/each}} {{#each glossary}}{{#multiLine}}{{.}}{{/multiLine}}{{#unless @last}} | {{/unless}}{{/each}}
{{~else~}} {{~else~}}
<ul>{{#each glossary}}<li>{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}</li>{{/each}}</ul> <ul>{{#each glossary}}<li>{{#multiLine}}{{.}}{{/multiLine}}</li>{{/each}}</ul>
{{~/if~}} {{~/if~}}
{{~#set "previousDictionary" dictionary~}}{{~/set~}} {{~#set "previousDictionary" dictionary~}}{{~/set~}}
{{/inline}} {{/inline}}
@ -919,12 +928,13 @@ ${getUpdateAdditions()}
{{~> glossary brief=true ~}} {{~> glossary brief=true ~}}
{{/inline}} {{/inline}}
${getUpdateAdditions(7)} <<<UPDATE-ADDITIONS>>>
{{~> (lookup . "marker") ~}}`.trimStart() {{~> (lookup . "marker") ~}}`.trimStart()
}, },
// formatGlossary update // formatGlossary update
{ {
oldVersion: 12, oldVersion: 12,
newVersion: 13,
old: ` old: `
{{~#if (op "<=" glossary.length 1)~}} {{~#if (op "<=" glossary.length 1)~}}
{{#each glossary}}{{#multiLine}}{{.}}{{/multiLine}}{{/each}} {{#each glossary}}{{#multiLine}}{{.}}{{/multiLine}}{{/each}}
@ -945,16 +955,17 @@ ${getUpdateAdditions(7)}
} }
]; ];
for (const {old, expected, oldVersion} of data) { const updatesPattern = /<<<UPDATE-ADDITIONS>>>/g;
for (const {old, expected, oldVersion, newVersion} of data) {
const options = createOptionsTestData1(); const options = createOptionsTestData1();
options.profiles[0].options.anki.fieldTemplates = old; options.profiles[0].options.anki.fieldTemplates = old;
if (typeof oldVersion === 'number') {
options.version = oldVersion; options.version = oldVersion;
}
const optionsUpdated = clone(await optionsUtil.update(options)); const expected2 = expected.replace(updatesPattern, getUpdateAdditions(oldVersion, newVersion));
const optionsUpdated = clone(await optionsUtil.update(options, newVersion));
const fieldTemplatesActual = optionsUpdated.profiles[0].options.anki.fieldTemplates; const fieldTemplatesActual = optionsUpdated.profiles[0].options.anki.fieldTemplates;
assert.deepStrictEqual(fieldTemplatesActual, expected); assert.deepStrictEqual(fieldTemplatesActual, expected2);
} }
} }