|
27 | 27 |
|
28 | 28 | const App = require('./../lib/app'); |
29 | 29 | const req = require('req-cwd'); |
30 | | - |
31 | | -module.exports = async (truffleConfig) => |
| 30 | +const path = require('path'); |
| 31 | +const dir = require('node-dir'); |
| 32 | +const Web3 = require('web3'); |
| 33 | +const util = require('util'); |
| 34 | +const ganache = require('ganache-core-sc'); |
| 35 | + |
| 36 | +module.exports = async function(truffleConfig){ |
| 37 | + let app; |
32 | 38 | let error; |
| 39 | + let testsErrored = false; |
33 | 40 |
|
34 | 41 | try { |
35 | | - |
36 | 42 | // Load truffle lib & coverage config |
37 | 43 | const truffle = loadTruffleLibrary(); |
38 | | - const coverageConfig = req.silent('./.solcover.js') || {}; |
| 44 | + |
| 45 | + const coverageConfigPath = path.join(truffleConfig.working_directory, '.solcover.js'); |
| 46 | + const coverageConfig = req.silent(coverageConfigPath) || {}; |
| 47 | + |
| 48 | + coverageConfig.cwd = truffleConfig.working_directory; |
| 49 | + coverageConfig.contractsDir = truffleConfig.contracts_directory; |
39 | 50 |
|
40 | 51 | // Start |
41 | | - const app = new App(coverageConfig); |
| 52 | + app = new App(coverageConfig); |
42 | 53 |
|
43 | 54 | // Write instrumented sources to temp folder |
44 | | - app.contractsDirectory = coveragePaths.contracts(truffleConfig, app); |
45 | | - app.generateEnvironment(truffleConfig.contracts_directory, app.contractsDirectory); |
46 | 55 | app.instrument(); |
47 | 56 |
|
48 | | - // Have truffle use temp folders |
49 | | - truffleConfig.contracts_directory = app.contractsDirectory; |
50 | | - truffleConfig.build_directory = coveragePaths.artifacts.root(truffleConfig, app); |
51 | | - truffleConfig.contracts_build_directory = coveragePaths.artifacts.contracts(truffleConfig, app); |
| 57 | + // Ask truffle to use temp folders |
| 58 | + truffleConfig.contracts_directory = paths.contracts(app); |
| 59 | + truffleConfig.build_directory = paths.build(app); |
| 60 | + truffleConfig.contracts_build_directory = paths.artifacts(truffleConfig, app); |
52 | 61 |
|
53 | | - // Compile w/out optimization |
54 | | - truffleConfig.compilers.solc.settings.optimization.enabled = false; |
| 62 | + // Additional config |
| 63 | + truffleConfig.all = true; |
| 64 | + truffleConfig.test_files = tests(truffleConfig); |
| 65 | + truffleConfig.compilers.solc.settings.optimizer.enabled = false; |
| 66 | + |
| 67 | + // Compile |
55 | 68 | await truffle.contracts.compile(truffleConfig); |
56 | 69 |
|
57 | | - // Launch provider & run tests |
58 | | - truffleConfig.provider = await app.getCoverageProvider(truffle); |
| 70 | + // Launch in-process provider |
| 71 | + const networkName = 'soliditycoverage'; |
| 72 | + const provider = await app.provider(ganache); |
| 73 | + const accounts = await (new Web3(provider)).eth.getAccounts(); |
| 74 | + |
| 75 | + truffleConfig.provider = provider; |
| 76 | + truffleConfig.network = networkName; |
| 77 | + truffleConfig.network_id = "*"; |
| 78 | + truffleConfig.networks[networkName] = { |
| 79 | + network_id: truffleConfig.network_id, |
| 80 | + provider: truffleConfig.provider, |
| 81 | + gas: app.gasLimit, |
| 82 | + gasPrice: app.gasPrice, |
| 83 | + from: accounts[0] |
| 84 | + } |
| 85 | + |
| 86 | + // Run tests |
59 | 87 | try { |
60 | | - await truffle.test.run(truffleConfig) |
| 88 | + failures = await truffle.test.run(truffleConfig) |
61 | 89 | } catch (e) { |
62 | | - error = e; |
63 | | - app.testsErrored = true; |
| 90 | + error = e.stack; |
64 | 91 | } |
65 | 92 |
|
66 | | - // Produce report |
67 | | - app.generateCoverage(); |
| 93 | + // Run Istanbul |
| 94 | + await app.report(); |
68 | 95 |
|
69 | 96 | } catch(e){ |
70 | 97 | error = e; |
71 | 98 | } |
72 | 99 |
|
73 | 100 | // Finish |
74 | | - return app.cleanUp(error); |
| 101 | + await app.cleanUp(); |
| 102 | + |
| 103 | + if (error !== undefined) throw new Error(error) |
| 104 | + if (failures > 0) throw new Error(`${failures} test(s) failed under coverage.`) |
75 | 105 | } |
76 | 106 |
|
77 | 107 | // -------------------------------------- Helpers -------------------------------------------------- |
| 108 | + |
| 109 | +function tests(truffle){ |
| 110 | + const regex = /.*\.(js|ts|es|es6|jsx|sol)$/; |
| 111 | + const files = dir.files(truffle.test_directory, { sync: true }) || []; |
| 112 | + return files.filter(f => f.match(regex) != null); |
| 113 | +} |
| 114 | + |
| 115 | + |
78 | 116 | function loadTruffleLibrary(){ |
79 | 117 |
|
80 | 118 | try { return require("truffle") } catch(err) {}; |
81 | 119 | try { return require("./truffle.library")} catch(err) {}; |
82 | 120 |
|
83 | | - throw new Error(utils.errors.NO_TRUFFLE_LIB) |
| 121 | + throw new Error('Missing truffle lib...') |
84 | 122 | } |
85 | 123 |
|
86 | | -const coveragePaths = { |
87 | | - contracts: (t, c) => path.join(path.dirname(t.contracts_directory), c.contractsDirName)), |
88 | | - |
89 | | - artifacts: { |
90 | | - root: (t, c) => path.join(path.dirname(t.build_directory), c.artifactsDirName), |
91 | | - contracts: (t, c) => { |
92 | | - const root = path.join(path.dirname(t.build_directory), c.artifactsDirName); |
93 | | - return path.join(root, path.basename(t.contracts_build_directory)); |
94 | | - } |
| 124 | +/** |
| 125 | + * Functions to generate substitute paths for instrumented contracts and artifacts. |
| 126 | + * @type {Object} |
| 127 | + */ |
| 128 | +const paths = { |
| 129 | + // "contracts_directory": |
| 130 | + contracts: (app) => { |
| 131 | + return path.join( |
| 132 | + app.coverageDir, |
| 133 | + app.contractsDirName |
| 134 | + ) |
| 135 | + }, |
| 136 | + |
| 137 | + // "build_directory": |
| 138 | + build: (app) => { |
| 139 | + return path.join( |
| 140 | + app.coverageDir, |
| 141 | + app.artifactsDirName |
| 142 | + ) |
| 143 | + }, |
| 144 | + |
| 145 | + // "contracts_build_directory": |
| 146 | + artifacts: (truffle, app) => { |
| 147 | + return path.join( |
| 148 | + app.coverageDir, |
| 149 | + app.artifactsDirName, |
| 150 | + path.basename(truffle.contracts_build_directory) |
| 151 | + ) |
95 | 152 | } |
96 | 153 | } |
97 | 154 |
|
|
0 commit comments