Skip to content

Commit 62cfebc

Browse files
committed
fixed utils for clashing tests
1 parent 7ab326c commit 62cfebc

File tree

3 files changed

+159
-82
lines changed

3 files changed

+159
-82
lines changed

package-lock.json

+132-64
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"flat": "bash scripts/make_flat.sh",
1212
"solidity-docgen": "rm -rf docs/docs/api* && npm i && npx hardhat docgen --exclude interfaces,libs,ERC677BridgeTokenRewardable.sol,Migrations.sol . contracts docs && node docs/website/sidebars-fix.js",
1313
"generate-uml": "sol2uml ./contracts -o docs/classDiagram.svg",
14-
"test": "npx hardhat test",
14+
"test": "node scripts/check_for_clashing.js && npx hardhat test",
1515
"testNoUpgradeProxy": "export CONTRACTS_NO_UPGRADE_PROXY=true && node scripts/check_for_clashing.js && bash scripts/test.sh",
1616
"testOnTestnet": "export CONTRACTS_NO_UPGRADE_PROXY=true && ./node_modules/.bin/truffle test --network testNet test/KeyGenHistory.js test/mockContracts/BlockRewardHbbftCoinsMock.sol test/mockContracts/RandomHbbftMock.sol test/mockContracts/ValidatorSetHbbftMock.sol test/mockContracts/StakingHbbftCoinsMock.sol",
1717
"getFunctionSignatures": "node scripts/getFunctionSignatures.js"
@@ -47,8 +47,10 @@
4747
"hardhat-contract-sizer": "^2.6.1",
4848
"hardhat-gas-reporter": "^1.0.8",
4949
"lodash": "^4.17.21",
50+
"solc": "^0.8.17",
5051
"solidity-coverage": "^0.7.21",
5152
"solidity-docgen": "^0.6.0-beta.29",
52-
"typechain": "^8.1.0"
53+
"typechain": "^8.1.0",
54+
"typescript": "^4.8.4"
5355
}
5456
}

scripts/utils/utils.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs');
22
const solc = require('solc');
3+
const path = require('node:path');
34

45
async function compile(dir, contractName) {
56
const input = {
@@ -17,30 +18,36 @@ async function compile(dir, contractName) {
1718
evmVersion: "istanbul",
1819
outputSelection: {
1920
'*': {
20-
'*': [ 'abi', 'evm.bytecode.object', 'evm.methodIdentifiers' ]
21+
'*': ['abi', 'evm.bytecode.object', 'evm.methodIdentifiers']
2122
}
2223
}
2324
}
2425
}
2526

26-
const compiled = JSON.parse(solc.compile(JSON.stringify(input), function(path) {
27-
let content;
28-
try {
29-
content = fs.readFileSync(dir + path);
30-
} catch (e) {
31-
if (e.code == 'ENOENT') {
32-
try {
33-
content = fs.readFileSync(dir + '../' + path);
34-
} catch (e) {
35-
content = fs.readFileSync(dir + '../node_modules/' + path);
36-
}
37-
}
27+
const intermediateFoldersOfCurrentContract = dir.slice(1, -1);
28+
29+
function findImports(path) {
30+
console.log(path)
31+
let sourceCodeToImport;
32+
if (path[0] === "@") { // directly into node_ module
33+
sourceCodeToImport = fs.readFileSync(`../../../node_modules/${path}`);
34+
return { contents: `${sourceCodeToImport}` };
3835
}
39-
return {
40-
contents: content.toString()
36+
if (dir.length === 2) { // array contract path is "./" + contract.sol, i.e simple import in the same folder as the compile.js
37+
sourceCodeToImport = fs.readFileSync(`./${path}`);
38+
return { contents: `${sourceCodeToImport}` };
4139
}
42-
}));
40+
if (!path.includes("/")) { // === contract to import is in the same folder as the contract we are compiling i.e the import path from the contract fiel doesn't include "/"
41+
sourceCodeToImport = fs.readFileSync(`./${intermediateFoldersOfCurrentContract}/${path}`);
42+
return { contents: `${sourceCodeToImport}` };
43+
}
44+
else { // if neither of these, contract must be (in my case) accessible from the compile.js, i.e no need to change the path
45+
sourceCodeToImport = fs.readFileSync(`./contracts/${path}`);
46+
return { contents: `${sourceCodeToImport}` }
47+
}
48+
}
4349

50+
const compiled = JSON.parse(solc.compile(JSON.stringify(input), { import: findImports }))
4451
return compiled.contracts[''][contractName];
4552
}
4653

0 commit comments

Comments
 (0)