Skip to content

Commit 308f32d

Browse files
authored
Add abi diff plugin method tests (#600)
1 parent faf8cb5 commit 308f32d

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
@@ -86,6 +86,7 @@ task("coverage", "Generates a code coverage report for tests")
8686
.addOptionalParam("solcoverjs", ui.flags.solcoverjs, "", types.string)
8787
.addOptionalParam('temp', ui.flags.temp, "", types.string)
8888
.addFlag('matrix', ui.flags.testMatrix)
89+
.addFlag('abi', ui.flags.abi)
8990
.setAction(async function(args, env){
9091

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

123+
// ===========================
124+
// Generate abi diff component
125+
// (This flag only useful within codecheck context)
126+
// ===========================
127+
if (args.abi){
128+
measureCoverage = false;
129+
await nomiclabsUtils.generateHumanReadableAbiList(env, api, TASK_COMPILE);
130+
return;
131+
}
132+
122133
// ================
123134
// Instrumentation
124135
// ================

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
@@ -189,7 +189,7 @@ async function getAllArtifacts(env){
189189
const all = [];
190190
const qualifiedNames = await env.artifacts.getArtifactPaths();
191191
for (const name of qualifiedNames){
192-
all.push(await env.artifacts.readArtifact(name));
192+
all.push(require(name));
193193
}
194194
return all;
195195
}
@@ -202,9 +202,9 @@ async function getAllArtifacts(env){
202202
* @param {HRE} env
203203
* @param {SolidityCoverageAPI} api
204204
*/
205-
async function generateHumanReadableAbiList(env, api){
205+
async function generateHumanReadableAbiList(env, api, TASK_COMPILE){
206206
await env.run(TASK_COMPILE);
207-
const _artifacts = getAllArtifacts(env);
207+
const _artifacts = await getAllArtifacts(env);
208208
const list = api.abiUtils.generateHumanReadableAbiList(_artifacts)
209209
api.saveHumanReadableAbis(list);
210210
}
@@ -261,6 +261,7 @@ module.exports = {
261261
getTestFilePaths,
262262
setNetworkFrom,
263263
collectTestMatrixData,
264-
getAllArtifacts
264+
getAllArtifacts,
265+
generateHumanReadableAbiList
265266
}
266267

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);
@@ -270,5 +270,6 @@ module.exports = {
270270
loadLibrary,
271271
normalizeConfig,
272272
filteredLogger,
273-
collectTestMatrixData
273+
collectTestMatrixData,
274+
generateHumanReadableAbiList
274275
}

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
@@ -289,5 +289,34 @@ describe('Truffle Plugin: command line options', function() {
289289
assert.deepEqual(producedMatrix, expectedMatrix);
290290
process.env.TRUFFLE_TEST = false;
291291
});
292+
293+
it('--abi', async function(){
294+
const expected = [
295+
{
296+
"contractName": "Migrations",
297+
"humanReadableAbiList": [
298+
"function last_completed_migration() view returns (uint256)",
299+
"function owner() view returns (address)",
300+
"function setCompleted(uint256) nonpayable",
301+
"function upgrade(address) nonpayable"
302+
]
303+
},
304+
{
305+
"contractName": "Simple",
306+
"humanReadableAbiList": [
307+
"function getX() view returns (uint256)",
308+
"function test(uint256) nonpayable"
309+
]
310+
}
311+
];
312+
313+
truffleConfig.abi = true;
314+
mock.install('Simple', 'simple.js', solcoverConfig);
315+
await plugin(truffleConfig);
316+
317+
const outputPath = path.join(process.cwd(), mock.pathToTemp('./humanReadableAbis.json'));
318+
const output = require(outputPath);
319+
assert.deepEqual(output, expected);
320+
})
292321
});
293322

0 commit comments

Comments
 (0)