Skip to content

Commit 10ba6d2

Browse files
committed
various fixes & improvements
1 parent 049400d commit 10ba6d2

File tree

6 files changed

+89
-14
lines changed

6 files changed

+89
-14
lines changed

workspace/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ Aptos Workspace provides a testing environment framework for Aptos developers to
1010

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

13+
## Prerequisites
14+
Currently you'll need to have Docker installed on your system in order to use Aptos Workspace.
15+
Docker is required because Aptos Workspace uses it to run a local indexer instance, which is part of the local network.
16+
17+
### Install Docker
18+
- **Mac**: Download [Docker Desktop for macOS](https://docs.docker.com/desktop/setup/install/mac-install/) from the Docker website.
19+
- **Windows**: Download [Docker Desktop for Windows](https://docs.docker.com/desktop/setup/install/windows-install/) from the Docker website.
20+
- **Linux**: Download [Docker Desktop for Linux](https://docs.docker.com/desktop/setup/install/linux/), or install [Docker Engine](https://docs.docker.com/engine/install/).
21+
1322
## Getting Started
1423

1524
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:

workspace/package-lock.json

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workspace/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aptos-labs/workspace",
3-
"version": "0.0.17",
3+
"version": "0.0.18",
44
"license": "Apache-2.0",
55
"bin": {
66
"aptos-workspace": "./dist/internal/cli.js",
@@ -51,6 +51,7 @@
5151
"@types/fs-extra": "^5.1.0",
5252
"@types/mocha": "^10.0.7",
5353
"@types/node": "^20.14.11",
54+
"@types/semver": "^7.5.8",
5455
"@typescript-eslint/eslint-plugin": "^7.2.0",
5556
"@typescript-eslint/parser": "^7.2.0",
5657
"chai": "^4.2.0",

workspace/src/internal/node.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ function generateRandomId(length: number): string {
2424
export class TestNode {
2525
private static readonly TIMEOUT_SECONDS = 60;
2626

27-
private constructor(private info: NodeInfo | null) {}
27+
private constructor(private info: NodeInfo | null) { }
2828

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

3232
const configVerbose = getUserConfigVerbose();
3333

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

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

3939
const rl = readline.createInterface({
4040
input: childProcess.stdout,
41-
output: process.stdout,
41+
output: undefined,
4242
terminal: false,
4343
});
4444

@@ -123,12 +123,19 @@ export class TestNode {
123123
public async stop() {
124124
await new Promise((resolve, reject) => {
125125
if (!this.info?.process?.pid) return;
126-
kill(this.info.process.pid, (err) => {
126+
127+
const process = this.info.process;
128+
kill(this.info.process.pid, 'SIGINT', (err) => {
127129
if (err) {
128130
reject(err);
129-
} else {
130-
resolve(true);
131+
return;
131132
}
133+
134+
process.on('exit', (code, signal) => {
135+
resolve(true)
136+
});
137+
138+
// TODO: timeout?
132139
});
133140
});
134141
}

workspace/src/internal/utils/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { ChildProcessWithoutNullStreams } from "child_process";
1+
import { ChildProcessByStdio, ChildProcessWithoutNullStreams } from "child_process";
2+
import { Readable } from "stream";
23

34
export type NodeInfo = {
4-
process: ChildProcessWithoutNullStreams;
5+
process: ChildProcessByStdio<null, Readable, null>;
56
rest_api_port: number;
67
faucet_port: number;
78
indexer_port: number;

workspace/src/tasks/test.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,69 @@ import {
88
} from "../internal/utils/source-files";
99
import { MochaOptions } from "mocha";
1010
import { isTSProject } from "../internal/utils/typescript-support";
11+
import * as os from 'os';
12+
import { exec } from "child_process";
13+
import semver from "semver";
1114

1215
export type TestOptionsArguments = {
1316
timeout: string;
1417
grep: string;
1518
jobs: number;
1619
};
1720

21+
function max_num_jobs(): number {
22+
const cpuCores = os.cpus().length;
23+
const totalMemoryGB = os.totalmem() / (1024 ** 3);
24+
25+
return Math.max(1, Math.min(Math.floor(cpuCores / 4), Math.floor(totalMemoryGB / 4)));
26+
}
27+
28+
async function checkAptosVersion(): Promise<void> {
29+
return new Promise((resolve, reject) => {
30+
exec("npx aptos --version", (error, stdout, stderr) => {
31+
if (error) {
32+
return reject(
33+
new Error(
34+
"Failed to run npx aptos, have you installed it properly?\n" +
35+
"If your project uses `yarn`, you need to run `yarn add --dev @aptos-labs/ts-sdk`.\n" +
36+
"If your project uses `pnpm`, you need to run `pnpm add -D @aptos-labs/aptos-cli`."
37+
)
38+
);
39+
}
40+
41+
const versionMatch = stdout.match(/aptos (\d+\.\d+\.\d+)/);
42+
43+
if (!versionMatch) {
44+
return reject(new Error("Could not determine the Aptos CLI version."));
45+
}
46+
47+
const version = versionMatch[1];
48+
const minimumVersion = "6.0.2";
49+
50+
if (semver.lt(version, minimumVersion)) {
51+
return reject(
52+
new Error(
53+
`Aptos CLI version needs to be at least ${minimumVersion}, please run "npx aptos update aptos" to update.`
54+
)
55+
);
56+
}
57+
58+
console.log(`Aptos CLI version ${version} detected.`);
59+
resolve(); // Successfully completed the check
60+
});
61+
});
62+
}
63+
1864
export const test = async (options: TestOptionsArguments) => {
1965
const userProjectPath = process.cwd();
2066

2167
isUserProjectTestsFolderExists(userProjectPath);
68+
await checkAptosVersion();
2269

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

2572
const mochaConfig: MochaOptions = {
26-
timeout: options.timeout ?? 20000, // to support local testnet run, TODO improve performance
73+
timeout: options.timeout ?? 60000, // to support local testnet run, TODO improve performance
2774
require: [path.join(__dirname, "internal/rootHook.js")],
2875
parallel: true,
2976
grep: "",
@@ -43,6 +90,9 @@ export const test = async (options: TestOptionsArguments) => {
4390
if (options.jobs !== undefined) {
4491
mochaConfig.jobs = options.jobs;
4592
}
93+
else {
94+
mochaConfig.jobs = max_num_jobs();
95+
}
4696

4797
const mocha = new Mocha(mochaConfig);
4898

0 commit comments

Comments
 (0)