Skip to content

Commit adcaab5

Browse files
committed
Add abi diff plugin method tests (#600)
1 parent d09ed17 commit adcaab5

File tree

7 files changed

+94
-7
lines changed

7 files changed

+94
-7
lines changed

plugins/hardhat.plugin.js

+11
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ task("coverage", "Generates a code coverage report for tests")
9898
.addOptionalParam("solcoverjs", ui.flags.solcoverjs, "", types.string)
9999
.addOptionalParam('temp', ui.flags.temp, "", types.string)
100100
.addFlag('matrix', ui.flags.testMatrix)
101+
.addFlag('abi', ui.flags.abi)
101102
.setAction(async function(args, env){
102103

103104
const API = require('./../lib/api');
@@ -141,6 +142,16 @@ task("coverage", "Generates a code coverage report for tests")
141142
}
142143
env.hardhatArguments = Object.assign(env.hardhatArguments, flags)
143144

145+
// ===========================
146+
// Generate abi diff component
147+
// (This flag only useful within codecheck context)
148+
// ===========================
149+
if (args.abi){
150+
measureCoverage = false;
151+
await nomiclabsUtils.generateHumanReadableAbiList(env, api, TASK_COMPILE);
152+
return;
153+
}
154+
144155
// ================
145156
// Instrumentation
146157
// ================

plugins/resources/nomiclabs.ui.js

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class PluginUI extends UI {
1212

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

15+
abi: `Generate a json object which can be used to produce a unified diff of your ` +
16+
`contracts public interface between two commits.`,
17+
1518
solcoverjs: `Relative path from working directory to config. ` +
1619
`Useful for monorepo packages that share settings.`,
1720

plugins/resources/nomiclabs.utils.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ async function getAllArtifacts(env){
190190
const all = [];
191191
const qualifiedNames = await env.artifacts.getArtifactPaths();
192192
for (const name of qualifiedNames){
193-
all.push(await env.artifacts.readArtifact(name));
193+
all.push(require(name));
194194
}
195195
return all;
196196
}
@@ -203,9 +203,9 @@ async function getAllArtifacts(env){
203203
* @param {HRE} env
204204
* @param {SolidityCoverageAPI} api
205205
*/
206-
async function generateHumanReadableAbiList(env, api){
206+
async function generateHumanReadableAbiList(env, api, TASK_COMPILE){
207207
await env.run(TASK_COMPILE);
208-
const _artifacts = getAllArtifacts(env);
208+
const _artifacts = await getAllArtifacts(env);
209209
const list = api.abiUtils.generateHumanReadableAbiList(_artifacts)
210210
api.saveHumanReadableAbis(list);
211211
}
@@ -263,6 +263,7 @@ module.exports = {
263263
getTestFilePaths,
264264
setNetworkFrom,
265265
collectTestMatrixData,
266-
getAllArtifacts
266+
getAllArtifacts,
267+
generateHumanReadableAbiList
267268
}
268269

plugins/resources/truffle.utils.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function getAllArtifacts(config){
6363
* @param {SolidityCoverageAPI} api
6464
*/
6565
async function generateHumanReadableAbiList(config, truffle, api){
66-
await truffle.compile(config);
66+
await truffle.contracts.compile(config);
6767
const _artifacts = getAllArtifacts(config);
6868
const list = api.abiUtils.generateHumanReadableAbiList(_artifacts)
6969
api.saveHumanReadableAbis(list);
@@ -268,5 +268,6 @@ module.exports = {
268268
loadLibrary,
269269
normalizeConfig,
270270
filteredLogger,
271-
collectTestMatrixData
271+
collectTestMatrixData,
272+
generateHumanReadableAbiList
272273
}

plugins/truffle.plugin.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@ async function plugin(config){
3030
truffle = truffleUtils.loadLibrary(config);
3131
api = new API(utils.loadSolcoverJS(config));
3232

33-
truffleUtils.setNetwork(config, api);
33+
// ===========================
34+
// Generate abi diff component
35+
// (This flag only useful within codecheck context)
36+
// ===========================
37+
if (config.abi){
38+
await truffleUtils.generateHumanReadableAbiList(config, truffle, api);
39+
return;
40+
}
3441

3542
// Server launch
43+
truffleUtils.setNetwork(config, api);
44+
3645
const client = api.client || truffle.ganache;
3746
const address = await api.ganache(client);
3847
const accountsRequest = await utils.getAccountsGanache(api.server.provider);

test/units/hardhat/flags.js

+33
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,38 @@ describe('Hardhat Plugin: command line options', function() {
191191

192192
assert.deepEqual(producedMatrix, expectedMatrix);
193193
});
194+
195+
it('--abi', async function(){
196+
const expected = [
197+
{
198+
"contractName": "Migrations",
199+
"humanReadableAbiList": [
200+
"function last_completed_migration() view returns (uint256)",
201+
"function owner() view returns (address)",
202+
"function setCompleted(uint256) nonpayable",
203+
"function upgrade(address) nonpayable"
204+
]
205+
},
206+
{
207+
"contractName": "Simple",
208+
"humanReadableAbiList": [
209+
"function getX() view returns (uint256)",
210+
"function test(uint256) nonpayable"
211+
]
212+
}
213+
];
214+
215+
const taskArgs = {
216+
abi: true
217+
}
218+
mock.install('Simple', 'simple.js', solcoverConfig);
219+
mock.hardhatSetupEnv(this);
220+
221+
await this.env.run("coverage", taskArgs);
222+
223+
const outputPath = path.join(process.cwd(), 'humanReadableAbis.json');
224+
const output = require(outputPath);
225+
assert.deepEqual(output, expected);
226+
})
194227
});
195228

test/units/truffle/flags.js

+29
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,34 @@ describe('Truffle Plugin: command line options', function() {
275275
assert.deepEqual(producedMatrix, expectedMatrix);
276276
process.env.TRUFFLE_TEST = false;
277277
});
278+
279+
it('--abi', async function(){
280+
const expected = [
281+
{
282+
"contractName": "Migrations",
283+
"humanReadableAbiList": [
284+
"function last_completed_migration() view returns (uint256)",
285+
"function owner() view returns (address)",
286+
"function setCompleted(uint256) nonpayable",
287+
"function upgrade(address) nonpayable"
288+
]
289+
},
290+
{
291+
"contractName": "Simple",
292+
"humanReadableAbiList": [
293+
"function getX() view returns (uint256)",
294+
"function test(uint256) nonpayable"
295+
]
296+
}
297+
];
298+
299+
truffleConfig.abi = true;
300+
mock.install('Simple', 'simple.js', solcoverConfig);
301+
await plugin(truffleConfig);
302+
303+
const outputPath = path.join(process.cwd(), mock.pathToTemp('./humanReadableAbis.json'));
304+
const output = require(outputPath);
305+
assert.deepEqual(output, expected);
306+
})
278307
});
279308

0 commit comments

Comments
 (0)