Skip to content

Commit

Permalink
feat: use own CancellationToken class (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed Feb 25, 2024
1 parent 22719ad commit 223b867
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 127 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: ["16.14", "16.x", "18.x", "20.x", "21.x"]
node-version: ["16.0", "16.x", "18.x", "20.x", "21.x"]
runs-on: ubuntu-latest

steps:
Expand All @@ -78,7 +78,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [windows-latest, macOS-latest]
os: [macOS-latest, windows-latest]
runs-on: ${{ matrix.os }}

steps:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@
},
"packageManager": "yarn@3.8.0",
"engines": {
"node": "^16.14 || 18.x || >=20.x"
"node": "16.x || 18.x || >=20.x"
}
}
7 changes: 4 additions & 3 deletions source/api/TSTyche.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { EventEmitter } from "#events";
import { SummaryReporter, ThoroughReporter } from "#reporters";
import { TaskRunner } from "#runner";
import type { StoreService } from "#store";
import { CancellationToken } from "#token";

export class TSTyche {
#abortController = new AbortController();
#cancellationToken = new CancellationToken();
#storeService: StoreService;
#taskRunner: TaskRunner;
static readonly version = "__version__";
Expand All @@ -32,7 +33,7 @@ export class TSTyche {
process.exitCode = 1;

if (this.resolvedConfig.failFast) {
this.#abortController.abort();
this.#cancellationToken.cancel();
}
}
}
Expand Down Expand Up @@ -65,7 +66,7 @@ export class TSTyche {
await this.#taskRunner.run(
this.#normalizePaths(testFiles),
this.resolvedConfig.target,
this.#abortController.signal,
this.#cancellationToken,
);
}
}
12 changes: 8 additions & 4 deletions source/cli/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { EventEmitter, type EventHandler } from "#events";
import { Logger } from "#logger";
import { addsPackageStepText, diagnosticText, formattedText, helpText } from "#output";
import { StoreService } from "#store";
import { CancellationToken } from "#token";

export class Cli {
#cancellationToken = new CancellationToken();
#logger: Logger;
#storeService: StoreService;

Expand All @@ -28,9 +30,10 @@ export class Cli {
for (const diagnostic of payload.diagnostics) {
switch (diagnostic.category) {
case DiagnosticCategory.Error:
process.exitCode = 1;

this.#cancellationToken.cancel();
this.#logger.writeError(diagnosticText(diagnostic));

process.exitCode = 1;
break;

case DiagnosticCategory.Warning:
Expand Down Expand Up @@ -86,13 +89,14 @@ export class Cli {

await configService.parseCommandLine(commandLineArguments);

if (process.exitCode === 1) {
if (this.#cancellationToken.isCancellationRequested) {
return;
}

await configService.readConfigFile();

if (process.exitCode === 1) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (this.#cancellationToken.isCancellationRequested) {
return;
}

Expand Down
27 changes: 7 additions & 20 deletions source/diagnostic/Diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,16 @@ export class Diagnostic {
static fromError(text: string | Array<string>, error: unknown): Diagnostic {
const messageText = Array.isArray(text) ? text : [text];

if (error instanceof Error) {
if (error.cause != null) {
messageText.push(this.#normalizeMessage(String(error.cause)));
if (error instanceof Error && error.stack != null) {
if (messageText.length > 1) {
messageText.push("");
}

messageText.push(this.#normalizeMessage(String(error.message)));
const stackLines = error.stack
.split("\n")
.map((line) => line.trimStart());

if (error.stack != null) {
const stackLines = error.stack
.split("\n")
.slice(1)
.map((line) => line.trimStart());

messageText.push(...stackLines);
}
messageText.push(...stackLines);
}

return Diagnostic.error(messageText);
Expand All @@ -84,14 +79,6 @@ export class Diagnostic {
return diagnostic.file != null && diagnostic.start != null && diagnostic.length != null;
}

static #normalizeMessage(text: string) {
if (text.endsWith(".")) {
return text;
}

return `${text}.`;
}

static warning(text: string | Array<string>, origin?: DiagnosticOrigin): Diagnostic {
return new Diagnostic(text, DiagnosticCategory.Warning, origin);
}
Expand Down
7 changes: 4 additions & 3 deletions source/runner/TaskRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ResolvedConfig } from "#config";
import { EventEmitter } from "#events";
import { Result, ResultManager, TargetResult } from "#result";
import type { StoreService } from "#store";
import type { CancellationToken } from "#token";
import { TestFileRunner } from "./TestFileRunner.js";

export class TaskRunner {
Expand All @@ -21,7 +22,7 @@ export class TaskRunner {
});
}

async run(testFiles: Array<URL>, target: Array<string>, signal?: AbortSignal): Promise<Result> {
async run(testFiles: Array<URL>, target: Array<string>, cancellationToken?: CancellationToken): Promise<Result> {
const result = new Result(this.resolvedConfig, testFiles);

EventEmitter.dispatch(["start", { result }]);
Expand All @@ -31,13 +32,13 @@ export class TaskRunner {

EventEmitter.dispatch(["target:start", { result: targetResult }]);

const compiler = await this.#storeService.load(versionTag, signal);
const compiler = await this.#storeService.load(versionTag, cancellationToken);

if (compiler) {
const testFileRunner = new TestFileRunner(this.resolvedConfig, compiler);

for (const testFile of testFiles) {
testFileRunner.run(testFile, signal);
testFileRunner.run(testFile, cancellationToken);
}
}

Expand Down
16 changes: 11 additions & 5 deletions source/runner/TestFileRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { EventEmitter } from "#events";
import { Expect } from "#expect";
import { ProjectService } from "#project";
import { FileResult } from "#result";
import type { CancellationToken } from "#token";
import { RunMode } from "./enums.js";
import { TestTreeWorker } from "./TestTreeWorker.js";

Expand All @@ -22,8 +23,8 @@ export class TestFileRunner {
this.#projectService = new ProjectService(compiler);
}

run(testFile: URL, signal?: AbortSignal): void {
if (signal?.aborted === true) {
run(testFile: URL, cancellationToken?: CancellationToken): void {
if (cancellationToken?.isCancellationRequested === true) {
return;
}

Expand All @@ -36,14 +37,19 @@ export class TestFileRunner {

EventEmitter.dispatch(["file:start", { result: fileResult }]);

this.#runFile(testFilePath, fileResult, position, signal);
this.#runFile(testFilePath, fileResult, position, cancellationToken);

EventEmitter.dispatch(["file:end", { result: fileResult }]);

this.#projectService.closeFile(testFilePath);
}

#runFile(testFilePath: string, fileResult: FileResult, position: number | undefined, signal?: AbortSignal) {
#runFile(
testFilePath: string,
fileResult: FileResult,
position: number | undefined,
cancellationToken?: CancellationToken,
) {
const languageService = this.#projectService.getLanguageService(testFilePath);

if (!languageService) {
Expand Down Expand Up @@ -105,10 +111,10 @@ export class TestFileRunner {
const expect = new Expect(this.compiler, typeChecker);

const testTreeWorker = new TestTreeWorker(this.resolvedConfig, this.compiler, expect, {
cancellationToken,
fileResult,
hasOnly: testTree.hasOnly,
position,
signal,
});

testTreeWorker.visit(testTree.members, RunMode.Normal, /* parentResult */ undefined);
Expand Down
9 changes: 5 additions & 4 deletions source/runner/TestTreeWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@ import { Diagnostic } from "#diagnostic";
import { EventEmitter } from "#events";
import type { Expect } from "#expect";
import { DescribeResult, ExpectResult, type FileResult, TestResult } from "#result";
import type { CancellationToken } from "#token";
import { RunMode } from "./enums.js";

interface TestFileWorkerOptions {
cancellationToken: CancellationToken | undefined;
fileResult: FileResult;
hasOnly: boolean;
position: number | undefined;
signal: AbortSignal | undefined;
}

export class TestTreeWorker {
#cancellationToken: CancellationToken | undefined;
#expect: Expect;
#fileResult: FileResult;
#hasOnly: boolean;
#position: number | undefined;
#signal: AbortSignal | undefined;

constructor(
readonly resolvedConfig: ResolvedConfig,
public compiler: typeof ts,
expect: Expect,
options: TestFileWorkerOptions,
) {
this.#cancellationToken = options.cancellationToken;
this.#expect = expect;
this.#fileResult = options.fileResult;
this.#hasOnly = options.hasOnly || resolvedConfig.only != null || options.position != null;
this.#position = options.position;
this.#signal = options.signal;
}

#resolveRunMode(mode: RunMode, member: TestMember): RunMode {
Expand Down Expand Up @@ -69,7 +70,7 @@ export class TestTreeWorker {
parentResult: DescribeResult | TestResult | undefined,
): void {
for (const member of members) {
if (this.#signal?.aborted === true) {
if (this.#cancellationToken?.isCancellationRequested === true) {
break;
}

Expand Down
5 changes: 3 additions & 2 deletions source/store/Lock.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { existsSync, rmSync, writeFileSync } from "node:fs";
import process from "node:process";
import type { CancellationToken } from "#token";

export interface IsLockedOptions {
cancellationToken?: CancellationToken | undefined;
onDiagnostic?: (diagnostic: string) => void;
signal?: AbortSignal | undefined;
timeout?: number;
}

Expand Down Expand Up @@ -39,7 +40,7 @@ export class Lock {
const waitStartTime = Date.now();

while (isLocked) {
if (options.signal?.aborted === true) {
if (options.cancellationToken?.isCancellationRequested === true) {
break;
}

Expand Down

0 comments on commit 223b867

Please sign in to comment.