-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest.ts
More file actions
419 lines (352 loc) · 11.4 KB
/
Copy pathtest.ts
File metadata and controls
419 lines (352 loc) · 11.4 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import process from 'node:process';
import {spawn} from 'node:child_process';
import path from 'node:path';
import fs from 'node:fs';
import os from 'node:os';
import {PassThrough} from 'node:stream';
import chalk from 'chalk';
import {globSync} from 'glob';
import chokidar from 'chokidar';
import debounce from 'debounce';
import {SemVer} from 'semver';
import {ActorMethod} from '@icp-sdk/core/agent';
import {sources} from '../sources.js';
import {getRootDir, readConfig} from '../../mops.js';
import {parallel} from '../../parallel.js';
import {MMF1} from './mmf1.js';
import {absToRel, pipeMMF, pipeStderrToMMF, pipeStdoutToMMF} from './utils.js';
import {Reporter} from './reporters/reporter.js';
import {VerboseReporter} from './reporters/verbose-reporter.js';
import {FilesReporter} from './reporters/files-reporter.js';
import {CompactReporter} from './reporters/compact-reporter.js';
import {SilentReporter} from './reporters/silent-reporter.js';
import {toolchain} from '../toolchain/index.js';
import {Replica} from '../replica.js';
import {TestMode} from '../../types.js';
import {getDfxVersion} from '../../helpers/get-dfx-version.js';
let ignore = [
'**/node_modules/**',
'**/.mops/**',
'**/.vessel/**',
'**/.git/**',
];
let globConfig = {
nocase: true,
ignore: ignore,
};
type ReporterName = 'verbose' | 'files' | 'compact' | 'silent';
type ReplicaName = 'dfx' | 'pocket-ic' | 'dfx-pocket-ic';
type TestOptions = {
watch : boolean;
reporter : ReporterName;
mode : TestMode;
replica : ReplicaName;
verbose : boolean;
};
let replica = new Replica();
let replicaStartPromise : Promise<void> | undefined;
async function startReplicaOnce(replica : Replica, type : ReplicaName) {
if (!replicaStartPromise) {
replicaStartPromise = new Promise((resolve) => {
replica.start({type, silent: true}).then(resolve);
});
}
return replicaStartPromise;
}
export async function test(filter = '', options : Partial<TestOptions> = {}) {
let config = readConfig();
let rootDir = getRootDir();
let replicaType = options.replica ?? (config.toolchain?.['pocket-ic'] ? 'pocket-ic' : 'dfx' as ReplicaName);
if (replicaType === 'pocket-ic' && !config.toolchain?.['pocket-ic']) {
let dfxVersion = getDfxVersion();
if (!dfxVersion || new SemVer(dfxVersion).compare('0.24.1') < 0) {
console.log(chalk.red('Please update dfx to the version >=0.24.1 or specify pocket-ic version in mops.toml'));
process.exit(1);
}
else {
replicaType = 'dfx-pocket-ic';
}
}
replica.type = replicaType;
replica.verbose = !!options.verbose;
if (options.watch) {
replica.ttl = 60 * 15; // 15 minutes
let sigint = false;
process.on('SIGINT', () => {
if (sigint) {
console.log('Force exit');
process.exit(0);
}
sigint = true;
if (replicaStartPromise) {
console.log('Stopping replica...');
replica.stop(true).then(() => {
process.exit(0);
});
}
else {
process.exit(0);
}
});
// todo: run only changed for *.test.mo?
// todo: run all for *.mo?
let curRun = Promise.resolve(true);
let controller = new AbortController();
let run = debounce(async () => {
controller.abort();
await curRun;
console.clear();
process.stdout.write('\x1Bc');
controller = new AbortController();
curRun = runAll(options.reporter, filter, options.mode, replicaType, true, controller.signal);
await curRun;
console.log('-'.repeat(50));
console.log('Waiting for file changes...');
console.log(chalk.gray((`Press ${chalk.gray('Ctrl+C')} to exit.`)));
}, 200);
let watcher = chokidar.watch([
path.join(rootDir, '**/*.mo'),
path.join(rootDir, 'mops.toml'),
], {
ignored: ignore,
ignoreInitial: true,
});
watcher.on('all', () => {
run();
});
run();
}
else {
let passed = await runAll(options.reporter, filter, options.mode, replicaType);
if (!passed) {
process.exit(1);
}
}
}
let mocPath = '';
let wasmtimePath = '';
async function runAll(reporterName : ReporterName | undefined, filter = '', mode : TestMode = 'interpreter', replicaType : ReplicaName, watch = false, signal ?: AbortSignal) : Promise<boolean> {
let done = await testWithReporter(reporterName, filter, mode, replicaType, watch, signal);
return done;
}
export async function testWithReporter(reporterName : ReporterName | Reporter | undefined, filter = '', defaultMode : TestMode = 'interpreter', replicaType : ReplicaName, watch = false, signal ?: AbortSignal) : Promise<boolean> {
let rootDir = getRootDir();
let files : string[] = [];
let libFiles = globSync('**/test?(s)/lib.mo', globConfig);
if (libFiles[0]) {
files = [libFiles[0]];
}
else {
let globStr = '**/test?(s)/**/*.test.mo';
if (filter) {
globStr = `**/test?(s)/**/*${filter}*.mo`;
}
files = globSync(path.join(rootDir, globStr), globConfig);
}
if (!files.length) {
if (filter) {
console.log(`No test files found for filter '${filter}'`);
return false;
}
console.log('No test files found');
console.log('Put your tests in \'test\' directory in *.test.mo files');
return false;
}
let reporter : Reporter;
if (!reporterName || typeof reporterName === 'string') {
if (!reporterName) {
reporterName = files.length > 1 ? 'files' : 'verbose';
}
if (reporterName == 'compact') {
reporter = new CompactReporter;
}
else if (reporterName == 'files') {
reporter = new FilesReporter;
}
else if (reporterName == 'silent') {
reporter = new SilentReporter;
}
else {
reporter = new VerboseReporter;
}
}
else {
reporter = reporterName;
}
reporter.addFiles(files);
let config = readConfig();
let sourcesArr = await sources();
if (!mocPath) {
mocPath = await toolchain.bin('moc', {fallback: true});
}
let testTempDir = path.join(getRootDir(), '.mops/.test/');
replica.dir = testTempDir;
fs.rmSync(testTempDir, {recursive: true, force: true});
fs.mkdirSync(testTempDir, {recursive: true});
let filesWithMode = files.map((file) => {
let lines = fs.readFileSync(file, 'utf8').split('\n');
let mode = defaultMode;
if (lines.includes('// @testmode wasi')) {
mode = 'wasi';
}
else if (lines.includes('// @testmode replica') || lines.find(line => line.match(/^(persistent )?actor( class)?/))) {
mode = 'replica';
}
return {file, mode};
});
let hasWasiTests = filesWithMode.some(({mode}) => mode === 'wasi');
let hasReplicaTests = filesWithMode.some(({mode}) => mode === 'replica');
// prepare wasmtime path
if (hasWasiTests && !wasmtimePath) {
// ensure wasmtime is installed or specified in config
if (config.toolchain?.wasmtime) {
wasmtimePath = await toolchain.bin('wasmtime');
}
// fallback wasmtime to global binary if not specified in config (legacy)
else {
wasmtimePath = 'wasmtime';
console.log(chalk.yellow('Warning:'), 'Wasmtime is not specified in config. Using global binary "wasmtime". This will be removed in the future.');
console.log(`Run ${chalk.green('mops toolchain use wasmtime')} or add ${chalk.green('wasmtime = "<version>"')} in mops.toml to avoid breaking changes with future versions of mops.`);
}
}
let runTestFile = async ({file, mode} : {file : string, mode : TestMode}) => {
if (signal?.aborted) {
return;
}
// print logs immediately for replica tests because we run them one-by-one
let mmf = new MMF1(mode === 'replica' ? 'print' : 'store', absToRel(file));
let promise = new Promise<void>((resolve) => {
let mocArgs = ['--hide-warnings', '--error-detail=2', ...sourcesArr.join(' ').split(' '), file].filter(x => x);
// interpret
if (mode === 'interpreter') {
let proc = spawn(mocPath, ['-r', '-ref-system-api', ...mocArgs], {signal});
proc.addListener('error', (error : any) => {
if (error?.code === 'ABORT_ERR') {
return;
}
throw error;
});
pipeMMF(proc, mmf).then(resolve);
}
// build and run wasm
else if (mode === 'wasi') {
let wasmFile = `${path.join(testTempDir, path.parse(file).name)}.wasm`;
// build
let buildProc = spawn(mocPath, [`-o=${wasmFile}`, '-wasi-system-api', ...mocArgs], {signal});
buildProc.addListener('error', (error : any) => {
if (error?.code === 'ABORT_ERR') {
return;
}
throw error;
});
pipeMMF(buildProc, mmf).then(async () => {
if (mmf.failed > 0) {
return;
}
// run
let wasmtimeArgs = [];
if (config.toolchain?.wasmtime && config.toolchain?.wasmtime >= '14.0.0') {
wasmtimeArgs = [
'-S', 'preview2=n',
'-C', 'cache=n',
'-W', 'bulk-memory',
'-W', 'multi-memory',
'-W', 'memory64',
'-W', 'max-wasm-stack=4000000',
'-W', 'nan-canonicalization=y',
wasmFile,
];
}
else {
console.error(chalk.red('Minimum wasmtime version is 14.0.0. Please update wasmtime to the latest version'));
process.exit(1);
}
let proc = spawn(wasmtimePath, wasmtimeArgs, {signal});
proc.addListener('error', (error : any) => {
if (error?.code === 'ABORT_ERR') {
return;
}
throw error;
});
await pipeMMF(proc, mmf);
}).finally(() => {
fs.rmSync(wasmFile, {force: true});
}).then(resolve);
}
// build and execute in replica
else if (mode === 'replica') {
let wasmFile = `${path.join(testTempDir, path.parse(file).name)}.wasm`;
// build
let buildProc = spawn(mocPath, [`-o=${wasmFile}`, ...mocArgs], {signal});
buildProc.addListener('error', (error : any) => {
if (error?.code === 'ABORT_ERR') {
return;
}
throw error;
});
pipeMMF(buildProc, mmf).then(async () => {
if (mmf.failed > 0) {
return;
}
await startReplicaOnce(replica, replicaType);
if (signal?.aborted) {
return;
}
let canisterName = path.parse(file).name;
let idlFactory = ({IDL} : any) => {
return IDL.Service({'runTests': IDL.Func([], [], [])});
};
interface _SERVICE {'runTests' : ActorMethod<[], undefined>;}
let canister = await replica.deploy(canisterName, wasmFile, idlFactory, undefined, signal);
if (signal?.aborted || !canister) {
return;
}
pipeStdoutToMMF(canister.stream, mmf);
let actor = await replica.getActor(canisterName) as _SERVICE;
try {
if (globalThis.mopsReplicaTestRunning) {
await new Promise<void>((resolve) => {
let timerId = setInterval(() => {
if (!globalThis.mopsReplicaTestRunning) {
resolve();
clearInterval(timerId);
}
}, Math.random() * 1000 | 0);
});
}
if (signal?.aborted) {
return;
}
globalThis.mopsReplicaTestRunning = true;
await actor.runTests();
globalThis.mopsReplicaTestRunning = false;
mmf.pass();
}
catch (e : any) {
let stderrStream = new PassThrough();
pipeStderrToMMF(stderrStream, mmf, path.dirname(file));
stderrStream.write(e.message);
}
}).finally(async () => {
globalThis.mopsReplicaTestRunning = false;
fs.rmSync(wasmFile, {force: true});
}).then(resolve);
}
});
if (signal?.aborted) {
return;
}
reporter.addRun(file, mmf, promise, mode);
await promise;
};
await parallel(os.cpus().length, filesWithMode.filter(({mode}) => mode !== 'replica'), runTestFile);
await parallel(1, filesWithMode.filter(({mode}) => mode === 'replica'), runTestFile);
if (hasReplicaTests && !watch) {
await replica.stop();
fs.rmSync(testTempDir, {recursive: true, force: true});
}
if (signal?.aborted) {
return false;
}
return reporter.done();
}