Skip to content

Commit

Permalink
feat: added forge build and logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ITZSHOAIB committed Sep 10, 2024
1 parent ecba385 commit d824f51
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 83 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ Chukti uses [Anvil](https://book.getfoundry.sh/reference/anvil/) and [Forge](htt
anvil --version
```

Once Foundry is installed, you can proceed with initializing a new project using Chukti.
Once Foundry is installed, you can proceed with initializing a new project using Chukti.

For Windows machines follow this: https://book.getfoundry.sh/getting-started/installation#prerequisites

### Initializing a New Project

Expand Down Expand Up @@ -95,4 +97,12 @@ Check out the sample-projects/anvil-cucumber directory for a complete example pr
Special thanks to the developers and contributors of Viem, Forge, Anvil and Cucumber for their amazing tools and libraries.
## License
Chukti is licensed under the MIT License. If you have any suggestions, bug reports, or feature requests, feel free to open an issue or submit a pull request. Your feedback is always welcome!
## Contributing
We welcome contributions from the community! If you have any ideas or suggestions, please open an issue or a pull request.
Happy Testing! 🚀
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@
},
"files": [
"dist",
"sample-projects",
"templates"
"sample-projects"
],
"keywords": [
"hardhat",
"Smart contract test",
"viem",
"cli",
"deploy",
"test",
"smart contract"
"smart contract",
"Smart contract cucumber"
]
}
20 changes: 18 additions & 2 deletions sample-projects/anvil-cucumber/contracts/Counter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@ pragma solidity ^0.8.13;
contract Counter {
uint256 public number;

function setNumber(uint256 newNumber) public {
number = newNumber;
constructor(uint256 _number) {
number = _number;
}

function setNumber(uint256 _number) public {
number = _number;
}

function increment() public {
number++;
}

function decrement() public {
number--;
}

function resetNumber() public {
number = 0;
}

function incrementBy(uint256 _value) public {
number += _value;
}
}
2 changes: 1 addition & 1 deletion sample-projects/anvil-cucumber/features/example.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feature: Manage Posts on JSONPlaceholder API

Scenario: Retrieve a list of posts
Given a deployed "Lock" contract with "1893456000" arguments and "0.001" Ether
Given a deployed "Counter" contract with "[10]" arguments and "0" Ether
2 changes: 1 addition & 1 deletion sample-projects/anvil-cucumber/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "chukti-project",
"type": "module",
"scripts": {
"test:cucumber": "cucumber-js"
"test": "chukti test"
},
"devDependencies": {
"@cucumber/cucumber": "^11.0.0",
Expand Down
33 changes: 25 additions & 8 deletions src/cucumber/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { spawn } from "child_process";
import { execSync, spawn } from "child_process";
import commandExists from "command-exists";
import { log } from "../internal/utils/logger.js";

let anvilProcess: any;

export const beforeAll = async () => {
console.log("Starting test environment...");
log("info", "Starting test environment...");

const forgeExist = commandExists.sync("forge");
if (!forgeExist) {
log(
"error",
"Forge is not installed. Please install it first. Please refer to the documentation for more information.\n\nhttps://github.com/ITZSHOAIB/chukti#readme"
);
}

execSync("forge build", { stdio: "inherit" });

const isAnvilExist = commandExists.sync("anvil");
if (!isAnvilExist) {
console.error("Anvil is not installed. Please install it first.");
log(
"error",
"Anvil is not installed. Please install it first. Please refer to the documentation for more information.\n\nhttps://github.com/ITZSHOAIB/chukti#readme"
);
process.exit(1);
}

Expand All @@ -32,11 +46,14 @@ export const beforeAll = async () => {

const onError = (data: any) => {
stderr += data.toString();
console.error(`Error while starting Local blockchain anvil:
log(
"error",
`Error while starting Local blockchain anvil:
stdout: ${stdio}
stderr: ${stderr}`);
stderr: ${stderr}`
);
if (retries > 0) {
console.log(`Retrying to start anvil... (${retries} retries left)`);
log("info", `Retrying to start anvil... (${retries} retries left)`);
setTimeout(
() =>
startAnvil(retries - 1, delay * 2)
Expand All @@ -51,7 +68,7 @@ export const beforeAll = async () => {
};

const onClose = () => {
console.log(`Local blockchain exited`);
log("info", "Local blockchain exited");
};

const cleanup = () => {
Expand All @@ -71,5 +88,5 @@ export const beforeAll = async () => {

export const afterAll = () => {
anvilProcess.kill();
console.log("Local blockchain stopped");
log("info", "Local blockchain stopped");
};
25 changes: 11 additions & 14 deletions src/internal/cli/handlers/init.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import kleur from "kleur";
import path from "path";
import fs from "fs-extra";
import { checkChuktiProject } from "../utils/helpers.js";
import { log } from "../../utils/logger.js";
import { execSync } from "child_process";

export const initProject = async () => {
try {
console.log(
kleur.cyan("🚀 Initializing a new Chukti project with Cucumber, Anvil")
);
log("info", "🚀 Initializing a new Chukti project with Cucumber, Anvil");

checkChuktiProject({ shouldExist: false });
await proceedWithInitialization();
} catch (error) {
console.error(
kleur.red(`❌ Error during initialization: ${(error as Error).message}`)
);
log("error", `❌ Error during initialization: ${(error as Error).message}`);
process.exit(1);
}
};
Expand All @@ -27,15 +24,15 @@ const proceedWithInitialization = async () => {
);

// Copy the template files to the current directory
console.log(kleur.cyan("📁 Copying template files..."));
log("info", "📁 Copying template files...");
fs.copySync(templateDir, currentDir);

console.log(kleur.green("✅ Project initialized successfully"));
log("sucess", "✅ Project initialized successfully");

// Install the dependencies
console.log(kleur.yellow("📦 Installing dependencies..."));
const { execa } = await import("execa");
await execa("npm", ["install"], { stdio: "inherit" });
console.log(kleur.yellow("📦 Installing chukti..."));
await execa("npm", ["install", "-D", "chukti"], { stdio: "inherit" });
log("info", "📦 Installing dependencies...");
execSync("npm install", { stdio: "inherit" });
log("info", "📦 Installing chukti...");
execSync("npm install -D chukti", { stdio: "inherit" });
log("sucess", "✅ Dependencies installed successfully");
};
18 changes: 6 additions & 12 deletions src/internal/cli/handlers/runTests.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import kleur from "kleur";
// import { execa } from "execa";
import { execSync } from "child_process";
import { log } from "../../utils/logger.js";

export const runTests = async () => {
try {
console.log(kleur.cyan("🚀 Running Cucumber tests..."));
// console.log("🚀 Running Cucumber tests...");
const execa = (await import("execa")).execa;
await execa("npx", ["cucumber-js"], { stdio: "inherit" });
// execSync("npx cucumber-js", { stdio: "inherit" });
console.log(kleur.green("✅ Tests completed successfully"));
// console.log("✅ Tests completed successfully");
log("info", "🚀 Running Cucumber tests...");
execSync("npx cucumber-js", { stdio: "inherit" });
log("sucess", "✅ Tests completed successfully");
} catch (error) {
console.error(
kleur.red(`❌ Error during tests: ${(error as Error).message}`)
);
log("error", `❌ Error during tests: ${(error as Error).message}`);
process.exit(1);
}
};
25 changes: 11 additions & 14 deletions src/internal/cli/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs-extra";
import kleur from "kleur";
import path from "path";
import { log } from "../../utils/logger.js";

export const checkChuktiProject = ({
shouldExist,
Expand All @@ -12,23 +12,20 @@ export const checkChuktiProject = ({
const projectExists = fs.existsSync(chuktiConfigPath);

if (shouldExist && !projectExists) {
console.error(
kleur.red(
"❌ Error: A Chukti project does not exist in this directory.\n\n"
) +
kleur.yellow(
`\tPlease run ${kleur
.magenta()
.bold("`chukti init`")} to initialize a new project.`
)
log(
"error",
"❌ Error: A Chukti project does not exist in this directory.\n\nPlease run `chukti init` to initialize a new project."
);

process.exit(1);
}

if (!shouldExist && projectExists) {
console.error(
kleur.red("❌ Error: A Chukti project already exists in this directory.")
log(
"error",
"❌ Error: A Chukti project already exists in this directory."
);

process.exit(1);
}
};
Expand All @@ -38,12 +35,12 @@ export const checkEveryPathExists = (paths: string[]) => {

for (const p of paths) {
if (!fs.existsSync(p)) {
errorMessages.push(`❌ Error: ${kleur.cyan(p)} does not exist.`);
errorMessages.push(`❌ Error: ${p} does not exist.`);
}
}

if (errorMessages.length > 0) {
console.error(kleur.red(errorMessages.join("\n")));
log("error", errorMessages.join("\n"));
process.exit(1);
}
};
22 changes: 22 additions & 0 deletions src/internal/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import kleur from "kleur";

type LogType = "sucess" | "error" | "warning" | "info";

export const log = (type: LogType, message: string) => {
switch (type) {
case "sucess":
console.log(kleur.green(message));
break;
case "error":
console.error(kleur.red(message));
break;
case "warning":
console.warn(kleur.yellow(message));
break;
case "info":
console.info(kleur.cyan(message));
break;
default:
console.log(message);
}
};
25 changes: 0 additions & 25 deletions src/utils/moduleType.ts

This file was deleted.

0 comments on commit d824f51

Please sign in to comment.