Add abi diff plugin method tests (#600)

experimental-options
cgewecke 4 years ago
parent ac0618c34f
commit 833dba065f
  1. 11
      plugins/hardhat.plugin.js
  2. 3
      plugins/resources/nomiclabs.ui.js
  3. 9
      plugins/resources/nomiclabs.utils.js
  4. 5
      plugins/resources/truffle.utils.js
  5. 11
      plugins/truffle.plugin.js
  6. 33
      test/units/hardhat/flags.js
  7. 29
      test/units/truffle/flags.js

@ -98,6 +98,7 @@ task("coverage", "Generates a code coverage report for tests")
.addOptionalParam("solcoverjs", ui.flags.solcoverjs, "", types.string) .addOptionalParam("solcoverjs", ui.flags.solcoverjs, "", types.string)
.addOptionalParam('temp', ui.flags.temp, "", types.string) .addOptionalParam('temp', ui.flags.temp, "", types.string)
.addFlag('matrix', ui.flags.testMatrix) .addFlag('matrix', ui.flags.testMatrix)
.addFlag('abi', ui.flags.abi)
.setAction(async function(args, env){ .setAction(async function(args, env){
const API = require('./../lib/api'); const API = require('./../lib/api');
@ -141,6 +142,16 @@ task("coverage", "Generates a code coverage report for tests")
} }
env.hardhatArguments = Object.assign(env.hardhatArguments, flags) 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 // Instrumentation
// ================ // ================

@ -12,6 +12,9 @@ class PluginUI extends UI {
testMatrix: `Generate a json object which maps which unit tests hit which lines of code.`, 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. ` + solcoverjs: `Relative path from working directory to config. ` +
`Useful for monorepo packages that share settings.`, `Useful for monorepo packages that share settings.`,

@ -190,7 +190,7 @@ async function getAllArtifacts(env){
const all = []; const all = [];
const qualifiedNames = await env.artifacts.getArtifactPaths(); const qualifiedNames = await env.artifacts.getArtifactPaths();
for (const name of qualifiedNames){ for (const name of qualifiedNames){
all.push(await env.artifacts.readArtifact(name)); all.push(require(name));
} }
return all; return all;
} }
@ -203,9 +203,9 @@ async function getAllArtifacts(env){
* @param {HRE} env * @param {HRE} env
* @param {SolidityCoverageAPI} api * @param {SolidityCoverageAPI} api
*/ */
async function generateHumanReadableAbiList(env, api){ async function generateHumanReadableAbiList(env, api, TASK_COMPILE){
await env.run(TASK_COMPILE); await env.run(TASK_COMPILE);
const _artifacts = getAllArtifacts(env); const _artifacts = await getAllArtifacts(env);
const list = api.abiUtils.generateHumanReadableAbiList(_artifacts) const list = api.abiUtils.generateHumanReadableAbiList(_artifacts)
api.saveHumanReadableAbis(list); api.saveHumanReadableAbis(list);
} }
@ -263,6 +263,7 @@ module.exports = {
getTestFilePaths, getTestFilePaths,
setNetworkFrom, setNetworkFrom,
collectTestMatrixData, collectTestMatrixData,
getAllArtifacts getAllArtifacts,
generateHumanReadableAbiList
} }

@ -63,7 +63,7 @@ function getAllArtifacts(config){
* @param {SolidityCoverageAPI} api * @param {SolidityCoverageAPI} api
*/ */
async function generateHumanReadableAbiList(config, truffle, api){ async function generateHumanReadableAbiList(config, truffle, api){
await truffle.compile(config); await truffle.contracts.compile(config);
const _artifacts = getAllArtifacts(config); const _artifacts = getAllArtifacts(config);
const list = api.abiUtils.generateHumanReadableAbiList(_artifacts) const list = api.abiUtils.generateHumanReadableAbiList(_artifacts)
api.saveHumanReadableAbis(list); api.saveHumanReadableAbis(list);
@ -268,5 +268,6 @@ module.exports = {
loadLibrary, loadLibrary,
normalizeConfig, normalizeConfig,
filteredLogger, filteredLogger,
collectTestMatrixData collectTestMatrixData,
generateHumanReadableAbiList
} }

@ -30,9 +30,18 @@ async function plugin(config){
truffle = truffleUtils.loadLibrary(config); truffle = truffleUtils.loadLibrary(config);
api = new API(utils.loadSolcoverJS(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 // Server launch
truffleUtils.setNetwork(config, api);
const client = api.client || truffle.ganache; const client = api.client || truffle.ganache;
const address = await api.ganache(client); const address = await api.ganache(client);
const accountsRequest = await utils.getAccountsGanache(api.server.provider); const accountsRequest = await utils.getAccountsGanache(api.server.provider);

@ -191,5 +191,38 @@ describe('Hardhat Plugin: command line options', function() {
assert.deepEqual(producedMatrix, expectedMatrix); 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);
})
}); });

@ -275,5 +275,34 @@ describe('Truffle Plugin: command line options', function() {
assert.deepEqual(producedMatrix, expectedMatrix); assert.deepEqual(producedMatrix, expectedMatrix);
process.env.TRUFFLE_TEST = false; 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);
})
}); });

Loading…
Cancel
Save