From 99ca5e0ab34e62517537899eb9a706ae9320b2e8 Mon Sep 17 00:00:00 2001 From: Osswald Date: Mon, 25 Mar 2024 17:13:43 +0100 Subject: [PATCH] test: Fix lintFiles on Windows --- src/detectors/typeChecker/host.ts | 4 ++-- src/detectors/typeChecker/index.ts | 8 ++++---- src/linter/linter.ts | 2 +- test/lib/linter/_linterHelper.ts | 16 +++++++++++++-- test/lib/linter/linter.ts | 33 ++++++++++++------------------ 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/detectors/typeChecker/host.ts b/src/detectors/typeChecker/host.ts index 8a9dd4d1e..b5358963c 100644 --- a/src/detectors/typeChecker/host.ts +++ b/src/detectors/typeChecker/host.ts @@ -91,7 +91,7 @@ export async function createVirtualCompilerHost( for (const filePath of files.keys()) { // Add every directory of the file path to the set of directories let directory = posixPath.dirname(filePath); - while (directory !== "/") { + while (directory !== "/" && directory !== ".") { directories.add(directory); directory = posixPath.dirname(directory); } @@ -100,7 +100,7 @@ export async function createVirtualCompilerHost( for (const typePackageDir of typePackageDirs) { // Add every directory of the type package path to the set of directories let directory = typePackageDir; - while (directory !== "/") { + while (directory !== "/" && directory !== ".") { directories.add(directory); directory = posixPath.dirname(directory); } diff --git a/src/detectors/typeChecker/index.ts b/src/detectors/typeChecker/index.ts index 1f8279776..869937993 100644 --- a/src/detectors/typeChecker/index.ts +++ b/src/detectors/typeChecker/index.ts @@ -272,7 +272,7 @@ export class TsFileDetector extends FileBasedDetector { const internalfilePaths = await Promise.all(filePaths.map(async (filePath: string) => { let transformationResult; filePath = path.join(this.rootDir, filePath); - let internalfilePath = filePath; + let internalfilePath = filePath.replace(/\\/g, "/"); if (filePath.endsWith(".js")) { const fileContent = ts.sys.readFile(filePath); if (!fileContent) { @@ -281,15 +281,15 @@ export class TsFileDetector extends FileBasedDetector { transformationResult = amdToEsm(path.basename(filePath, ".js"), fileContent); } else if (filePath.endsWith(".xml")) { const fileStream = fs.createReadStream(filePath); - internalfilePath = filePath.replace(/\.xml$/, ".js"); + internalfilePath = internalfilePath.replace(/\.xml$/, ".js"); transformationResult = await xmlToJs(path.basename(filePath), fileStream); } else if (filePath.endsWith(".json")) { const fileContent = ts.sys.readFile(filePath); if (!fileContent) { throw new Error(`Failed to read file ${filePath}`); } - internalfilePath = filePath.replace(/\.json$/, ".js"); - transformationResult = await lintManifest(internalfilePath, fileContent); + internalfilePath = internalfilePath.replace(/\.json$/, ".js"); + transformationResult = await lintManifest(filePath.replace(/\.json$/, ".js"), fileContent); } else { throw new Error(`Unsupported file type for ${filePath}`); } diff --git a/src/linter/linter.ts b/src/linter/linter.ts index 04eef2cfc..86e4ef2fc 100644 --- a/src/linter/linter.ts +++ b/src/linter/linter.ts @@ -6,7 +6,7 @@ import path from "node:path"; import {stat} from "node:fs/promises"; import {ProjectGraph} from "@ui5/project"; -interface LinterOptions { +export interface LinterOptions { rootDir: string; filePaths: string[]; reportCoverage?: boolean; diff --git a/test/lib/linter/_linterHelper.ts b/test/lib/linter/_linterHelper.ts index 4f8e433b2..21bcc1b21 100644 --- a/test/lib/linter/_linterHelper.ts +++ b/test/lib/linter/_linterHelper.ts @@ -6,12 +6,13 @@ import esmock from "esmock"; import {LintResult} from "../../../src/detectors/AbstractDetector.js"; import FileLinter from "../../../src/detectors/typeChecker/FileLinter.js"; import {SourceFile, TypeChecker} from "typescript"; +import {LinterOptions} from "../../../src/linter/linter.js"; util.inspect.defaultOptions.depth = 4; // Increase AVA's printing depth since coverageInfo objects are on level 4 const test = anyTest as TestFn<{ sinon: sinonGlobal.SinonSandbox; - lintFile: SinonStub; + lintFile: SinonStub<[LinterOptions], Promise>; }>; test.before(async (t) => { @@ -93,7 +94,7 @@ export function createTestsForFixtures(fixturesPath: string) { messageDetails: true, }); assertExpectedLintResults(t, res, fixturesPath, filePaths); - res.forEach((results: {filePath: string}) => { + res.forEach((results) => { results.filePath = testName; }); t.snapshot(res); @@ -107,3 +108,14 @@ export function createTestsForFixtures(fixturesPath: string) { throw err; } } + +export function preprocessLintResultsForSnapshot(res: LintResult[]) { + res.sort((a, b) => { + return a.filePath.localeCompare(b.filePath); + }); + res.forEach((message) => { + // Convert to posix paths to align snapshots across platforms + message.filePath = message.filePath.replace(/\\/g, "/"); + }); + return res; +} diff --git a/test/lib/linter/linter.ts b/test/lib/linter/linter.ts index c772de935..ab1af26be 100644 --- a/test/lib/linter/linter.ts +++ b/test/lib/linter/linter.ts @@ -2,7 +2,12 @@ import anyTest, {TestFn} from "ava"; import sinonGlobal, {SinonStub} from "sinon"; import path from "node:path"; import {fileURLToPath} from "node:url"; -import {createTestsForFixtures, assertExpectedLintResults, esmockDeprecationText} from "./_linterHelper.js"; +import { + createTestsForFixtures, assertExpectedLintResults, + esmockDeprecationText, preprocessLintResultsForSnapshot, +} from "./_linterHelper.js"; +import {LintResult} from "../../../src/detectors/AbstractDetector.js"; +import {LinterOptions} from "../../../src/linter/linter.js"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const fixturesBasePath = path.join(__dirname, "..", "..", "fixtures", "linter"); @@ -11,7 +16,7 @@ const fixturesProjectsPath = path.join(fixturesBasePath, "projects"); const test = anyTest as TestFn<{ sinon: sinonGlobal.SinonSandbox; - lintProject: SinonStub; + lintProject: SinonStub<[LinterOptions], Promise>; }>; test.before(async (t) => { @@ -32,18 +37,14 @@ test.serial("lint: All files of com.ui5.troublesome.app", async (t) => { const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app"); const {lintProject} = t.context; - let res = await lintProject({ + const res = await lintProject({ rootDir: projectPath, filePaths: [], reportCoverage: true, messageDetails: true, }); - res = res.sort((a: {filePath: string}, b: {filePath: string}) => { - return a.filePath.localeCompare(b.filePath); - }); - - t.snapshot(res); + t.snapshot(preprocessLintResultsForSnapshot(res)); }); test.serial("lint: Some files of com.ui5.troublesome.app (without details / coverage)", async (t) => { @@ -55,37 +56,29 @@ test.serial("lint: Some files of com.ui5.troublesome.app (without details / cove ]; const {lintProject} = t.context; - let res = await lintProject({ + const res = await lintProject({ rootDir: projectPath, filePaths, }); - res = res.sort((a: {filePath: string}, b: {filePath: string}) => { - return a.filePath.localeCompare(b.filePath); - }); - assertExpectedLintResults(t, res, projectPath, [ path.join("webapp", "model", "models.js"), ...filePaths, ]); - t.snapshot(res); + t.snapshot(preprocessLintResultsForSnapshot(res)); }); test.serial("lint: All files of library.with.custom.paths", async (t) => { const projectPath = path.join(fixturesProjectsPath, "library.with.custom.paths"); const {lintProject} = t.context; - let res = await lintProject({ + const res = await lintProject({ rootDir: projectPath, filePaths: [], reportCoverage: true, messageDetails: true, }); - res = res.sort((a: {filePath: string}, b: {filePath: string}) => { - return a.filePath.localeCompare(b.filePath); - }); - - t.snapshot(res); + t.snapshot(preprocessLintResultsForSnapshot(res)); });