All files / solidity-coverage/lib api.js

98.35% Statements 119/121
91.53% Branches 54/59
100% Functions 20/20
99.15% Lines 117/118

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 3171x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x             50x 50x 50x 50x     50x 49x 49x     49x   49x 49x 49x 49x 49x   49x 49x 49x 49x 49x 49x   49x   49x 49x 49x 49x   49x 49x   49x 49x   49x 49x                               36x 36x   36x 36x 57x   57x 36x 36x     57x   57x         56x   56x               1x 1x     35x                 4x                 1x             30x   30x 30x   30x 30x 30x   30x 30x   30x   64x     29x 29x   29x 29x       1x 1x                                   111x 111x   111x             111x     111x 364x   111x                                         6x 6x 6x   6x           114x   114x 42x 2x   42x 7x     42x   8x 8x 3x 1x 1x       8x 7x       8x                     30x 30x       1x 1x 1x       1x 1x       1x 1x                           31x   31x   52x   31x                       49x         1x  
const shell = require('shelljs');
const pify = require('pify');
const fs = require('fs');
const path = require('path');
const istanbul = require('sc-istanbul');
const assert = require('assert');
const _ = require('lodash/lang');
 
const ConfigValidator = require('./validator');
const Instrumenter = require('./instrumenter');
const Coverage = require('./coverage');
const DataCollector = require('./collector');
const AppUI = require('./ui').AppUI;
const AbiUtils = require('./abi');
 
/**
 * Coverage Runner
 */
class API {
  constructor(config={}) {
    this.validator = new ConfigValidator();
    this.abiUtils = new AbiUtils();
    this.config = config || {};
    this.testMatrix = {};
 
    // Validate
    this.validator.validate(this.config);
    this.coverage = new Coverage();
    this.instrumenter = new Instrumenter(this.config);
 
    // Options
    this.testsErrored = false;
 
    this.cwd = config.cwd || process.cwd();
    this.abiOutputPath = config.abiOutputPath || "humanReadableAbis.json";
    this.matrixOutputPath = config.matrixOutputPath || "testMatrix.json";
    this.mochaJsonOutputPath = config.mochaJsonOutputPath || "mochaOutput.json";
    this.matrixReporterPath = config.matrixReporterPath || "solidity-coverage/plugins/resources/matrix.js"
 
    this.defaultHook = () => {};
    this.onServerReady = config.onServerReady           || this.defaultHook;
    this.onTestsComplete = config.onTestsComplete       || this.defaultHook;
    this.onCompileComplete = config.onCompileComplete   || this.defaultHook;
    this.onIstanbulComplete = config.onIstanbulComplete || this.defaultHook;
    this.onPreCompile = config.onPreCompile             || this.defaultHook;
 
    this.skipFiles = config.skipFiles || [];
 
    this.log = config.log || console.log;
    this.gasLimit = 0xffffffffff                // default "gas sent" with transactions
    this.gasLimitNumber = 0x1fffffffffffff;     // block gas limit for Hardhat
    this.gasPrice = 0x01;
 
    this.istanbulFolder = config.istanbulFolder || false;
    this.istanbulReporter = config.istanbulReporter || ['html', 'lcov', 'text', 'json'];
 
    this.viaIR = config.viaIR;
    this.solcOptimizerDetails = config.solcOptimizerDetails;
 
    this.setLoggingLevel(config.silent);
    this.ui = new AppUI(this.log);
  }
 
  /**
   * Instruments a set of sources to prepare them for running under coverage
   * @param  {Object[]}  targets (see below)
   * @return {Object[]}          (see below)
   * @example of input/output array:
   * [{
   *   source:         (required) <solidity-source>,
   *   canonicalPath:  (required) <absolute path to source file>
   *   relativePath:   (optional) <rel path to source file for logging>
   * }]
   */
  instrument(targets=[]) {
    let currentFile;      // Keep track of filename in case we crash...
    let started = false;
    let outputs = [];
 
    try {
      for (let target of targets) {
        currentFile = target.relativePath || target.canonicalPath;
 
        if(!started){
          started = true;
          this.ui.report('instr-start');
        }
 
        this.ui.report('instr-item', [currentFile]);
 
        const instrumented = this.instrumenter.instrument(
          target.source,
          target.canonicalPath
        );
 
        this.coverage.addContract(instrumented, target.canonicalPath);
 
        outputs.push({
          canonicalPath: target.canonicalPath,
          relativePath: target.relativePath,
          source: instrumented.contract
        })
      }
 
    } catch (err) {
      err.message = this.ui.generate('instr-fail', [currentFile]) + err.message;
      throw err;
    }
 
    return outputs;
  }
 
  /**
   * Returns a copy of the hit map created during instrumentation.
   * Useful if you'd like to delegate coverage collection to multiple processes.
   * @return {Object} instrumentationData
   */
  getInstrumentationData(){
    return _.cloneDeep(this.instrumenter.instrumentationData)
  }
 
  /**
   * Sets the hit map object generated during instrumentation. Useful if you'd like
   * to collect data for a pre-existing instrumentation.
   * @param {Object} data
   */
  setInstrumentationData(data={}){
    this.instrumenter.instrumentationData = _.cloneDeep(data);
  }
 
  /**
   * Generate coverage / write coverage report / run istanbul
   */
  async report(_folder) {
    const folder = _folder || this.istanbulFolder;
 
    const collector = new istanbul.Collector();
    const reporter = new istanbul.Reporter(false, folder);
 
    return new Promise((resolve, reject) => {
      try {
        this.coverage.generate(this.instrumenter.instrumentationData);
 
        const mapping = this.makeKeysRelative(this.coverage.data, this.cwd);
        this.saveCoverage(mapping);
 
        collector.add(mapping);
 
        this.istanbulReporter.forEach(report => reporter.add(report));
 
        // Pify doesn't like this one...
        reporter.write(collector, true, (err) => {
          Iif (err) return reject(err);
 
          this.ui.report('istanbul');
          resolve();
        });
 
      } catch (error) {
        error.message = this.ui.generate('istanbul-fail') + error.message;
        throw error;
      }
    })
  }
 
  /**
   * Removes coverage build artifacts, kills testrpc.
   */
  async finish() { /* Just a stub now - used to shutdown ganache */}
 
  // ------------------------------------------ Utils ----------------------------------------------
 
  // ========
  // Provider
  // ========
 
  // Hardhat
  async attachToHardhatVM(provider){
    const self = this;
    this.collector = new DataCollector(this.instrumenter.instrumentationData, this.viaIR);
 
    Iif ('init' in provider) {
      // Newer versions of Hardhat initialize the provider lazily, so we need to
      // call provider.init() explicitly. This is a no-op if the provider is
      // already initialized.
      await provider.init();
    }
 
    let cur = provider;
 
    // Go down to core HardhatNetworkProvider
    while (cur._wrapped) {
      cur = Object.assign({}, cur._wrapped)
    }
    cur._node._vm.evm.events.on('step', self.collector.step.bind(self.collector))
  }
 
  // Temporarily disabled because some relevant traces aren't available
  // (maybe bytecode cannot be found)
  /*hardhatTraceHandler(trace, isTraceFromCall){
    for (const step of trace.steps){
      if (trace.bytecode && trace.bytecode._pcToInstruction){
        const instruction = trace.bytecode._pcToInstruction.get(step.pc)
        this.collector.trackHardhatEVMInstruction(instruction)
      }
    }
  }*/
 
  // ==========================
  // Test Matrix Data Collector
  // ==========================
  /**
   * @param  {Object} testInfo Mocha object passed to reporter 'test end' event
   */
  collectTestMatrixData(testInfo){
    const hashes = Object.keys(this.instrumenter.instrumentationData);
    const title = testInfo.title;
    const file = path.relative(this.cwd, testInfo.file);
 
    for (const hash of hashes){
      const {
        contractPath,
        hits,
        type,
        id
      } = this.instrumenter.instrumentationData[hash];
 
      if (type === 'line'){
        if (!this.testMatrix[contractPath]){
          this.testMatrix[contractPath] = {};
        }
        if (!this.testMatrix[contractPath][id]){
          this.testMatrix[contractPath][id] = [];
        }
 
        if (hits > 0){
          // Search for and exclude duplicate entries
          let duplicate = false;
          for (const item of this.testMatrix[contractPath][id]){
            if (item.title === title && item.file === file){
              duplicate = true;
              break;
            }
          }
 
          if (!duplicate) {
            this.testMatrix[contractPath][id].push({title, file});
          }
 
          // Reset line data
          this.instrumenter.instrumentationData[hash].hits = 0;
        }
      }
    }
  }
 
  // ========
  // File I/O
  // ========
 
  saveCoverage(data){
    const covPath = path.join(this.cwd, "coverage.json");
    fs.writeFileSync(covPath, JSON.stringify(data));
  }
 
  saveTestMatrix(){
    const matrixPath = path.join(this.cwd, this.matrixOutputPath);
    const mapping = this.makeKeysRelative(this.testMatrix, this.cwd);
    fs.writeFileSync(matrixPath, JSON.stringify(mapping, null, ' '));
  }
 
  saveMochaJsonOutput(data){
    const outputPath = path.join(this.cwd, this.mochaJsonOutputPath);
    fs.writeFileSync(outputPath, JSON.stringify(data, null, ' '));
  }
 
  saveHumanReadableAbis(data){
    const abiPath = path.join(this.cwd, this.abiOutputPath);
    fs.writeFileSync(abiPath, JSON.stringify(data, null, ' '));
  }
 
  // =====
  // Paths
  // =====
  //
  /**
   * Relativizes path keys so that istanbul report can be read on Windows
   * @param  {Object} map  coverage map generated by coverageMap
   * @param  {String} wd   working directory
   * @return {Object}      map with relativized keys
   */
  makeKeysRelative(map, wd) {
    const newCoverage = {};
 
    Object
     .keys(map)
     .forEach(pathKey => newCoverage[path.relative(wd, pathKey)] = map[pathKey]);
 
    return newCoverage;
  }
 
  // =======
  // Logging
  // =======
 
  /**
   * Turn logging off (for CI)
   * @param {Boolean} isSilent
   */
  setLoggingLevel(isSilent) {
    if (isSilent) this.log = () => {};
  }
 
}
 
module.exports = API;