Skip to content

Commit

Permalink
feat: Generate command implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Feb 20, 2024
1 parent a692fef commit 8fd7565
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Args, Flags } from "@oclif/core";
import { getTemplates, prepareTestFiles, processTemplates } from "../../../lib/index.js";
import { Contract } from "../../../lib/contract.js";
import { SwankyCommand } from "../../../lib/swankyCommand.js";
import { ConfigError, FileError } from "../../../lib/errors.js";
import { getTemplates, prepareTestFiles, processTemplates } from "../../lib/index.js";
import { Contract } from "../../lib/contract.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError, FileError, InputError } from "../../lib/errors.js";
import path from "node:path";
import { existsSync } from "node:fs";
import inquirer from "inquirer";
import { paramCase, pascalCase } from "change-case";
import { TestType } from "../../../index.js";
import { TestType } from "../../index.js";

export class GenerateTests extends SwankyCommand<typeof GenerateTests> {
static description = "Generate test files for the specified contract";

static args = {
contractName: Args.string({
name: "contractName",
required: true,
required: false,
description: "Name of the contract",
}),
};
Expand All @@ -33,27 +33,12 @@ export class GenerateTests extends SwankyCommand<typeof GenerateTests> {
async run(): Promise<void> {
const { args, flags } = await this.parse(GenerateTests);

const contractRecord = this.swankyConfig.contracts[args.contractName];
if (!contractRecord) {
throw new ConfigError(
`Cannot find a contract named ${args.contractName} in swanky.config.json`
);
}

const contract = new Contract(contractRecord);

if (!(await contract.pathExists())) {
throw new FileError(
`Path to contract ${args.contractName} does not exist: ${contract.contractPath}`
);
}

const artifactsCheck = await contract.artifactsExist();
if (flags.mocha) {
if (!args.contractName) {
throw new InputError("The 'contractName' argument is required to generate mocha tests.");
}

if (!artifactsCheck.result) {
throw new FileError(
`No artifact file found at path: ${artifactsCheck.missingPaths.toString()}`
);
await this.checkContract(args.contractName)
}

const testType: TestType = flags.mocha ? "mocha" : "e2e";
Expand All @@ -63,7 +48,7 @@ export class GenerateTests extends SwankyCommand<typeof GenerateTests> {
const templates = getTemplates();
const templateName = await this.resolveTemplateName(flags, templates.contractTemplatesList);

const overwrite = await this.checkOverwrite(testPath, args.contractName, testType);
const overwrite = await this.checkOverwrite(testPath, testType, args.contractName);
if (!overwrite) return;

await this.generateTests(
Expand All @@ -75,10 +60,33 @@ export class GenerateTests extends SwankyCommand<typeof GenerateTests> {
);
}

async checkContract(name: string) {
const contractRecord = this.swankyConfig.contracts[name];
if (!contractRecord) {
throw new ConfigError(
`Cannot find a contract named ${name} in swanky.config.json`
);
}

const contract = new Contract(contractRecord);
if (!(await contract.pathExists())) {
throw new FileError(
`Path to contract ${name} does not exist: ${contract.contractPath}`
);
}

const artifactsCheck = await contract.artifactsExist();
if (!artifactsCheck.result) {
throw new FileError(
`No artifact file found at path: ${artifactsCheck.missingPaths.toString()}`
);
}
}

async checkOverwrite(
testPath: string,
contractName: string,
testType: TestType
testType: TestType,
contractName?: string
): Promise<boolean> {
if (!existsSync(testPath)) return true; // No need to overwrite
const message =
Expand All @@ -96,21 +104,27 @@ export class GenerateTests extends SwankyCommand<typeof GenerateTests> {
return overwrite;
}

getTestPath(testType: TestType, testsPath: string, contractName: string): string {
return testType === "e2e"
? path.resolve(testsPath, "test_helpers")
: path.resolve(testsPath, contractName, "index.test.ts");
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,
}]);
const response = await inquirer.prompt([
{
type: "list",
name: "template",
message: "Choose a contract template:",
choices: templates,
},
]);
return response.template;
}
return flags.template;
Expand All @@ -120,7 +134,7 @@ export class GenerateTests extends SwankyCommand<typeof GenerateTests> {
testType: TestType,
templatesPath: string,
projectPath: string,
contractName: string,
contractName?: string,
templateName?: string
): Promise<void> {
if (testType === "e2e") {
Expand All @@ -139,8 +153,8 @@ export class GenerateTests extends SwankyCommand<typeof GenerateTests> {
processTemplates(projectPath, {
project_name: paramCase(this.config.pjson.name),
swanky_version: this.config.pjson.version,
contract_name: contractName,
contract_name_pascal: pascalCase(contractName),
contract_name: contractName ?? "",
contract_name_pascal: contractName ? pascalCase(contractName) : "",
}),
"Processing templates"
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Args } from "@oclif/core";
import { generateTypes } from "../../../lib/index.js";
import { Contract } from "../../../lib/contract.js";
import { SwankyCommand } from "../../../lib/swankyCommand.js";
import { ConfigError, FileError } from "../../../lib/errors.js";
import { generateTypes } from "../../lib/index.js";
import { Contract } from "../../lib/contract.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError, FileError } from "../../lib/errors.js";

export class GenerateTypes extends SwankyCommand<typeof GenerateTypes> {
static description = "Generate types from compiled contract metadata";
Expand Down

0 comments on commit 8fd7565

Please sign in to comment.