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 | 1x 1x 1x 1x 33x 33x 33x 2x 2x 2x 2x 58x 58x 38x 38x 38x 38x 38x 34x 34x 1x 33x 33x 34x 34x 34x 34x 34x 34x 58x 2x 56x 34x 34x 34x 9x 34x 9x 19x 2x 34x 39x 39x 39x 39x 38x 38x 1x 1x 1x 38x 38x 38x 38x 38x 38x 2x 38x 1x 1x 37x 32x 32x 1x | /** * A collection of utilities for common tasks plugins will need in the course * of composing a workflow using the solidity-coverage API */ const PluginUI = require('./plugin.ui'); const path = require('path'); const fs = require('fs-extra'); const shell = require('shelljs'); // === // UI // === /** * Displays a list of skipped contracts * @param {TruffleConfig} config * @return {Object[]} skipped array of objects generated by `assembleTargets` method */ function reportSkipped(config, skipped=[]){ let started = false; const ui = new PluginUI(config.logger.log); for (let item of skipped){ Eif (!started) { ui.report('instr-skip', []); started = true; } ui.report('instr-skipped', [item.relativePath]); } } // ======== // File I/O // ======== /** * Loads source * @param {String} _path absolute path * @return {String} source file */ function loadSource(_path){ return fs.readFileSync(_path).toString(); } /** * Sets up temporary folders for instrumented contracts and their compilation artifacts * @param {PlatformConfig} config * @param {String} tempContractsDir * @param {String} tempArtifactsDir */ function setupTempFolders(config, tempContractsDir, tempArtifactsDir){ checkContext(config, tempContractsDir, tempArtifactsDir); shell.mkdir(tempContractsDir); shell.mkdir(tempArtifactsDir); } /** * Save a set of instrumented files to a temporary directory. * @param {Object[]} targets array of targets generated by `assembleTargets` * @param {[type]} originalDir absolute path to original contracts directory * @param {[type]} tempDir absolute path to temp contracts directory */ function save(targets, originalDir, tempDir){ let _path; for (target of targets) { _path = path.normalize(target.canonicalPath) .replace(originalDir, tempDir); fs.outputFileSync(_path, target.source); } } /** * Relativizes an absolute file path, given an absolute parent path * @param {String} pathToFile * @param {String} pathToParent * @return {String} relative path */ function toRelativePath(pathToFile, pathToParent){ return pathToFile.replace(`${pathToParent}${path.sep}`, ''); } /** * Returns a pair of canonically named temporary directory paths for contracts * and artifacts. Instrumented assets can be written & compiled to these. * Then the unit tests can be run, consuming them as sources. * @param {TruffleConfig} config * @return {Object} temp paths */ function getTempLocations(config){ const contractsRoot = path.parse(config.contractsDir).dir const cwd = config.workingDir; const contractsDirName = config.coverageContractsTemp || '.coverage_contracts'; const artifactsDirName = config.temp || '.coverage_artifacts'; return { tempContractsDir: path.join(contractsRoot, contractsDirName), tempArtifactsDir: path.join(cwd, artifactsDirName) } } /** * Checks for existence of contract sources, and sweeps away debris * left over from an uncontrolled crash. */ function checkContext(config, tempContractsDir, tempArtifactsDir){ const ui = new PluginUI(config.logger.log); if (!shell.test('-e', config.contractsDir)){ const msg = ui.generate('sources-fail', [config.contractsDir]) throw new Error(msg); } if (shell.test('-e', tempContractsDir)){ shell.rm('-Rf', tempContractsDir); } if (shell.test('-e', tempArtifactsDir)){ shell.rm('-Rf', tempArtifactsDir); } } // ============================= // Instrumentation Set Assembly // ============================= function assembleFiles(config, skipFiles=[]){ let targets; let targetsPath; // The targets (contractsDir) could actually be a single named file (OR a folder) const extName = path.extname(config.contractsDir); if (extName.length !== 0) { targets = [ path.normalize(config.contractsDir) ]; } else { targetsPath = path.join(config.contractsDir, '**', '*.{sol,vy}'); targets = shell.ls(targetsPath).map(path.normalize); } skipFiles = assembleSkipped(config, targets, skipFiles); return assembleTargets(config, targets, skipFiles) } function assembleTargets(config, targets=[], skipFiles=[]){ const skipped = []; const filtered = []; const cd = config.contractsDir; for (let target of targets){ if (skipFiles.includes(target) || path.extname(target) === '.vy'){ skipped.push({ canonicalPath: target, relativePath: toRelativePath(target, cd), source: loadSource(target) }) } else { filtered.push({ canonicalPath: target, relativePath: toRelativePath(target, cd), source: loadSource(target) }) } } return { skipped: skipped, targets: filtered } } /** * Parses the skipFiles option (which also accepts folders) */ function assembleSkipped(config, targets, skipFiles=[]){ // Make paths absolute skipFiles = skipFiles.map(contract => path.join(config.contractsDir, contract)); // Enumerate files in skipped folders const skipFolders = skipFiles.filter(item => { return path.extname(item) !== '.sol' || path.extname(item) !== '.vy' }); for (let folder of skipFolders){ for (let target of targets ) { if (target.indexOf(folder) === 0) skipFiles.push(target); } }; return skipFiles; } function loadSolcoverJS(config={}){ let solcoverjs; let coverageConfig; let log = config.logger ? config.logger.log : console.log; let ui = new PluginUI(log); // Handle --solcoverjs flag (config.solcoverjs) ? solcoverjs = path.join(config.workingDir, config.solcoverjs) : solcoverjs = path.join(config.workingDir, '.solcover.js'); // Catch solcoverjs syntax errors if (shell.test('-e', solcoverjs)){ try { coverageConfig = require(solcoverjs); } catch(error){ error.message = ui.generate('solcoverjs-fail') + error.message; throw new Error(error) } // Config is optional } else { coverageConfig = {}; } // viaIR is eval'd in `nomiclab.utils.normalizeConfig` coverageConfig.viaIR = config.viaIR; coverageConfig.log = log; coverageConfig.cwd = config.workingDir; coverageConfig.originalContractsDir = config.contractsDir; // Solidity-Coverage writes to Truffle config config.mocha = config.mocha || {}; if (coverageConfig.mocha && typeof coverageConfig.mocha === 'object'){ config.mocha = Object.assign( config.mocha, coverageConfig.mocha ); } // Per fvictorio recommendation in #691 if (config.mocha.parallel) { const message = ui.generate('mocha-parallel-fail'); throw new Error(message); } return coverageConfig; } // ========================== // Setup RPC Calls // ========================== async function getAccountsHardhat(provider){ return provider.send("eth_accounts", []) } async function getNodeInfoHardhat(provider){ return provider.send("web3_clientVersion", []) } // ========================== // Finishing / Cleanup // ========================== /** * Silently removes temporary folders and calls api.finish to shut server down * @param {TruffleConfig} config * @param {SolidityCoverage} api * @return {Promise} */ async function finish(config, api){ const { tempContractsDir, tempArtifactsDir } = getTempLocations(config); shell.config.silent = true; shell.rm('-Rf', tempContractsDir); shell.rm('-Rf', tempArtifactsDir); shell.config.silent = false; if (api) await api.finish(); } module.exports = { assembleFiles, assembleSkipped, assembleTargets, checkContext, finish, getTempLocations, loadSource, loadSolcoverJS, reportSkipped, save, toRelativePath, setupTempFolders, getAccountsHardhat, getNodeInfoHardhat, } |