More test sources reorg and truffle mock utilities cleanup

pull/381/head
cgewecke 5 years ago
parent 9e78ae8d78
commit 29e368b3bf
  1. 24
      dist/truffle.plugin.js
  2. 2
      package.json
  3. 0
      test/integration/truffle/assets/SimpleError.sol
  4. 1
      test/integration/truffle/assets/asset.js
  5. 0
      test/integration/truffle/contracts/Migrations.sol
  6. 0
      test/integration/truffle/migrations/1_initial.js
  7. 0
      test/sources/js/block-gas-limit.js
  8. 0
      test/sources/js/empty.js
  9. 0
      test/sources/js/inheritance.js
  10. 0
      test/sources/js/only-call.js
  11. 0
      test/sources/js/oraclize.js
  12. 0
      test/sources/js/pureview.js
  13. 0
      test/sources/js/requires-externally.js
  14. 0
      test/sources/js/sign.js
  15. 0
      test/sources/js/simple.js
  16. 0
      test/sources/js/sol-parse-fail.js
  17. 0
      test/sources/js/testrpc-options.js
  18. 0
      test/sources/js/totallyPure.js
  19. 0
      test/sources/js/truffle-crash.js
  20. 0
      test/sources/js/truffle-test-fail.js
  21. 0
      test/sources/js/wallet.js
  22. 0
      test/sources/solidity/contracts/app/CLibrary.sol
  23. 0
      test/sources/solidity/contracts/app/Empty.sol
  24. 0
      test/sources/solidity/contracts/app/Events.sol
  25. 0
      test/sources/solidity/contracts/app/Expensive.sol
  26. 0
      test/sources/solidity/contracts/app/Face.sol
  27. 25
      test/sources/solidity/contracts/app/Migrations.sol
  28. 0
      test/sources/solidity/contracts/app/OnlyCall.sol
  29. 0
      test/sources/solidity/contracts/app/Owned.sol
  30. 0
      test/sources/solidity/contracts/app/Proxy.sol
  31. 0
      test/sources/solidity/contracts/app/PureView.sol
  32. 0
      test/sources/solidity/contracts/app/Simple.sol
  33. 0
      test/sources/solidity/contracts/app/TotallyPure.sol
  34. 0
      test/sources/solidity/contracts/app/Wallet.sol
  35. 0
      test/sources/solidity/contracts/assembly/if.sol
  36. 0
      test/sources/solidity/contracts/assembly/spaces-in-function.sol
  37. 0
      test/sources/solidity/contracts/assert/Assert.sol
  38. 0
      test/sources/solidity/contracts/assert/RequireMultiline.sol
  39. 0
      test/sources/solidity/contracts/comments/postContractComment.sol
  40. 0
      test/sources/solidity/contracts/comments/postFunctionDeclarationComment.sol
  41. 0
      test/sources/solidity/contracts/comments/postIfStatementComment.sol
  42. 0
      test/sources/solidity/contracts/comments/postLineComment.sol
  43. 0
      test/sources/solidity/contracts/conditional/declarative-exp-assignment-alternate.sol
  44. 0
      test/sources/solidity/contracts/conditional/identifier-assignment-alternate.sol
  45. 0
      test/sources/solidity/contracts/conditional/mapping-assignment.sol
  46. 0
      test/sources/solidity/contracts/conditional/multiline-alternate.sol
  47. 0
      test/sources/solidity/contracts/conditional/multiline-consequent.sol
  48. 0
      test/sources/solidity/contracts/conditional/sameline-alternate.sol
  49. 0
      test/sources/solidity/contracts/conditional/sameline-consequent.sol
  50. 0
      test/sources/solidity/contracts/conditional/variable-decl-assignment-alternate.sol
  51. 0
      test/sources/solidity/contracts/expressions/new-expression.sol
  52. 0
      test/sources/solidity/contracts/expressions/single-binary-expression.sol
  53. 0
      test/sources/solidity/contracts/function/abstract.sol
  54. 0
      test/sources/solidity/contracts/function/calldata.sol
  55. 0
      test/sources/solidity/contracts/function/chainable-new.sol
  56. 0
      test/sources/solidity/contracts/function/chainable-value.sol
  57. 0
      test/sources/solidity/contracts/function/chainable.sol
  58. 0
      test/sources/solidity/contracts/function/constructor-keyword.sol
  59. 0
      test/sources/solidity/contracts/function/empty-body.sol
  60. 0
      test/sources/solidity/contracts/function/function-call.sol
  61. 0
      test/sources/solidity/contracts/function/function.sol
  62. 0
      test/sources/solidity/contracts/function/modifier.sol
  63. 0
      test/sources/solidity/contracts/function/multiple.sol
  64. 0
      test/sources/solidity/contracts/if/else-if-unbracketed-multi.sol
  65. 0
      test/sources/solidity/contracts/if/else-if-without-brackets.sol
  66. 0
      test/sources/solidity/contracts/if/else-with-brackets.sol
  67. 0
      test/sources/solidity/contracts/if/else-without-brackets.sol
  68. 0
      test/sources/solidity/contracts/if/if-else-no-brackets.sol
  69. 0
      test/sources/solidity/contracts/if/if-elseif-else.sol
  70. 0
      test/sources/solidity/contracts/if/if-no-brackets-multiline.sol
  71. 0
      test/sources/solidity/contracts/if/if-no-brackets.sol
  72. 0
      test/sources/solidity/contracts/if/if-with-brackets-multiline.sol
  73. 0
      test/sources/solidity/contracts/if/if-with-brackets.sol
  74. 0
      test/sources/solidity/contracts/if/nested-if-missing-else.sol
  75. 0
      test/sources/solidity/contracts/loops/for-no-brackets.sol
  76. 0
      test/sources/solidity/contracts/loops/for-with-brackets.sol
  77. 0
      test/sources/solidity/contracts/loops/while-no-brackets.sol
  78. 0
      test/sources/solidity/contracts/loops/while-with-brackets.sol
  79. 0
      test/sources/solidity/contracts/return/return.sol
  80. 0
      test/sources/solidity/contracts/statements/emit-coverage.sol
  81. 0
      test/sources/solidity/contracts/statements/emit-instrument.sol
  82. 0
      test/sources/solidity/contracts/statements/empty-contract-ala-melonport.sol
  83. 0
      test/sources/solidity/contracts/statements/empty-contract-body.sol
  84. 0
      test/sources/solidity/contracts/statements/fn-argument-multiline.sol
  85. 0
      test/sources/solidity/contracts/statements/fn-argument.sol
  86. 0
      test/sources/solidity/contracts/statements/fn-struct.sol
  87. 0
      test/sources/solidity/contracts/statements/library.sol
  88. 0
      test/sources/solidity/contracts/statements/multiple.sol
  89. 0
      test/sources/solidity/contracts/statements/post-close-brace.sol
  90. 0
      test/sources/solidity/contracts/statements/single.sol
  91. 0
      test/sources/solidity/contracts/statements/tuple.sol
  92. 0
      test/sources/solidity/contracts/statements/type-keyword.sol
  93. 0
      test/sources/solidity/errors/Oraclize.sol
  94. 0
      test/sources/solidity/errors/compilation-error.sol
  95. 0
      test/sources/solidity/external/CLibrary.sol
  96. 0
      test/sources/solidity/external/Face.sol
  97. 0
      test/sources/solidity/external/PureView.sol
  98. 38
      test/units/app.js
  99. 121
      test/util/mock.truffle.js
  100. 199
      test/util/mockTruffle.js
  101. Some files were not shown because too many files have changed in this diff Show More

@ -25,10 +25,10 @@
test: await truffle.test.run(config)
*/
const SolidityCoverage = require('./../lib/app.js');
const App = require('./../lib/app');
const req = require('req-cwd');
module.exports = async (config) =>
module.exports = async (truffleConfig) =>
let error;
try {
@ -38,26 +38,26 @@ module.exports = async (config) =>
const coverageConfig = req.silent('./.solcover.js') || {};
// Start
const app = new SolidityCoverage(coverageConfig);
const app = new App(coverageConfig);
// Write instrumented sources to temp folder
app.contractsDirectory = coveragePaths.contracts(config, app);
app.generateEnvironment(config.contracts_directory, app.contractsDirectory);
app.contractsDirectory = coveragePaths.contracts(truffleConfig, app);
app.generateEnvironment(truffleConfig.contracts_directory, app.contractsDirectory);
app.instrument();
// Have truffle use temp folders
config.contracts_directory = app.contractsDirectory;
config.build_directory = coveragePaths.artifacts.root(config, app);
config.contracts_build_directory = coveragePaths.artifacts.contracts(config, app);
truffleConfig.contracts_directory = app.contractsDirectory;
truffleConfig.build_directory = coveragePaths.artifacts.root(truffleConfig, app);
truffleConfig.contracts_build_directory = coveragePaths.artifacts.contracts(truffleConfig, app);
// Compile w/out optimization
config.compilers.solc.settings.optimization.enabled = false;
await truffle.contracts.compile(config);
truffleConfig.compilers.solc.settings.optimization.enabled = false;
await truffle.contracts.compile(truffleConfig);
// Launch provider & run tests
config.provider = await app.getCoverageProvider(truffle);
truffleConfig.provider = await app.getCoverageProvider(truffle);
try {
await truffle.test.run(config)
await truffle.test.run(truffleConfig)
} catch (e) {
error = e;
app.testsErrored = true;

@ -34,7 +34,7 @@
"@nomiclabs/buidler-web3": "^1.0.0-beta.8",
"mocha": "5.2.0",
"solc": "^0.5.10",
"truffle": "^5.0.26",
"truffle": "^5.0.31",
"web3": "1.0.0-beta.37"
}
}

@ -0,0 +1 @@
module.exports = { value: true };

@ -0,0 +1,25 @@
pragma solidity ^0.5.0;
contract Migrations {
address public owner;
uint public last_completed_migration;
modifier restricted() {
if (msg.sender == owner) { _; }
}
constructor() public {
owner = msg.sender;
}
function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}
function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}

@ -35,6 +35,25 @@ function assertExecutionFails(){
describe.skip('app', function() {
afterEach(() => mock.remove());
it('simple contract: should generate coverage, cleanup & exit(0)', () => {
assertCleanInitialState();
// Run script (exits 0);
mock.install('Simple.sol', 'simple.js', config);
shell.exec(script);
assert(shell.error() === null, 'script should not error');
assertCoverageGenerated();
// Coverage should be real.
// This test is tightly bound to the function names in Simple.sol
const produced = JSON.parse(fs.readFileSync('./coverage.json', 'utf8'));
const path = Object.keys(produced)[0];
assert(produced[path].fnMap['1'].name === 'test', 'coverage.json should map "test"');
assert(produced[path].fnMap['2'].name === 'getX', 'coverage.json should map "getX"');
});
it('config with testrpc options string: should generate coverage, cleanup & exit(0)', () => {
const privateKey = '0x3af46c9ac38ee1f01b05f9915080133f644bf57443f504d339082cb5285ccae4';
@ -71,25 +90,6 @@ describe.skip('app', function() {
});
it('simple contract: should generate coverage, cleanup & exit(0)', () => {
assertCleanInitialState();
// Run script (exits 0);
mock.install('Simple.sol', 'simple.js', config);
shell.exec(script);
assert(shell.error() === null, 'script should not error');
assertCoverageGenerated();
// Coverage should be real.
// This test is tightly bound to the function names in Simple.sol
const produced = JSON.parse(fs.readFileSync('./coverage.json', 'utf8'));
const path = Object.keys(produced)[0];
assert(produced[path].fnMap['1'].name === 'test', 'coverage.json should map "test"');
assert(produced[path].fnMap['2'].name === 'getX', 'coverage.json should map "getX"');
});
it('Oraclize @ solc v.0.4.24 (large, many unbracketed statements)', () => {
const trufflejs =
`module.exports = {

@ -0,0 +1,121 @@
/*
Utilities for generating a mock truffle project to test plugin.
*/
const fs = require('fs');
const shell = require('shelljs');
const configPath = './mock/.solcover.js';
const testPath = './test/sources/js/';
const sourcesPath = './test/sources/solidity/contracts/app/';
const migrationPath = './mock/migrations/2_deploy.js';
const defaultTruffleConfig = `
module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*"
}
},
compilers: {
solc: {
version: "0.5.3",
}
}
};
`
/**
* Installs mock truffle project at ./mock with a single contract
* and test specified by the params.
* @param {String} contract <contractName.sol> located in /test/sources/cli/
* @param {[type]} test <testName.js> located in /test/cli/
*/
function install(
contract,
test,
config,
_truffleConfig,
_trufflejsName,
noMigrations
) {
const configjs = `module.exports = ${JSON.stringify(config)}`;
const migration = `
const A = artifacts.require('${contract}');
module.exports = function(deployer) { deployer.deploy(A) };
`;
// Mock truffle-config.js
const trufflejsName = _trufflejsName || 'truffle-config.js';
const trufflejs = _truffleConfig || defaultTruffleConfig;
// Generate mock
shell.mkdir('./mock');
shell.cp('-Rf', './test/integration/truffle', './mock');
shell.cp(`${sourcesPath}${contract}.sol`, `./mock/contracts/${contract}.sol`);
if (!noMigrations){
fs.writeFileSync(migrationPath, migration);
}
fs.writeFileSync(`./mock/${trufflejsName}`, trufflejs);
fs.writeFileSync(`${configPath}`, configjs);
shell.cp(`${testPath}${test}`, `./mock/test/${test}`);
};
/**
* Installs mock truffle project with two contracts (for inheritance, libraries, etc)
* @param {config} .solcover.js configuration
*/
function installMultiple(contracts, test, config) {
const configjs = `module.exports = ${JSON.stringify(config)}`;
const deployContracts = `
var A = artifacts.require(`${contracts[0]}`);
var B = artifacts.require(`${contracts[1]}`);
module.exports = function(deployer) {
deployer.deploy(A);
deployer.link(A, B);
deployer.deploy(B);
};
`;
shell.mkdir('./mock');
shell.cp('-Rf', './test/integration/truffle', './mock');
contracts.forEach(item => {
shell.cp(`${sourcesPath}${item}.sol`, `./mock/contracts/${item}.sol`)
});
shell.cp(`${testPath}${test}`, `./mock/test/${test}`);
fs.writeFileSync('./mock/truffle-config.js', defaultTruffleJs);
fs.writeFileSync('./.solcover.js', configjs);
fs.writeFileSync(migrationPath, migration)
};
/**
* Removes mock truffle project and coverage reports generated by exec.js
*/
function remove() {
shell.config.silent = true;
shell.rm('./.solcover.js');
shell.rm('-Rf', 'mock');
shell.rm('-Rf', 'coverage');
shell.rm('coverage.json');
shell.config.silent = false;
};
module.exports = {
install: install,
installMultiple: installMultiple,
remove: remove
}

@ -1,199 +0,0 @@
/*
This file contains utilities for generating a mock truffle project to test solcover's
run script against.
*/
const fs = require('fs');
const shell = require('shelljs');
const defaultTruffleJs = `module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*"
}
},
compilers: {
solc: {
version: "0.5.3",
}
}
};`
/**
* Installs mock truffle project at ./mock with a single contract
* and test specified by the params.
* @param {String} contract <contractName.sol> located in /test/sources/cli/
* @param {[type]} test <testName.js> located in /test/cli/
*/
module.exports.install = function install(
contract,
test,
config,
_trufflejs,
_trufflejsName,
noMigrations
) {
const configjs = `module.exports = ${JSON.stringify(config)}`;
const contractLocation = `./${contract}`;
const trufflejsName = _trufflejsName || 'truffle-config.js';
// Mock migrations
const initialMigration = `
let Migrations = artifacts.require('Migrations.sol');
module.exports = function(deployer) {
deployer.deploy(Migrations);
};`;
const deployContracts = `
var contract = artifacts.require('${contractLocation}');
module.exports = function(deployer) {
deployer.deploy(contract);
};`;
// Mock external asset
const asset = 'module.exports = { value: true };';
// Mock truffle-config.js
const trufflejs = _trufflejs || defaultTruffleJs;
// Generate mock
shell.mkdir('./mock');
shell.mkdir('./mock/contracts');
shell.mkdir('./mock/migrations');
shell.mkdir('./mock/assets');
shell.cp('./test/sources/cli/SimpleError.sol', './mock/assets/SimpleError.sol');
shell.mkdir('./mock/node_modules');
shell.mkdir('./mock/test');
if (Array.isArray(contract)) {
contract.forEach(item => {
shell.cp(`./test/sources/cli/${item}`, `./mock/contracts/${item}`);
});
} else {
shell.cp(`./test/sources/cli/${contract}`, `./mock/contracts/${contract}`);
}
if (!noMigrations){
shell.cp('./test/sources/cli/Migrations.sol', './mock/contracts/Migrations.sol');
fs.writeFileSync('./mock/migrations/1_initial_migration.js', initialMigration);
fs.writeFileSync('./mock/migrations/2_deploy_contracts.js', deployContracts);
}
fs.writeFileSync(`./mock/${trufflejsName}`, trufflejs);
fs.writeFileSync('./mock/assets/asset.js', asset);
fs.writeFileSync('./.solcover.js', configjs);
shell.cp(`./test/cli/${test}`, `./mock/test/${test}`);
};
/**
* Installs mock truffle project at ./mock with two contracts - one inherits from the other
* and test specified by the params.
* @param {config} .solcover.js configuration
*/
module.exports.installInheritanceTest = function installInheritanceTest(config) {
shell.mkdir('./mock');
shell.mkdir('./mock/contracts');
shell.mkdir('./mock/migrations');
shell.mkdir('./mock/test');
// Mock contracts
shell.cp('./test/sources/cli/Proxy.sol', './mock/contracts/Proxy.sol');
shell.cp('./test/sources/cli/Owned.sol', './mock/contracts/Owned.sol');
shell.cp('./test/sources/cli/Migrations.sol', './mock/contracts/Migrations.sol');
// Mock migrations
const initialMigration = `
let Migrations = artifacts.require('Migrations.sol');
module.exports = function(deployer) {
deployer.deploy(Migrations);
};`;
const deployContracts = `
var Owned = artifacts.require('./Owned.sol');
var Proxy = artifacts.require('./Proxy.sol');
module.exports = function(deployer) {
deployer.deploy(Owned);
deployer.link(Owned, Proxy);
deployer.deploy(Proxy);
};`;
fs.writeFileSync('./mock/migrations/1_initial_migration.js', initialMigration);
fs.writeFileSync('./mock/migrations/2_deploy_contracts.js', deployContracts);
// Mock test
shell.cp('./test/cli/inheritance.js', './mock/test/inheritance.js');
// Mock truffle-config.js
const trufflejs = defaultTruffleJs;
const configjs = `module.exports = ${JSON.stringify(config)}`;
fs.writeFileSync('./mock/truffle-config.js', trufflejs);
fs.writeFileSync('./.solcover.js', configjs);
};
/**
* Installs mock truffle project at ./mock with two contracts - one is a library the other
* uses, and test specified by the params.
* @param {config} .solcover.js configuration
*/
module.exports.installLibraryTest = function installInheritanceTest(config) {
shell.mkdir('./mock');
shell.mkdir('./mock/contracts');
shell.mkdir('./mock/migrations');
shell.mkdir('./mock/test');
shell.mkdir('./mock/assets');
// Mock contracts
shell.cp('./test/sources/cli/TotallyPure.sol', './mock/contracts/TotallyPure.sol');
shell.cp('./test/sources/cli/Migrations.sol', './mock/contracts/Migrations.sol');
// Mock migrations
const initialMigration = `
let Migrations = artifacts.require('Migrations.sol');
module.exports = function(deployer) {
deployer.deploy(Migrations);
};`;
const deployContracts = `
var CLibrary = artifacts.require('CLibrary.sol');
var TotallyPure = artifacts.require('./TotallyPure.sol');
module.exports = function(deployer) {
deployer.deploy(CLibrary);
deployer.link(CLibrary, TotallyPure);
deployer.deploy(TotallyPure);
};`;
fs.writeFileSync('./mock/migrations/1_initial_migration.js', initialMigration);
fs.writeFileSync('./mock/migrations/2_deploy_contracts.js', deployContracts);
// Mock test
shell.cp('./test/cli/totallyPure.js', './mock/test/totallyPure.js');
shell.cp('./test/sources/cli/PureView.sol', './mock/assets/PureView.sol');
shell.cp('./test/sources/cli/CLibrary.sol', './mock/assets/CLibrary.sol');
shell.cp('./test/sources/cli/Face.sol', './mock/assets/Face.sol');
// Mock truffle-config.js
const trufflejs = defaultTruffleJs;
const configjs = `module.exports = ${JSON.stringify(config)}`;
fs.writeFileSync('./mock/truffle-config.js', trufflejs);
fs.writeFileSync('./.solcover.js', configjs);
};
/**
* Removes mock truffle project and coverage reports generated by exec.js
*/
module.exports.remove = function remove() {
shell.config.silent = true;
shell.rm('./.solcover.js');
shell.rm('-Rf', 'mock');
shell.rm('-Rf', 'coverage');
shell.rm('coverage.json');
shell.config.silent = false;
};

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save