Skip to content

Commit

Permalink
Merge pull request #10157 from keymanapp/fix/common/10143-kpj-handle-…
Browse files Browse the repository at this point in the history
…empty-files-element

fix(common): kpj loader needs to handle empty Files element 🦕
  • Loading branch information
mcdurdin authored Dec 11, 2023
2 parents a63070d + d693bbd commit 03625c6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
8 changes: 7 additions & 1 deletion common/web/types/src/kpj/kpj-file-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<KeymanDeveloperProject>
<Options>
<BuildPath>$PROJECTPATH</BuildPath>
<CompilerWarningsAsErrors>False</CompilerWarningsAsErrors>
<WarnDeprecatedCode>False</WarnDeprecatedCode>
<CheckFilenameConventions>False</CheckFilenameConventions>
<ProjectType>keyboard</ProjectType>
</Options>
<Files>
</Files>
</KeymanDeveloperProject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<KeymanDeveloperProject>
<Options>
<BuildPath>$PROJECTPATH</BuildPath>
<CompilerWarningsAsErrors>False</CompilerWarningsAsErrors>
<WarnDeprecatedCode>False</WarnDeprecatedCode>
<CheckFilenameConventions>False</CheckFilenameConventions>
<ProjectType>keyboard</ProjectType>
</Options>
</KeymanDeveloperProject>
30 changes: 30 additions & 0 deletions common/web/types/test/kpj/test-kpj-file-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,34 @@ describe('kpj-file-reader', function () {
assert.isEmpty(f.details);
assert.lengthOf(f.childFiles, 0);
});

it('should load a v1.0 keyboard project with missing <File>', 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);
assert.lengthOf(kpj.KeymanDeveloperProject.Files.File, 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 <Files>', 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);
assert.lengthOf(kpj.KeymanDeveloperProject.Files.File, 0);
const project = reader.transform(path, kpj);
assert.equal(callbacks.messages.length, 0);
assert.isNotNull(project);
});

});

0 comments on commit 03625c6

Please sign in to comment.