Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Fix lintFiles on Windows #49

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/detectors/typeChecker/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/detectors/typeChecker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/linter/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 14 additions & 2 deletions test/lib/linter/_linterHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LintResult[]>>;
}>;

test.before(async (t) => {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
33 changes: 13 additions & 20 deletions test/lib/linter/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -11,7 +16,7 @@ const fixturesProjectsPath = path.join(fixturesBasePath, "projects");

const test = anyTest as TestFn<{
sinon: sinonGlobal.SinonSandbox;
lintProject: SinonStub;
lintProject: SinonStub<[LinterOptions], Promise<LintResult[]>>;
}>;

test.before(async (t) => {
Expand All @@ -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) => {
Expand All @@ -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));
});