Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds optional coverage for contracts in node_modules #375

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class App {
this.port = config.port || 8555; // Port testrpc should listen on
this.buildDirPath = config.buildDirPath || '/build/contracts' // Build directory path for compiled smart contracts

this.instrumentImports = config.instrumentImports || false; // Run code coverage on contracts imported from node_modules
this.copyNodeModules = config.copyNodeModules || false; // Copy node modules into coverageEnv?
this.copyPackages = config.copyPackages || []; // Only copy specific node_modules packages into coverageEnv
this.testrpcOptions = config.testrpcOptions || null; // Options for testrpc-sc
Expand Down Expand Up @@ -134,6 +135,28 @@ class App {

this.runCompileCommand();
this.originalArtifacts = this.loadArtifacts();

// If instrumentImports setting is true, use the contract artifacts to
// find the path to all imported contracts. If these paths lead outside
// of the /contracts directory (ex. node_modules), copy the contracts
// into the coverage environment and save their paths in npmContracts.
this.npmContracts = [];

if (this.instrumentImports) {
this.npmContracts = this.originalArtifacts
.map(artifact => artifact['sourcePath'])
.filter(path => !path.includes('coverageEnv'))
.map((file) => {
let originalPath = `node_modules/${file}`;
let coveragePath = `${this.coverageDir}/${originalPath}`;
if (!shell.test('-e', `${this.workingDir}/${coveragePath}`)) {
shell.mkdir('-p', path.dirname(coveragePath));
shell.cp('-L', originalPath, path.dirname(coveragePath));
}
return coveragePath;
});
}

shell.rm('-Rf', `${this.coverageDir}${this.buildDirPath}`);

} catch (err) {
Expand All @@ -157,7 +180,7 @@ class App {
const instrumentedFiles = [];
let currentFile;
try {
shell.ls(`${this.coverageDir}/contracts/**/*.sol`).forEach(file => {
shell.ls(`${this.coverageDir}/contracts/**/*.sol`).concat(this.npmContracts).forEach(file => {
currentFile = file;

if (!this.skipFiles.includes(file) && !this.inSkippedFolder(file)) {
Expand Down