Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
feat(solidity/extension): functions to run foundry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ByFishh committed Dec 6, 2023
1 parent bef131d commit 76d6288
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions toolchains/solidity/extension/src/foundry-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import {exec} from 'child_process';
import * as vscode from "vscode";

type TestResult = {
status: string,
reason: string | null,
counterexample: any | null,
logs: any[],
decoded_logs: string[]

Check warning on line 9 in toolchains/solidity/extension/src/foundry-test.ts

View workflow job for this annotation

GitHub Actions / lint (osmium-solidity)

Type Property name `decoded_logs` must match one of the following formats: camelCase
kind: any,
traces: any
coverage: any
labeled_addresses: {

Check warning on line 13 in toolchains/solidity/extension/src/foundry-test.ts

View workflow job for this annotation

GitHub Actions / lint (osmium-solidity)

Type Property name `labeled_addresses` must match one of the following formats: camelCase
[key: string]: string
}
debug: any | null
breakpoints: any
};

type SuiteResult = {
duration: {
nanos: number
secs: number
},
test_results: {

Check warning on line 25 in toolchains/solidity/extension/src/foundry-test.ts

View workflow job for this annotation

GitHub Actions / lint (osmium-solidity)

Type Property name `test_results` must match one of the following formats: camelCase
[key: string]: TestResult
},
warnings: string[]
};

type FileResult = {
[key: string]: SuiteResult
};

const hasForge = async (workspace: string) => {
return new Promise((resolve, reject) => {
exec('forge --version', {
cwd: workspace
}, (err, stdout, stderr) => {
if (err) {
console.log(err);
vscode.window.showErrorMessage('Forge not found. Please install it and try again.');
resolve(false);
} else {
resolve(true);
}
});
});
};

const testAll = async (workspace: string): Promise<FileResult> => {
return new Promise(async (resolve, reject) => {
if (!(await hasForge(workspace))) {
reject("No forge found");
}

exec('forge test --json', {
cwd: workspace
}, (error, stdout, stderr) => {
if (error) {
if (!stderr.length) {
resolve(JSON.parse(stdout));
}
console.log(stderr);
vscode.window.showErrorMessage('Error while running forge tests.');
reject(stderr);
} else {
resolve(JSON.parse(stdout));
}
});
});
};

const testContract = (workspace: string, contractName: string): Promise<FileResult> => {
return new Promise(async (resolve, reject) => {
if (!(await hasForge(workspace))) {
reject("No forge found");
}

exec('forge test --json --match-contract ' + contractName, {
cwd: workspace
}, (error, stdout, stderr) => {
if (error) {
if (!stderr.length) {
resolve(JSON.parse(stdout));
}
console.log(stderr);
vscode.window.showErrorMessage('Error while running forge tests.');
reject(stderr);
} else {
resolve(JSON.parse(stdout));
}
});
});
};

const testFunction = (workspace: string, contractName: string, functionName: string): Promise<FileResult> => {
return new Promise(async (resolve, reject) => {
if (!(await hasForge(workspace))) {
reject("No forge found");
}

exec('forge test --json --match-contract ' + contractName + " --match-test " + functionName, {
cwd: workspace
}, (error, stdout, stderr) => {
if (error) {
if (!stderr.length) {
resolve(JSON.parse(stdout));
}
console.log(stderr);
vscode.window.showErrorMessage('Error while running forge tests.');
reject(stderr);
} else {
resolve(JSON.parse(stdout));
}
});
});
};


export {hasForge, testAll, testContract, testFunction};

0 comments on commit 76d6288

Please sign in to comment.