This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(solidity/extension): functions to run foundry tests
- Loading branch information
Showing
1 changed file
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] | ||
kind: any, | ||
traces: any | ||
coverage: any | ||
labeled_addresses: { | ||
[key: string]: string | ||
} | ||
debug: any | null | ||
breakpoints: any | ||
}; | ||
|
||
type SuiteResult = { | ||
duration: { | ||
nanos: number | ||
secs: number | ||
}, | ||
test_results: { | ||
[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}; |