Code coverage for Solidity smart-contracts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
solidity-coverage/test/util/mockTruffle.js

93 lines
2.9 KiB

/*
This file contains utilities for generating a mock truffle project to test solcover's
run script against.
*/
const assert = require('assert');
const fs = require('fs');
const shell = require('shelljs');
/**
* Moves existing coverage reports into a safe place while testing run script which
* would overwrite them. Silences shell complaints about non-existent files.
*/
module.exports.protectCoverage = function () {
shell.config.silent = true;
shell.rm('-Rf', './safe');
shell.mkdir('./safe');
shell.mv('./coverage', './safe/coverage');
shell.mv('./coverage.json', './safe/coverage.json');
shell.config.silent = false;
};
/**
* Restores pre-existing coverage reports after testing run script.
* Silences shell complaints about non-existent files.
*/
module.exports.restoreCoverage = function () {
shell.config.silent = true;
shell.mv('./safe/coverage', './coverage');
shell.mv('./safe/coverage.json', './coverage.json');
shell.rm('-Rf', './safe');
shell.config.silent = false;
};
/**
* 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/run/
* @param {[type]} test <testName.js> located in /test/run/
*/
module.exports.install = function (contract, test) {
shell.mkdir('./mock');
shell.mkdir('./mock/contracts');
shell.mkdir('./mock/migrations');
shell.mkdir('./mock/test');
// Mock contracts
shell.cp(`./test/sources/run/${contract}`, `./mock/contracts/${contract}`);
shell.cp('./test/sources/run/Migrations.sol', './mock/contracts/Migrations.sol');
// Mock migrations
const initialMigration = `
let Migrations = artifacts.require('Migrations.sol');
module.exports = function(deployer) {
deployer.deploy(Migrations);
};`;
const contractLocation = './' + contract;
const deployContracts = `
var contract = artifacts.require('${contractLocation}');
module.exports = function(deployer) {
deployer.deploy(contract);
};`;
fs.writeFileSync('./mock/migrations/1_initial_migration.js', initialMigration);
fs.writeFileSync('./mock/migrations/2_deploy_contracts.js', deployContracts);
// Mock test
shell.cp(`./test/run/${test}`, `./mock/test/${test}`);
// Mock truffle.js
const trufflejs = `module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*"
}}};`
;
fs.writeFileSync('./mock/truffle.js', trufflejs);
};
/**
* Removes mock truffle project and coverage reports generated by runCovered tests
*/
module.exports.remove = function () {
shell.config.silent = true;
shell.rm('-Rf', 'mock');
shell.rm('-Rf', 'coverage');
shell.rm('coverage.json');
shell.config.silent = false;
};