@travetto/test 7.0.0-rc.2 → 7.0.0-rc.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,108 +0,0 @@
1
- import { spawn } from 'node:child_process';
2
- import { createReadStream } from 'node:fs';
3
- import fs from 'node:fs/promises';
4
- import readline from 'node:readline/promises';
5
- import path from 'node:path';
6
-
7
- import { Env, ExecUtil, ShutdownManager, Util, RuntimeIndex, Runtime } from '@travetto/runtime';
8
- import { TestConfig, TestRun } from '../model/test.ts';
9
-
10
- /**
11
- * Simple Test Utilities
12
- */
13
- export class RunnerUtil {
14
- /**
15
- * Add 50 ms to the shutdown to allow for buffers to output properly
16
- */
17
- static registerCleanup(scope: string): void {
18
- ShutdownManager.onGracefulShutdown(() => Util.blockingTimeout(50), `test.${scope}.bufferOutput`);
19
- }
20
-
21
- /**
22
- * Determine if a given file path is a valid test file
23
- */
24
- static async isTestFile(file: string): Promise<boolean> {
25
- const reader = readline.createInterface({ input: createReadStream(file) });
26
- const state = { imp: false, suite: false };
27
- for await (const line of reader) {
28
- state.imp ||= line.includes('@travetto/test');
29
- state.suite ||= line.includes('Suite'); // Decorator or name
30
- if (state.imp && state.suite) {
31
- reader.close();
32
- return true;
33
- }
34
- }
35
- return false;
36
- }
37
-
38
- /**
39
- * Find all valid test files given the globs
40
- */
41
- static async* getTestImports(globs?: string[]): AsyncIterable<string> {
42
- const all = RuntimeIndex.find({
43
- module: mod => mod.roles.includes('test') || mod.roles.includes('std'),
44
- folder: folder => folder === 'test',
45
- file: file => file.role === 'test'
46
- });
47
-
48
- // Collect globs
49
- if (globs?.length) {
50
- const allFiles = new Map(all.map(file => [file.sourceFile, file]));
51
- for await (const item of fs.glob(globs)) {
52
- const source = Runtime.workspaceRelative(path.resolve(item));
53
- const match = allFiles.get(source);
54
- if (match && await this.isTestFile(match.sourceFile)) {
55
- yield match.import;
56
- }
57
- }
58
- } else {
59
- for await (const match of all) {
60
- if (await this.isTestFile(match.sourceFile)) {
61
- yield match.import;
62
- }
63
- }
64
- }
65
- }
66
-
67
- /**
68
- * Get count of tests for a given set of globs
69
- * @param globs
70
- * @returns
71
- */
72
- static async getTestDigest(globs: string[] = ['**/*.ts'], tags?: string[]): Promise<TestConfig[]> {
73
- const countRes = await ExecUtil.getResult(
74
- spawn('npx', ['trv', 'test:digest', '-o', 'json', ...globs], {
75
- env: { ...process.env, ...Env.FORCE_COLOR.export(0), ...Env.NO_COLOR.export(true) }
76
- }),
77
- { catch: true }
78
- );
79
- if (!countRes.valid) {
80
- throw new Error(countRes.stderr);
81
- }
82
-
83
- const testFilter = tags?.length ?
84
- Util.allowDeny<string, [TestConfig]>(
85
- tags,
86
- rule => rule,
87
- (rule, core) => core.tags?.includes(rule) ?? false
88
- ) :
89
- ((): boolean => true);
90
-
91
- const parsed: TestConfig[] = countRes.valid ? JSON.parse(countRes.stdout) : [];
92
- return parsed.filter(testFilter);
93
- }
94
-
95
- /**
96
- * Get run events
97
- */
98
- static getTestRuns(tests: TestConfig[]): TestRun[] {
99
- const events = tests.reduce((runs, test) => {
100
- if (!runs.has(test.classId)) {
101
- runs.set(test.classId, { import: test.import, classId: test.classId, methodNames: [], runId: Util.uuid() });
102
- }
103
- runs.get(test.classId)!.methodNames!.push(test.methodName);
104
- return runs;
105
- }, new Map<string, TestRun>());
106
- return [...events.values()];
107
- }
108
- }