Skip to content

Add abi diff plugin method tests #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions plugins/hardhat.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ task("coverage", "Generates a code coverage report for tests")
.addOptionalParam("solcoverjs", ui.flags.solcoverjs, "", types.string)
.addOptionalParam('temp', ui.flags.temp, "", types.string)
.addFlag('matrix', ui.flags.testMatrix)
.addFlag('abi', ui.flags.abi)
.setAction(async function(args, env){

let error;
Expand Down Expand Up @@ -119,6 +120,16 @@ task("coverage", "Generates a code coverage report for tests")
}
env.hardhatArguments = Object.assign(env.hardhatArguments, flags)

// ===========================
// Generate abi diff component
// (This flag only useful within codecheck context)
// ===========================
if (args.abi){
measureCoverage = false;
await nomiclabsUtils.generateHumanReadableAbiList(env, api, TASK_COMPILE);
return;
}

// ================
// Instrumentation
// ================
Expand Down
3 changes: 3 additions & 0 deletions plugins/resources/nomiclabs.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class PluginUI extends UI {

testMatrix: `Generate a json object which maps which unit tests hit which lines of code.`,

abi: `Generate a json object which can be used to produce a unified diff of your ` +
`contracts public interface between two commits.`,

solcoverjs: `Relative path from working directory to config. ` +
`Useful for monorepo packages that share settings.`,

Expand Down
9 changes: 5 additions & 4 deletions plugins/resources/nomiclabs.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ async function getAllArtifacts(env){
const all = [];
const qualifiedNames = await env.artifacts.getArtifactPaths();
for (const name of qualifiedNames){
all.push(await env.artifacts.readArtifact(name));
all.push(require(name));
}
return all;
}
Expand All @@ -202,9 +202,9 @@ async function getAllArtifacts(env){
* @param {HRE} env
* @param {SolidityCoverageAPI} api
*/
async function generateHumanReadableAbiList(env, api){
async function generateHumanReadableAbiList(env, api, TASK_COMPILE){
await env.run(TASK_COMPILE);
const _artifacts = getAllArtifacts(env);
const _artifacts = await getAllArtifacts(env);
const list = api.abiUtils.generateHumanReadableAbiList(_artifacts)
api.saveHumanReadableAbis(list);
}
Expand Down Expand Up @@ -261,6 +261,7 @@ module.exports = {
getTestFilePaths,
setNetworkFrom,
collectTestMatrixData,
getAllArtifacts
getAllArtifacts,
generateHumanReadableAbiList
}

5 changes: 3 additions & 2 deletions plugins/resources/truffle.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function getAllArtifacts(config){
* @param {SolidityCoverageAPI} api
*/
async function generateHumanReadableAbiList(config, truffle, api){
await truffle.compile(config);
await truffle.contracts.compile(config);
const _artifacts = getAllArtifacts(config);
const list = api.abiUtils.generateHumanReadableAbiList(_artifacts)
api.saveHumanReadableAbis(list);
Expand Down Expand Up @@ -270,5 +270,6 @@ module.exports = {
loadLibrary,
normalizeConfig,
filteredLogger,
collectTestMatrixData
collectTestMatrixData,
generateHumanReadableAbiList
}
11 changes: 10 additions & 1 deletion plugins/truffle.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ async function plugin(config){
truffle = truffleUtils.loadLibrary(config);
api = new API(utils.loadSolcoverJS(config));

truffleUtils.setNetwork(config, api);
// ===========================
// Generate abi diff component
// (This flag only useful within codecheck context)
// ===========================
if (config.abi){
await truffleUtils.generateHumanReadableAbiList(config, truffle, api);
return;
}

// Server launch
truffleUtils.setNetwork(config, api);

const client = api.client || truffle.ganache;
const address = await api.ganache(client);
const accountsRequest = await utils.getAccountsGanache(api.server.provider);
Expand Down
33 changes: 33 additions & 0 deletions test/units/hardhat/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,38 @@ describe('Hardhat Plugin: command line options', function() {

assert.deepEqual(producedMatrix, expectedMatrix);
});

it('--abi', async function(){
const expected = [
{
"contractName": "Migrations",
"humanReadableAbiList": [
"function last_completed_migration() view returns (uint256)",
"function owner() view returns (address)",
"function setCompleted(uint256) nonpayable",
"function upgrade(address) nonpayable"
]
},
{
"contractName": "Simple",
"humanReadableAbiList": [
"function getX() view returns (uint256)",
"function test(uint256) nonpayable"
]
}
];

const taskArgs = {
abi: true
}
mock.install('Simple', 'simple.js', solcoverConfig);
mock.hardhatSetupEnv(this);

await this.env.run("coverage", taskArgs);

const outputPath = path.join(process.cwd(), 'humanReadableAbis.json');
const output = require(outputPath);
assert.deepEqual(output, expected);
})
});

29 changes: 29 additions & 0 deletions test/units/truffle/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,34 @@ describe('Truffle Plugin: command line options', function() {
assert.deepEqual(producedMatrix, expectedMatrix);
process.env.TRUFFLE_TEST = false;
});

it('--abi', async function(){
const expected = [
{
"contractName": "Migrations",
"humanReadableAbiList": [
"function last_completed_migration() view returns (uint256)",
"function owner() view returns (address)",
"function setCompleted(uint256) nonpayable",
"function upgrade(address) nonpayable"
]
},
{
"contractName": "Simple",
"humanReadableAbiList": [
"function getX() view returns (uint256)",
"function test(uint256) nonpayable"
]
}
];

truffleConfig.abi = true;
mock.install('Simple', 'simple.js', solcoverConfig);
await plugin(truffleConfig);

const outputPath = path.join(process.cwd(), mock.pathToTemp('./humanReadableAbis.json'));
const output = require(outputPath);
assert.deepEqual(output, expected);
})
});