-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubspecTestRunner.ts
More file actions
73 lines (62 loc) · 2.25 KB
/
Copy pathpubspecTestRunner.ts
File metadata and controls
73 lines (62 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import fs from 'fs/promises';
import path from 'path';
import { spawn } from 'node:child_process';
import { type DartInvocation } from './dartToolchain.js';
import { ui } from './ui.js';
const ENSEMBLE_GIT_URL = 'https://github.com/EnsembleUI/ensemble.git';
const ENSEMBLE_TEST_RUNNER_REF = 'support-test-cases';
const ENSEMBLE_TEST_RUNNER_PATH = 'packages/ensemble_test_runner';
const TEST_RUNNER_DEV_DEP_BLOCK = ` ensemble_test_runner:
git:
url: ${ENSEMBLE_GIT_URL}
ref: ${ENSEMBLE_TEST_RUNNER_REF}
path: ${ENSEMBLE_TEST_RUNNER_PATH}
`;
function hasEnsembleTestRunnerDep(pubspecContent: string): boolean {
return /^\s*ensemble_test_runner\s*:/m.test(pubspecContent);
}
export function spliceTestRunnerDevDependency(pubspecContent: string): string {
if (hasEnsembleTestRunnerDep(pubspecContent)) return pubspecContent;
const match = pubspecContent.match(/^dev_dependencies:\s*$/m);
if (!match || match.index === undefined) {
throw new Error('pubspec.yaml has no dev_dependencies section.');
}
const insertAt = match.index + match[0].length;
return `${pubspecContent.slice(0, insertAt)}\n${TEST_RUNNER_DEV_DEP_BLOCK}${pubspecContent.slice(insertAt)}`;
}
export async function runDartWithExitCode(
dart: DartInvocation,
args: string[],
cwd: string
): Promise<number> {
return new Promise((resolve, reject) => {
const child = spawn(dart.command, [...dart.prefixArgs, ...args], { cwd, stdio: 'inherit' });
child.on('error', reject);
child.on('close', (code) => resolve(code ?? 1));
});
}
export async function withTemporaryTestRunnerDep<T>(
projectRoot: string,
fn: () => Promise<T>
): Promise<T> {
const pubspecPath = path.join(projectRoot, 'pubspec.yaml');
const original = await fs.readFile(pubspecPath, 'utf8');
let modified = false;
try {
if (!hasEnsembleTestRunnerDep(original)) {
await fs.writeFile(pubspecPath, spliceTestRunnerDevDependency(original), 'utf8');
modified = true;
}
return await fn();
} finally {
if (modified) {
try {
await fs.writeFile(pubspecPath, original, 'utf8');
} catch (error) {
ui.warn(
`Failed to restore pubspec.yaml after tests: ${error instanceof Error ? error.message : String(error)}`
);
}
}
}
}