-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(developer): handle missing files in kmc-model
If a file is not found, loadfile returns null, which kmc-model now handles with a clear error message rather than a generic exception. Checks added for missing .model.ts and missing wordlist.tsv files. Added corresponding unit tests. Fixes: #12553 Fixes: KEYMAN-DEVELOPER-294
- Loading branch information
Showing
5 changed files
with
86 additions
and
4 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
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
5 changes: 5 additions & 0 deletions
5
...ixtures/invalid-models/example.qaa.missing-wordlist/example.qaa.missing-wordlist.model.ts
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,5 @@ | ||
const source: LexicalModelSource = { | ||
format: 'trie-1.0', | ||
sources: ['wordlist.tsv'], | ||
}; | ||
export default source; |
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 |
---|---|---|
@@ -1,10 +1,62 @@ | ||
import 'mocha'; | ||
import { assert } from 'chai'; | ||
import { ModelCompilerMessages } from '../src/model-compiler-messages.js'; | ||
import { verifyCompilerMessagesObject } from '@keymanapp/developer-test-helpers'; | ||
import { TestCompilerCallbacks, verifyCompilerMessagesObject } from '@keymanapp/developer-test-helpers'; | ||
import { CompilerErrorNamespace } from '@keymanapp/developer-utils'; | ||
import { LexicalModelCompiler } from '../src/lexical-model-compiler.js'; | ||
import { makePathToFixture } from './helpers/index.js'; | ||
|
||
describe('ModelCompilerMessages', function () { | ||
|
||
const callbacks = new TestCompilerCallbacks(); | ||
|
||
this.beforeEach(function() { | ||
callbacks.clear(); | ||
}); | ||
|
||
this.afterEach(function() { | ||
if(this.currentTest?.isFailed()) { | ||
callbacks.printMessages(); | ||
} | ||
}); | ||
|
||
it('should have a valid ModelCompilerMessages object', function() { | ||
return verifyCompilerMessagesObject(ModelCompilerMessages, CompilerErrorNamespace.ModelCompiler); | ||
}); | ||
|
||
async function testForMessage(fixture: string[], messageId?: number) { | ||
const compiler = new LexicalModelCompiler(); | ||
assert.isTrue(await compiler.init(callbacks, null)); | ||
|
||
const modelPath = makePathToFixture(...fixture); | ||
|
||
// Note: throwing away compile results (just to memory) | ||
await compiler.run(modelPath, null); | ||
|
||
if(messageId) { | ||
assert.isTrue(callbacks.hasMessage(messageId), `messageId ${messageId.toString(16)} not generated, instead got: `+JSON.stringify(callbacks.messages,null,2)); | ||
assert.lengthOf(callbacks.messages, 1, `messages should have 1 entry, instead has: `+JSON.stringify(callbacks.messages,null,2)); | ||
} else { | ||
assert.lengthOf(callbacks.messages, 0, `messages should be empty, but instead got: `+JSON.stringify(callbacks.messages,null,2)); | ||
} | ||
} | ||
|
||
// ERROR_ModelFileNotFound | ||
|
||
it('should generate ERROR_ModelFileNotFound if a .model.ts file is missing', async function() { | ||
await testForMessage( | ||
['invalid-models', 'missing-file.model.ts'], | ||
ModelCompilerMessages.ERROR_ModelFileNotFound | ||
); | ||
}); | ||
|
||
// ERROR_WordlistFileNotFound | ||
|
||
it('should generate ERROR_WordlistFileNotFound if a .tsv file is missing', async function() { | ||
await testForMessage( | ||
['invalid-models', 'example.qaa.missing-wordlist', 'example.qaa.missing-wordlist.model.ts'], | ||
ModelCompilerMessages.ERROR_WordlistFileNotFound | ||
); | ||
}); | ||
|
||
}); |