-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtests.ts
151 lines (132 loc) · 4.48 KB
/
tests.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
import { Args, Flags } from "@oclif/core";
import {
findContractRecord,
getTemplates,
prepareTestFiles,
processTemplates,
} from "../../lib/index.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError, InputError } from "../../lib/errors.js";
import path from "node:path";
import { existsSync } from "node:fs";
import inquirer from "inquirer";
import { kebabCase, pascalCase } from "change-case";
import { TestType } from "../../index.js";
import { contractFromRecord, ensureArtifactsExist } from "../../lib/checks.js";
export class GenerateTests extends SwankyCommand<typeof GenerateTests> {
static description = "Generate test files for the specified contract";
static args = {
contractName: Args.string({
name: "contractName",
description: "Name of the contract",
}),
};
static flags = {
template: Flags.string({
options: getTemplates().contractTemplatesList,
}),
mocha: Flags.boolean({
default: false,
description: "Generate mocha test files",
}),
};
async run(): Promise<void> {
const { args, flags } = await this.parse(GenerateTests);
if (flags.mocha) {
if (!args.contractName) {
throw new InputError("The 'contractName' argument is required to generate mocha tests.");
}
await this.checkContract(args.contractName);
}
const testType: TestType = flags.mocha ? "mocha" : "e2e";
const testsFolderPath = path.resolve("tests");
const testPath = this.getTestPath(testType, testsFolderPath, args.contractName);
const templates = getTemplates();
const templateName = await this.resolveTemplateName(flags, templates.contractTemplatesList);
const overwrite = await this.checkOverwrite(testPath, testType, args.contractName);
if (!overwrite) return;
await this.generateTests(
testType,
templates.templatesPath,
process.cwd(),
args.contractName,
templateName
);
}
async checkContract(name: string) {
const contractRecord = findContractRecord(this.swankyConfig, name);
const contract = await contractFromRecord(contractRecord);
await ensureArtifactsExist(contract);
}
async checkOverwrite(
testPath: string,
testType: TestType,
contractName?: string
): Promise<boolean> {
if (!existsSync(testPath)) return true; // No need to overwrite
const message =
testType === "e2e"
? "Test helpers already exist. Overwrite?"
: `Mocha tests for ${contractName} already exist. Overwrite?`;
const { overwrite } = await inquirer.prompt({
type: "confirm",
name: "overwrite",
message,
default: false,
});
return overwrite;
}
getTestPath(testType: TestType, testsPath: string, contractName?: string): string {
if (testType === "e2e") {
return path.resolve(testsPath, "test_helpers");
} else if (testType === "mocha" && contractName) {
return path.resolve(testsPath, contractName, "index.test.ts");
} else {
throw new InputError("The 'contractName' argument is required to generate mocha tests.");
}
}
async resolveTemplateName(flags: any, templates: any): Promise<string | undefined> {
if (flags.mocha && !flags.template) {
if (!templates?.length) throw new ConfigError("Template list is empty!");
const response = await inquirer.prompt([
{
type: "list",
name: "template",
message: "Choose a contract template:",
choices: templates,
},
]);
return response.template;
}
return flags.template;
}
async generateTests(
testType: TestType,
templatesPath: string,
projectPath: string,
contractName?: string,
templateName?: string
): Promise<void> {
if (testType === "e2e") {
await this.spinner.runCommand(
() => prepareTestFiles("e2e", templatesPath, projectPath),
"Generating e2e test helpers"
);
} else {
await this.spinner.runCommand(
() => prepareTestFiles("mocha", templatesPath, projectPath, templateName, contractName),
`Generating tests for ${contractName} with mocha`
);
}
await this.spinner.runCommand(
() =>
processTemplates(projectPath, {
project_name: kebabCase(this.config.pjson.name),
swanky_version: this.config.pjson.version,
contract_name: contractName ?? "",
contract_name_pascal: contractName ? pascalCase(contractName) : "",
}),
"Processing templates"
);
}
}