From a330666515445038b51315a258493f364744f7b6 Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Wed, 6 Dec 2023 12:37:25 +0700 Subject: [PATCH 1/2] fix(common): kpj loader needs to handle empty Files element Fixes #10143. --- common/web/types/src/kpj/kpj-file-reader.ts | 8 +++++- .../project_missing_file.kpj | 12 ++++++++ .../project_missing_files.kpj | 10 +++++++ .../types/test/kpj/test-kpj-file-reader.ts | 28 +++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 common/web/types/test/fixtures/kpj/project-missing-file/project_missing_file.kpj create mode 100644 common/web/types/test/fixtures/kpj/project-missing-file/project_missing_files.kpj diff --git a/common/web/types/src/kpj/kpj-file-reader.ts b/common/web/types/src/kpj/kpj-file-reader.ts index 29ffe8009d9..d005e0a0abb 100644 --- a/common/web/types/src/kpj/kpj-file-reader.ts +++ b/common/web/types/src/kpj/kpj-file-reader.ts @@ -117,7 +117,13 @@ export class KPJFileReader { * @param source KVKSourceFile */ private boxArrays(source: KPJFile) { - boxXmlArray(source.KeymanDeveloperProject?.Files, 'File'); + if(!source.KeymanDeveloperProject) { + return source; + } + if(!source.KeymanDeveloperProject.Files || typeof source.KeymanDeveloperProject.Files == 'string') { + source.KeymanDeveloperProject.Files = {File:[]}; + } + boxXmlArray(source.KeymanDeveloperProject.Files, 'File'); return source; } } \ No newline at end of file diff --git a/common/web/types/test/fixtures/kpj/project-missing-file/project_missing_file.kpj b/common/web/types/test/fixtures/kpj/project-missing-file/project_missing_file.kpj new file mode 100644 index 00000000000..38fa0201781 --- /dev/null +++ b/common/web/types/test/fixtures/kpj/project-missing-file/project_missing_file.kpj @@ -0,0 +1,12 @@ + + + + $PROJECTPATH + False + False + False + keyboard + + + + diff --git a/common/web/types/test/fixtures/kpj/project-missing-file/project_missing_files.kpj b/common/web/types/test/fixtures/kpj/project-missing-file/project_missing_files.kpj new file mode 100644 index 00000000000..6033b4de103 --- /dev/null +++ b/common/web/types/test/fixtures/kpj/project-missing-file/project_missing_files.kpj @@ -0,0 +1,10 @@ + + + + $PROJECTPATH + False + False + False + keyboard + + diff --git a/common/web/types/test/kpj/test-kpj-file-reader.ts b/common/web/types/test/kpj/test-kpj-file-reader.ts index 12c1e7a134e..589f6cad98f 100644 --- a/common/web/types/test/kpj/test-kpj-file-reader.ts +++ b/common/web/types/test/kpj/test-kpj-file-reader.ts @@ -123,4 +123,32 @@ describe('kpj-file-reader', function () { assert.isEmpty(f.details); assert.lengthOf(f.childFiles, 0); }); + + it('should load a v1.0 keyboard project with missing ', function() { + const path = makePathToFixture('kpj', 'project-missing-file', 'project_missing_file.kpj'); + const input = fs.readFileSync(path); + const reader = new KPJFileReader(callbacks); + const kpj = reader.read(input); + reader.validate(kpj); + if(callbacks.messages.length) { + callbacks.printMessages(); + } + assert.equal(callbacks.messages.length, 0); + const project = reader.transform(path, kpj); + assert.equal(callbacks.messages.length, 0); + assert.isNotNull(project); + }); + + it('should load a v1.0 keyboard project with missing ', function() { + const path = makePathToFixture('kpj', 'project-missing-file', 'project_missing_files.kpj'); + const input = fs.readFileSync(path); + const reader = new KPJFileReader(callbacks); + const kpj = reader.read(input); + reader.validate(kpj); + assert.equal(callbacks.messages.length, 0); + const project = reader.transform(path, kpj); + assert.equal(callbacks.messages.length, 0); + assert.isNotNull(project); + }); + }); From d693bbd457d3b6a5a8d3068f66581a72370439db Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Wed, 6 Dec 2023 13:00:04 +0700 Subject: [PATCH 2/2] chore: address review comments --- common/web/types/test/kpj/test-kpj-file-reader.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/web/types/test/kpj/test-kpj-file-reader.ts b/common/web/types/test/kpj/test-kpj-file-reader.ts index 589f6cad98f..cbebdd79207 100644 --- a/common/web/types/test/kpj/test-kpj-file-reader.ts +++ b/common/web/types/test/kpj/test-kpj-file-reader.ts @@ -134,6 +134,7 @@ describe('kpj-file-reader', function () { callbacks.printMessages(); } assert.equal(callbacks.messages.length, 0); + assert.lengthOf(kpj.KeymanDeveloperProject.Files.File, 0); const project = reader.transform(path, kpj); assert.equal(callbacks.messages.length, 0); assert.isNotNull(project); @@ -146,6 +147,7 @@ describe('kpj-file-reader', function () { const kpj = reader.read(input); reader.validate(kpj); assert.equal(callbacks.messages.length, 0); + assert.lengthOf(kpj.KeymanDeveloperProject.Files.File, 0); const project = reader.transform(path, kpj); assert.equal(callbacks.messages.length, 0); assert.isNotNull(project);