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.
284 lines
7.2 KiB
284 lines
7.2 KiB
5 years ago
|
|
||
|
/*
|
||
|
Utilities for generating a mock truffle project to test plugin.
|
||
|
*/
|
||
|
|
||
|
const path = require('path');
|
||
|
const fs = require('fs');
|
||
|
const shell = require('shelljs');
|
||
|
const TruffleConfig = require('truffle-config');
|
||
|
const decache = require('decache');
|
||
|
|
||
5 years ago
|
const temp = './sc_temp';
|
||
5 years ago
|
const truffleConfigName = 'truffle-config.js';
|
||
5 years ago
|
const buidlerConfigName = 'buidler.config.js';
|
||
5 years ago
|
const configPath = `${temp}/.solcover.js`;
|
||
|
const testPath = './test/sources/js/';
|
||
|
const sourcesPath = './test/sources/solidity/contracts/app/';
|
||
|
const migrationPath = `${temp}/migrations/2_deploy.js`;
|
||
5 years ago
|
const templatePath = './test/integration/generic/*';
|
||
5 years ago
|
const projectPath = './test/integration/projects/'
|
||
5 years ago
|
|
||
5 years ago
|
|
||
|
// ==========================
|
||
|
// Misc Utils
|
||
|
// ==========================
|
||
|
function decacheConfigs(){
|
||
|
decache(`${process.cwd()}/${temp}/.solcover.js`);
|
||
|
decache(`${process.cwd()}/${temp}/${truffleConfigName}`);
|
||
5 years ago
|
decache(`${process.cwd()}/${temp}/${buidlerConfigName}`);
|
||
5 years ago
|
}
|
||
|
|
||
|
function clean() {
|
||
|
shell.config.silent = true;
|
||
|
|
||
|
shell.rm('-Rf', temp);
|
||
|
shell.rm('-Rf', 'coverage');
|
||
|
shell.rm('coverage.json');
|
||
|
shell.rm('.solcover.js');
|
||
|
|
||
|
shell.config.silent = false;
|
||
|
};
|
||
|
|
||
5 years ago
|
function pathToContract(config, file) {
|
||
|
return path.join('contracts', file);
|
||
|
}
|
||
|
|
||
|
function getOutput(truffleConfig){
|
||
|
const jsonPath = path.join(truffleConfig.working_directory, "coverage.json");
|
||
|
return JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
|
||
|
}
|
||
|
|
||
5 years ago
|
// ==========================
|
||
5 years ago
|
// Truffle Configuration
|
||
5 years ago
|
// ==========================
|
||
5 years ago
|
function getDefaultTruffleConfig(){
|
||
|
const logger = process.env.SILENT ? { log: () => {} } : console;
|
||
|
const reporter = process.env.SILENT ? 'dot' : 'spec';
|
||
|
|
||
|
const mockwd = path.join(process.cwd(), temp);
|
||
|
const vals = {
|
||
|
working_directory: mockwd,
|
||
|
build_directory: path.join(mockwd, 'build'),
|
||
|
contracts_directory: path.join(mockwd, 'contracts'),
|
||
|
contracts_build_directory: path.join(mockwd, 'build', 'contracts'),
|
||
|
migrations_directory: path.join(mockwd, 'migrations'),
|
||
|
test_directory: path.join(mockwd, 'test'),
|
||
|
logger: logger,
|
||
|
mocha: { reporter: reporter },
|
||
|
networks: {
|
||
|
development: {
|
||
|
host: "localhost",
|
||
|
port: 8545,
|
||
|
network_id: "*"
|
||
|
}
|
||
|
},
|
||
|
compilers: {
|
||
|
solc: {
|
||
|
version: "0.5.3",
|
||
|
settings: { optimizer: {} }
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return (new TruffleConfig()).with(vals);
|
||
|
}
|
||
|
|
||
5 years ago
|
function getTruffleConfigJS(config){
|
||
|
if (config) {
|
||
|
return `module.exports = ${JSON.stringify(config, null, ' ')}`;
|
||
|
} else {
|
||
|
return `module.exports = ${JSON.stringify(getDefaultTruffleConfig(), null, ' ')}`;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// ==========================
|
||
|
// Buidler Configuration
|
||
|
// ==========================
|
||
|
function getDefaultBuidlerConfig() {
|
||
|
const logger = process.env.SILENT ? { log: () => {} } : console;
|
||
|
const reporter = process.env.SILENT ? 'dot' : 'spec';
|
||
|
|
||
|
const mockwd = path.join(process.cwd(), temp);
|
||
|
const vals = {
|
||
|
root: mockwd,
|
||
|
artifacts: path.join(mockwd, 'artifacts'),
|
||
|
cache: path.join(mockwd, 'cache'),
|
||
|
sources: path.join(mockwd, 'contracts'),
|
||
|
tests: path.join(mockwd, 'test'),
|
||
|
logger: logger,
|
||
|
mocha: {
|
||
|
reporter: reporter
|
||
|
},
|
||
|
networks: {
|
||
|
development: {
|
||
|
url: "http://127.0.0.1:8545",
|
||
|
}
|
||
|
},
|
||
|
solc: {
|
||
|
version: "0.5.3",
|
||
|
optimizer: {}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return vals;
|
||
|
}
|
||
|
|
||
|
function getBuidlerConfigJS(config){
|
||
|
if (config) {
|
||
|
return `module.exports = ${JSON.stringify(config, null, ' ')}`
|
||
|
} else {
|
||
|
return `module.exports = ${JSON.stringify(getDefaultBuidlerConfig(), null, ' ')}`
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// ==========================
|
||
|
// .solcover.js Configuration
|
||
|
// ==========================
|
||
5 years ago
|
function getSolcoverJS(config){
|
||
|
return `module.exports = ${JSON.stringify(config, null, ' ')}`
|
||
|
}
|
||
|
|
||
|
|
||
5 years ago
|
// ==========================
|
||
|
// Migration Generators
|
||
|
// ==========================
|
||
5 years ago
|
function deploySingle(contractName){
|
||
|
return `
|
||
|
const A = artifacts.require("${contractName}");
|
||
|
module.exports = function(deployer) { deployer.deploy(A) };
|
||
|
`;
|
||
|
}
|
||
|
|
||
|
function deployDouble(contractNames){
|
||
|
return `
|
||
|
var A = artifacts.require("${contractNames[0]}");
|
||
|
var B = artifacts.require("${contractNames[1]}");
|
||
|
module.exports = function(deployer) {
|
||
|
deployer.deploy(A);
|
||
|
deployer.link(A, B);
|
||
|
deployer.deploy(B);
|
||
|
};
|
||
|
`;
|
||
|
}
|
||
|
|
||
5 years ago
|
// ==========================
|
||
|
// Project Installers
|
||
|
// ==========================
|
||
5 years ago
|
/**
|
||
5 years ago
|
* Installs mock truffle/buidler project at ./temp with a single contract
|
||
5 years ago
|
* 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,
|
||
5 years ago
|
solcoverConfig,
|
||
|
devPlatformConfig,
|
||
5 years ago
|
noMigrations
|
||
|
) {
|
||
|
|
||
5 years ago
|
let configjs;
|
||
5 years ago
|
if(solcoverConfig) solcoverJS = getSolcoverJS(solcoverConfig);
|
||
5 years ago
|
|
||
5 years ago
|
const trufflejs = getTruffleConfigJS(devPlatformConfig);
|
||
|
const buidlerjs = getBuidlerConfigJS(devPlatformConfig);
|
||
5 years ago
|
const migration = deploySingle(contract);
|
||
|
|
||
|
// Scaffold
|
||
|
shell.mkdir(temp);
|
||
|
shell.cp('-Rf', templatePath, temp);
|
||
|
|
||
|
// Contract
|
||
|
shell.cp(`${sourcesPath}${contract}.sol`, `${temp}/contracts/${contract}.sol`);
|
||
|
|
||
|
// Migration
|
||
|
if (!noMigrations) fs.writeFileSync(migrationPath, migration);
|
||
|
|
||
|
// Test
|
||
|
shell.cp(`${testPath}${test}`, `${temp}/test/${test}`);
|
||
|
|
||
|
// Configs
|
||
|
fs.writeFileSync(`${temp}/${truffleConfigName}`, trufflejs);
|
||
5 years ago
|
fs.writeFileSync(`${temp}/${buidlerConfigName}`, buidlerjs);
|
||
|
if(solcoverConfig) fs.writeFileSync(configPath, solcoverJS);
|
||
5 years ago
|
|
||
5 years ago
|
decacheConfigs();
|
||
5 years ago
|
};
|
||
|
|
||
|
/**
|
||
5 years ago
|
* Installs mock truffle/buidler project with two contracts (for inheritance, libraries, etc)
|
||
5 years ago
|
*/
|
||
|
function installDouble(contracts, test, config) {
|
||
|
const configjs = getSolcoverJS(config);
|
||
|
const migration = deployDouble(contracts);
|
||
|
|
||
|
// Scaffold
|
||
|
shell.mkdir(temp);
|
||
|
shell.cp('-Rf', templatePath, temp);
|
||
|
|
||
|
// Contracts
|
||
|
contracts.forEach(item => {
|
||
|
shell.cp(`${sourcesPath}${item}.sol`, `${temp}/contracts/${item}.sol`)
|
||
|
});
|
||
|
|
||
|
// Migration
|
||
|
fs.writeFileSync(migrationPath, migration)
|
||
|
|
||
|
// Test
|
||
|
shell.cp(`${testPath}${test}`, `${temp}/test/${test}`);
|
||
|
|
||
|
// Configs
|
||
|
fs.writeFileSync(`${temp}/${truffleConfigName}`, getTruffleConfigJS());
|
||
5 years ago
|
fs.writeFileSync(`${temp}/${buidlerConfigName}`, getBuidlerConfigJS());
|
||
5 years ago
|
fs.writeFileSync(configPath, configjs);
|
||
|
|
||
5 years ago
|
decacheConfigs();
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
/**
|
||
|
* Installs full truffle/buidler project
|
||
|
*/
|
||
5 years ago
|
function installFullProject(name, config) {
|
||
5 years ago
|
shell.mkdir(temp);
|
||
|
shell.cp('-Rf', `${projectPath}${name}/{.,}*`, temp);
|
||
|
|
||
5 years ago
|
if (config){
|
||
|
const configjs = getSolcoverJS(config);
|
||
|
fs.writeFileSync(`${temp}/.solcover.js`, configjs);
|
||
|
}
|
||
|
|
||
5 years ago
|
decacheConfigs();
|
||
|
}
|
||
|
|
||
5 years ago
|
// ==========================
|
||
|
// Logging
|
||
|
// ==========================
|
||
|
const loggerOutput = {
|
||
|
val: ''
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
const testLogger = {
|
||
|
log: (val) => {
|
||
|
if (val !== undefined) loggerOutput.val += val;
|
||
|
if (!process.env.SILENT && val !== undefined)
|
||
|
console.log(val)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
|
||
|
module.exports = {
|
||
5 years ago
|
testLogger: testLogger,
|
||
|
loggerOutput: loggerOutput,
|
||
5 years ago
|
getDefaultTruffleConfig: getDefaultTruffleConfig,
|
||
5 years ago
|
getDefaultBuidlerConfig: getDefaultBuidlerConfig,
|
||
5 years ago
|
install: install,
|
||
|
installDouble: installDouble,
|
||
5 years ago
|
installFullProject: installFullProject,
|
||
5 years ago
|
clean: clean,
|
||
|
pathToContract: pathToContract,
|
||
|
getOutput: getOutput
|
||
5 years ago
|
}
|
||
|
|