From 354f42836f94a1d41a499d6703321052318281d4 Mon Sep 17 00:00:00 2001 From: Anjey Tsibylskij <130153594+atldays@users.noreply.github.com> Date: Mon, 11 May 2026 23:07:45 +0300 Subject: [PATCH] fix(tests): improve path normalization and add Windows-specific diagnostics test --- .github/workflows/ci.yml | 10 +++--- .../locale.test.ts} | 33 +++++++++++++------ 2 files changed, 28 insertions(+), 15 deletions(-) rename src/{locale/LocaleTypes.test.ts => types/locale.test.ts} (74%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2fd75d2..c9f48b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,12 +27,12 @@ jobs: steps: - id: set run: | - if [[ "${{ inputs.full }}" == "true" ]]; then - echo 'matrix={"os":["ubuntu-latest","windows-latest"],"node":[22,24]}' >> $GITHUB_OUTPUT - echo 'name_suffix=(full matrix)' >> $GITHUB_OUTPUT + if [[ "${{ github.event_name }}" != "workflow_call" || "${{ inputs.full }}" == "true" ]]; then + echo 'matrix={"os":["ubuntu-latest","windows-latest"],"node":[22,24]}' >> "$GITHUB_OUTPUT" + echo 'name_suffix=(full matrix)' >> "$GITHUB_OUTPUT" else - echo 'matrix={"os":["ubuntu-latest"],"node":[22]}' >> $GITHUB_OUTPUT - echo 'name_suffix=' >> $GITHUB_OUTPUT + echo 'matrix={"os":["ubuntu-latest"],"node":[22]}' >> "$GITHUB_OUTPUT" + echo 'name_suffix=' >> "$GITHUB_OUTPUT" fi build-and-test: diff --git a/src/locale/LocaleTypes.test.ts b/src/types/locale.test.ts similarity index 74% rename from src/locale/LocaleTypes.test.ts rename to src/types/locale.test.ts index d526a6b..9d57c0b 100644 --- a/src/locale/LocaleTypes.test.ts +++ b/src/types/locale.test.ts @@ -1,13 +1,13 @@ import path from "path"; import ts from "typescript"; -const normalizeDiagnosticFilename = (filename: string): string => { - return path.normalize(filename).toLowerCase(); +const normalizeFilename = (filename: string): string => { + return path.normalize(filename).replaceAll("\\", "/").toLowerCase(); }; -const typecheck = (source: string): string[] => { - const filename = path.join(__dirname, "__locale-type-test.ts"); - const normalizedFilename = normalizeDiagnosticFilename(filename); +const typecheck = (source: string, filename = path.join(__dirname, "__locale-type-test.ts")): string[] => { + const normalizedFilename = normalizeFilename(filename); + const isTestFilename = (file: string): boolean => normalizeFilename(file) === normalizedFilename; const options: ts.CompilerOptions = { module: ts.ModuleKind.ESNext, moduleResolution: ts.ModuleResolutionKind.Bundler, @@ -24,28 +24,28 @@ const typecheck = (source: string): string[] => { const fileExists = host.fileExists.bind(host); host.getSourceFile = (file, languageVersion, onError, shouldCreateNewSourceFile) => { - if (file === filename) { + if (isTestFilename(file)) { return ts.createSourceFile(file, source, languageVersion, true); } return getSourceFile(file, languageVersion, onError, shouldCreateNewSourceFile); }; - host.readFile = file => (file === filename ? source : readFile(file)); - host.fileExists = file => file === filename || fileExists(file); + host.readFile = file => (isTestFilename(file) ? source : readFile(file)); + host.fileExists = file => isTestFilename(file) || fileExists(file); const program = ts.createProgram([filename], options, host); return ts .getPreEmitDiagnostics(program) .filter(diagnostic => { - return diagnostic.file && normalizeDiagnosticFilename(diagnostic.file.fileName) === normalizedFilename; + return diagnostic.file?.text === source; }) .map(diagnostic => ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")); }; const prelude = ` -import type {LocaleNonPluralKeys, LocalePluralKeys, LocaleSubstitutionArgs} from "../types/locale"; +import type {LocaleNonPluralKeys, LocalePluralKeys, LocaleSubstitutionArgs} from "./locale"; interface Structure { "app.name": {plural: false; substitutions: []}; @@ -66,6 +66,19 @@ declare function choice>( `; describe("locale types", () => { + test("captures diagnostics for Windows-style test filenames", () => { + const diagnostics = typecheck( + ` +declare function takesString(value: string): void; + +takesString(123); +`, + path.join(__dirname, "__locale-type-test.ts").split(path.sep).join("\\") + ).join("\n"); + + expect(diagnostics).toMatch("Argument of type 'number' is not assignable to parameter of type 'string'."); + }); + test("accept valid locale calls", () => { expect( typecheck(`