-
Notifications
You must be signed in to change notification settings - Fork 30
/
test.ts
157 lines (133 loc) · 4.58 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
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
import "ts-mocha";
import { Flags, Args } from "@oclif/core";
import path from "node:path";
import { globby } from "globby";
import Mocha from "mocha";
import { emptyDir, pathExistsSync } from "fs-extra/esm";
import { Contract } from "../../lib/contract.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { FileError, ProcessError, TestError } from "../../lib/errors.js";
import { spawn } from "node:child_process";
import { findContractRecord, Spinner } from "../../lib/index.js";
import {
contractFromRecord,
ensureArtifactsExist,
ensureContractNameOrAllFlagIsSet,
ensureTypedContractExists,
} from "../../lib/checks.js";
declare global {
var contractTypesPath: string; // eslint-disable-line no-var
}
export class TestContract extends SwankyCommand<typeof TestContract> {
static description = "Run tests for a given contact";
static flags = {
all: Flags.boolean({
default: false,
char: "a",
description: "Run tests for all contracts",
}),
mocha: Flags.boolean({
default: false,
description: "Run tests with mocha",
}),
};
static args = {
contractName: Args.string({
name: "contractName",
default: "",
description: "Name of the contract to test",
}),
};
async run(): Promise<void> {
const { args, flags } = await this.parse(TestContract);
ensureContractNameOrAllFlagIsSet(args, flags);
const contractNames = flags.all
? Object.keys(this.swankyConfig.contracts)
: [args.contractName];
const spinner = new Spinner();
for (const contractName of contractNames) {
const contractRecord = findContractRecord(this.swankyConfig, contractName);
const contract = await contractFromRecord(contractRecord);
console.log(`Testing contract: ${contractName}`);
if (flags.mocha) {
await this.runMochaTests(contract);
} else {
await spinner.runCommand(
async () => {
return new Promise<string>((resolve, reject) => {
const compileArgs = [
"test",
"--features",
"e2e-tests",
"--manifest-path",
`contracts/${contractName}/Cargo.toml`,
"--release",
];
const compile = spawn("cargo", compileArgs);
this.logger.info(`Running e2e-tests command: [${JSON.stringify(compile.spawnargs)}]`);
let outputBuffer = "";
let errorBuffer = "";
compile.stdout.on("data", (data) => {
outputBuffer += data.toString();
spinner.ora.clear();
});
compile.stdout.pipe(process.stdout);
compile.stderr.on("data", (data) => {
errorBuffer += data;
});
compile.on("exit", (code) => {
if (code === 0) {
const regex = /test result: (.*)/;
const match = outputBuffer.match(regex);
if (match) {
this.logger.info(`Contract ${contractName} e2e-testing done.`);
resolve(match[1]);
}
} else {
reject(new ProcessError(errorBuffer));
}
});
});
},
`Testing ${contractName} contract`,
`${contractName} testing finished successfully`
);
}
}
}
async runMochaTests(contract: Contract): Promise<void> {
const testDir = path.resolve("tests", contract.name);
if (!pathExistsSync(testDir)) {
throw new FileError(`Test directory does not exist: ${testDir}`);
}
await ensureArtifactsExist(contract);
await ensureTypedContractExists(contract);
const reportDir = path.resolve(testDir, "testReports");
await emptyDir(reportDir);
const mocha = new Mocha({
timeout: 200000,
reporter: "mochawesome",
reporterOptions: {
reportDir,
quiet: true,
},
});
const testFiles = await globby(`${testDir}/*.test.ts`);
testFiles.forEach((file) => mocha.addFile(file));
global.contractTypesPath = path.resolve(testDir, "typedContract");
try {
await new Promise<void>((resolve, reject) => {
mocha.run((failures) => {
if (failures) {
reject(new Error(`Tests failed. See report: ${reportDir}`));
} else {
console.log(`All tests passed. See report: ${reportDir}`);
resolve();
}
});
});
} catch (error) {
throw new TestError("Mocha tests failed", { cause: error });
}
}
}