/
test.ts
65 lines (56 loc) · 1.92 KB
/
test.ts
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
import { Command, flags } from '@oclif/command';
import { execSync } from 'child_process';
import { join } from 'path';
import { existsSync } from 'fs';
import runCommand from '../lib/runCommand';
import CLI from '../CLI';
/**
* Runs unit tests for a contract directory.
* Examples:
* $ wasmknife test counter
* $ wasmknife test counter --no-fail-fast
*/
export default class Test extends Command {
// Specify description to be displayed upon help command execution.
static description = 'Runs unit tests for a contract directory.';
// Specify examples included upon help test command execution.
static examples = [
'$ wasmknife test counter',
'$ wasmknife test counter --no-fail-fast',
];
// Initialize args to be given after test command.
static args = [{ name: 'contract-name', required: true }];
// Initialize flags, prefixed with '--' in a terminal command, to be given after args.
static flags = {
'no-fail-fast': flags.boolean({ description: 'Run all tests regardless of failure.' }),
};
async run() {
// Extract args and flags specified in executed test command.
const { args, flags } = this.parse(Test);
// Command execution path.
const execPath = join('contracts', args['contract-name']);
// Command to be performed.
const command = () => {
process.chdir(execPath);
execSync(
`cargo test ${flags['no-fail-fast'] ? '--no-fail-fast' : ''}`,
{ stdio: 'inherit' },
);
};
// Error check to be performed upon each backtrack iteration.
const errorCheck = () => {
if (existsSync('contracts') && !existsSync(execPath)) {
CLI.error(
`Contract "${args['contract-name']}" not available in "contracts/" directory.`,
'Contract Unavailable',
);
}
};
// Attempt to execute command while backtracking through file tree.
await runCommand(
execPath,
command,
errorCheck,
);
}
}