Skip to content

Commit

Permalink
feat(developer): local imports directory fixes
Browse files Browse the repository at this point in the history
- use path.join instead of path.resolve
- rename importsPath to cldrImportsPath
- remove k_015_importlocal from core, as it duplicated tests in common and developer

Fixes: #10649
  • Loading branch information
srl295 committed Dec 2, 2024
1 parent 48da8fd commit f4f0355
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 44 deletions.
22 changes: 0 additions & 22 deletions core/tests/unit/ldml/keyboards/k_015_importlocal.xml

This file was deleted.

6 changes: 0 additions & 6 deletions core/tests/unit/ldml/keyboards/keys-Zyyy-morepunctuation.xml

This file was deleted.

1 change: 0 additions & 1 deletion core/tests/unit/ldml/keyboards/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ tests_without_testdata = [
'k_010_mt',
'k_011_mt_iso',
'k_012_other',
'k_015_importlocal',
'k_030_transform_plus',
'k_100_keytest',
'k_101_keytest',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface NameAndProps {

export class LDMLKeyboardXMLSourceFileReaderOptions {
/** path to the CLDR imports */
importsPath: string;
cldrImportsPath: string;
/** ordered list of paths for local imports */
localImportsPaths: string[];
};
Expand All @@ -34,14 +34,14 @@ export class LDMLKeyboardXMLSourceFileReader {
}

readImportFile(version: string, subpath: string): Uint8Array {
const importPath = this.callbacks.resolveFilename(this.options.importsPath, `${version}/${subpath}`);
const importPath = this.callbacks.resolveFilename(this.options.cldrImportsPath, `${version}/${subpath}`);
return this.callbacks.loadFile(importPath);
}

readLocalImportFile(path: string): Uint8Array {
// try each of the local imports paths
for (const localPath of this.options.localImportsPaths) {
const importPath = this.callbacks.resolveFilename(localPath, path);
const importPath = this.callbacks.path.join(localPath, path);
if(this.callbacks.fs.existsSync(importPath)) {
return this.callbacks.loadFile(importPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { LDMLKeyboardXMLSourceFile } from '../../src/types/ldml-keyboard/ldml-ke
import { LDMLKeyboardTestDataXMLSourceFile } from '../../src/types/ldml-keyboard/ldml-keyboard-testdata-xml.js';
import { TestCompilerCallbacks } from '@keymanapp/developer-test-helpers';
import { fileURLToPath } from 'url';
import { dirname, sep } from 'node:path';
import { dirname } from 'node:path';

const readerOptions: LDMLKeyboardXMLSourceFileReaderOptions = {
importsPath: fileURLToPath(new URL(...LDMLKeyboardXMLSourceFileReader.defaultImportsURL)),
cldrImportsPath: fileURLToPath(new URL(...LDMLKeyboardXMLSourceFileReader.defaultImportsURL)),
localImportsPaths: [],
};

Expand Down Expand Up @@ -85,8 +85,8 @@ export function testReaderCases(cases : CompilationCase[]) {
it(testHeading, function () {
callbacks.clear();
const path = makePathToFixture('ldml-keyboard', testcase.subpath);
// update readerOptions to point to the source dir. Need a trailing separator.
readerOptions.localImportsPaths = [ dirname(path) + sep ];
// update readerOptions to point to the source dir.
readerOptions.localImportsPaths = [ dirname(path) ];
const reader = new LDMLKeyboardXMLSourceFileReader(readerOptions, callbacks);
const data = loadFile(path);
assert.ok(data, `reading ${testcase.subpath}`);
Expand Down
4 changes: 2 additions & 2 deletions developer/src/kmc-ldml/test/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const compilerTestCallbacks = new TestCompilerCallbacks();

export const compilerTestOptions: LdmlCompilerOptions = {
readerOptions: {
importsPath: fileURLToPath(new URL(...LDMLKeyboardXMLSourceFileReader.defaultImportsURL)),
cldrImportsPath: fileURLToPath(new URL(...LDMLKeyboardXMLSourceFileReader.defaultImportsURL)),
localImportsPaths: [], // will be fixed up in loadSectionFixture
}
};
Expand All @@ -60,7 +60,7 @@ export async function loadSectionFixture(compilerClass: SectionCompilerNew, file
const data = callbacks.loadFile(inputFilename);
assert.isNotNull(data, `Failed to read file ${inputFilename}`);

compilerTestOptions.readerOptions.localImportsPaths = [ path.dirname(inputFilename) + path.sep ];
compilerTestOptions.readerOptions.localImportsPaths = [ path.dirname(inputFilename) ];

const reader = new LDMLKeyboardXMLSourceFileReader(compilerTestOptions.readerOptions, callbacks);
const source = reader.load(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CompilerOptions, CompilerCallbacks } from '@keymanapp/developer-utils';
import { LDMLKeyboardXMLSourceFileReader } from '@keymanapp/developer-utils';
import { BuildActivity } from './BuildActivity.js';
import { fileURLToPath } from 'url';
import { dirname, sep } from 'node:path';
import { dirname } from 'node:path';

export class BuildLdmlKeyboard extends BuildActivity {
public get name(): string { return 'LDML keyboard'; }
Expand All @@ -14,8 +14,8 @@ export class BuildLdmlKeyboard extends BuildActivity {
public async build(infile: string, outfile: string, callbacks: CompilerCallbacks, options: CompilerOptions): Promise<boolean> {
// TODO-LDML: consider hardware vs touch -- touch-only layout will not have a .kvk
const ldmlCompilerOptions: kmcLdml.LdmlCompilerOptions = {...options, readerOptions: {
importsPath: fileURLToPath(new URL(...LDMLKeyboardXMLSourceFileReader.defaultImportsURL)),
localImportsPaths: [ dirname(infile) + sep ], // local dir
cldrImportsPath: fileURLToPath(new URL(...LDMLKeyboardXMLSourceFileReader.defaultImportsURL)),
localImportsPaths: [ dirname(infile) ], // local dir
}};
const compiler = new kmcLdml.LdmlKeyboardCompiler();
return await super.runCompiler(compiler, infile, outfile, callbacks, ldmlCompilerOptions);
Expand Down
6 changes: 3 additions & 3 deletions developer/src/kmc/src/commands/buildTestData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fileURLToPath } from 'url';
import { CommandLineBaseOptions } from 'src/util/baseOptions.js';
import { exitProcess } from '../../util/sysexits.js';
import { InfrastructureMessages } from '../../messages/infrastructureMessages.js';
import { dirname, sep } from 'node:path';
import { dirname } from 'node:path';

export async function buildTestData(infile: string, _options: any, commander: any): Promise<void> {
const options: CommandLineBaseOptions = commander.optsWithGlobals();
Expand All @@ -18,8 +18,8 @@ export async function buildTestData(infile: string, _options: any, commander: an
saveDebug: false,
shouldAddCompilerVersion: false,
readerOptions: {
importsPath: fileURLToPath(new URL(...LDMLKeyboardXMLSourceFileReader.defaultImportsURL)),
localImportsPaths: [ dirname(infile) + sep ], // local dir
cldrImportsPath: fileURLToPath(new URL(...LDMLKeyboardXMLSourceFileReader.defaultImportsURL)),
localImportsPaths: [ dirname(infile) ], // local dir
}
};

Expand Down

0 comments on commit f4f0355

Please sign in to comment.