Skip to content

Commit

Permalink
various fixes & improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vgao1996 committed Jan 28, 2025
1 parent 049400d commit 10ba6d2
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 14 deletions.
9 changes: 9 additions & 0 deletions workspace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ Aptos Workspace provides a testing environment framework for Aptos developers to

Aptos Workspace utilizes [mocha](https://mochajs.org/) as the testing framework and [chai](https://www.chaijs.com/) as the assertion framework.

## Prerequisites
Currently you'll need to have Docker installed on your system in order to use Aptos Workspace.
Docker is required because Aptos Workspace uses it to run a local indexer instance, which is part of the local network.

### Install Docker
- **Mac**: Download [Docker Desktop for macOS](https://docs.docker.com/desktop/setup/install/mac-install/) from the Docker website.
- **Windows**: Download [Docker Desktop for Windows](https://docs.docker.com/desktop/setup/install/windows-install/) from the Docker website.
- **Linux**: Download [Docker Desktop for Linux](https://docs.docker.com/desktop/setup/install/linux/), or install [Docker Engine](https://docs.docker.com/engine/install/).

## Getting Started

To start using Workspace you need to create an `npm project` by going to an empty folder (or `cd` into an existing one), and run:
Expand Down
11 changes: 9 additions & 2 deletions workspace/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion workspace/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aptos-labs/workspace",
"version": "0.0.17",
"version": "0.0.18",
"license": "Apache-2.0",
"bin": {
"aptos-workspace": "./dist/internal/cli.js",
Expand Down Expand Up @@ -51,6 +51,7 @@
"@types/fs-extra": "^5.1.0",
"@types/mocha": "^10.0.7",
"@types/node": "^20.14.11",
"@types/semver": "^7.5.8",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"chai": "^4.2.0",
Expand Down
23 changes: 15 additions & 8 deletions workspace/src/internal/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ function generateRandomId(length: number): string {
export class TestNode {
private static readonly TIMEOUT_SECONDS = 60;

private constructor(private info: NodeInfo | null) {}
private constructor(private info: NodeInfo | null) { }

public static async spawn(): Promise<TestNode> {
const id = generateRandomId(4);

const configVerbose = getUserConfigVerbose();

const cliCommand = "aptos-workspace-server";
const cliArgs: string[] = [];
const cliCommand = "npx";
const cliArgs: string[] = ["aptos", "workspace", "run"];

const childProcess = spawn(cliCommand, cliArgs);
const childProcess = spawn(cliCommand, cliArgs, { detached: false, stdio: ['ignore', 'pipe', 'ignore'], shell: true });

const rl = readline.createInterface({
input: childProcess.stdout,
output: process.stdout,
output: undefined,
terminal: false,
});

Expand Down Expand Up @@ -123,12 +123,19 @@ export class TestNode {
public async stop() {
await new Promise((resolve, reject) => {
if (!this.info?.process?.pid) return;
kill(this.info.process.pid, (err) => {

const process = this.info.process;
kill(this.info.process.pid, 'SIGINT', (err) => {
if (err) {
reject(err);
} else {
resolve(true);
return;
}

process.on('exit', (code, signal) => {
resolve(true)
});

// TODO: timeout?
});
});
}
Expand Down
5 changes: 3 additions & 2 deletions workspace/src/internal/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ChildProcessWithoutNullStreams } from "child_process";
import { ChildProcessByStdio, ChildProcessWithoutNullStreams } from "child_process";
import { Readable } from "stream";

export type NodeInfo = {
process: ChildProcessWithoutNullStreams;
process: ChildProcessByStdio<null, Readable, null>;
rest_api_port: number;
faucet_port: number;
indexer_port: number;
Expand Down
52 changes: 51 additions & 1 deletion workspace/src/tasks/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,69 @@ import {
} from "../internal/utils/source-files";
import { MochaOptions } from "mocha";
import { isTSProject } from "../internal/utils/typescript-support";
import * as os from 'os';
import { exec } from "child_process";
import semver from "semver";

export type TestOptionsArguments = {
timeout: string;
grep: string;
jobs: number;
};

function max_num_jobs(): number {
const cpuCores = os.cpus().length;
const totalMemoryGB = os.totalmem() / (1024 ** 3);

return Math.max(1, Math.min(Math.floor(cpuCores / 4), Math.floor(totalMemoryGB / 4)));
}

async function checkAptosVersion(): Promise<void> {
return new Promise((resolve, reject) => {
exec("npx aptos --version", (error, stdout, stderr) => {
if (error) {
return reject(
new Error(
"Failed to run npx aptos, have you installed it properly?\n" +
"If your project uses `yarn`, you need to run `yarn add --dev @aptos-labs/ts-sdk`.\n" +
"If your project uses `pnpm`, you need to run `pnpm add -D @aptos-labs/aptos-cli`."
)
);
}

const versionMatch = stdout.match(/aptos (\d+\.\d+\.\d+)/);

if (!versionMatch) {
return reject(new Error("Could not determine the Aptos CLI version."));
}

const version = versionMatch[1];
const minimumVersion = "6.0.2";

if (semver.lt(version, minimumVersion)) {
return reject(
new Error(
`Aptos CLI version needs to be at least ${minimumVersion}, please run "npx aptos update aptos" to update.`
)
);
}

console.log(`Aptos CLI version ${version} detected.`);
resolve(); // Successfully completed the check
});
});
}

export const test = async (options: TestOptionsArguments) => {
const userProjectPath = process.cwd();

isUserProjectTestsFolderExists(userProjectPath);
await checkAptosVersion();

const { default: Mocha } = await import("mocha");

const mochaConfig: MochaOptions = {
timeout: options.timeout ?? 20000, // to support local testnet run, TODO improve performance
timeout: options.timeout ?? 60000, // to support local testnet run, TODO improve performance
require: [path.join(__dirname, "internal/rootHook.js")],
parallel: true,
grep: "",
Expand All @@ -43,6 +90,9 @@ export const test = async (options: TestOptionsArguments) => {
if (options.jobs !== undefined) {
mochaConfig.jobs = options.jobs;
}
else {
mochaConfig.jobs = max_num_jobs();
}

const mocha = new Mocha(mochaConfig);

Expand Down

0 comments on commit 10ba6d2

Please sign in to comment.