UNPKG

11.3 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __generator = (this && this.__generator) || function (thisArg, body) {
12 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14 function verb(n) { return function (v) { return step([n, v]); }; }
15 function step(op) {
16 if (f) throw new TypeError("Generator is already executing.");
17 while (_) try {
18 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19 if (y = 0, t) op = [op[0] & 2, t.value];
20 switch (op[0]) {
21 case 0: case 1: t = op; break;
22 case 4: _.label++; return { value: op[1], done: false };
23 case 5: _.label++; y = op[1]; op = [0]; continue;
24 case 7: op = _.ops.pop(); _.trys.pop(); continue;
25 default:
26 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30 if (t[2]) _.ops.pop();
31 _.trys.pop(); continue;
32 }
33 op = body.call(thisArg, _);
34 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36 }
37};
38var __importDefault = (this && this.__importDefault) || function (mod) {
39 return (mod && mod.__esModule) ? mod : { "default": mod };
40};
41Object.defineProperty(exports, "__esModule", { value: true });
42var async_1 = __importDefault(require("async"));
43require('colors');
44var compiler_1 = require("./compiler");
45var deployer_1 = require("./deployer");
46var testRunner_1 = require("./testRunner");
47var web3_1 = __importDefault(require("web3"));
48var remix_simulator_1 = require("remix-simulator");
49var createWeb3Provider = function () {
50 return __awaiter(this, void 0, void 0, function () {
51 var web3, provider;
52 return __generator(this, function (_a) {
53 switch (_a.label) {
54 case 0:
55 web3 = new web3_1.default();
56 provider = new remix_simulator_1.Provider();
57 return [4 /*yield*/, provider.init()];
58 case 1:
59 _a.sent();
60 web3.setProvider(provider);
61 return [2 /*return*/, web3];
62 }
63 });
64 });
65};
66/**
67 * @dev Run tests from source of a test contract file (used for IDE)
68 * @param contractSources Sources of contract
69 * @param compilerConfig current compiler configuration
70 * @param testCallback Test callback
71 * @param resultCallback Result Callback
72 * @param finalCallback Final Callback
73 * @param importFileCb Import file callback
74 * @param opts Options
75 */
76function runTestSources(contractSources, compilerConfig, testCallback, resultCallback, finalCallback, importFileCb, opts) {
77 return __awaiter(this, void 0, void 0, function () {
78 var sourceASTs, web3, _a, accounts;
79 return __generator(this, function (_b) {
80 switch (_b.label) {
81 case 0:
82 opts = opts || {};
83 sourceASTs = {};
84 _a = opts.web3;
85 if (_a) return [3 /*break*/, 2];
86 return [4 /*yield*/, createWeb3Provider()];
87 case 1:
88 _a = (_b.sent());
89 _b.label = 2;
90 case 2:
91 web3 = _a;
92 accounts = opts.accounts || null;
93 async_1.default.waterfall([
94 function getAccountList(next) {
95 if (accounts)
96 return next();
97 web3.eth.getAccounts(function (_err, _accounts) {
98 accounts = _accounts;
99 next();
100 });
101 },
102 function compile(next) {
103 compiler_1.compileContractSources(contractSources, compilerConfig, importFileCb, { accounts: accounts }, next);
104 },
105 function deployAllContracts(compilationResult, asts, next) {
106 for (var filename in asts) {
107 if (filename.endsWith('_test.sol'))
108 sourceASTs[filename] = asts[filename].ast;
109 }
110 deployer_1.deployAll(compilationResult, web3, false, function (err, contracts) {
111 if (err) {
112 // If contract deployment fails because of 'Out of Gas' error, try again with double gas
113 // This is temporary, should be removed when remix-tests will have a dedicated UI to
114 // accept deployment params from UI
115 if (err.message.includes('The contract code couldn\'t be stored, please check your gas limit')) {
116 deployer_1.deployAll(compilationResult, web3, true, function (error, contracts) {
117 if (error)
118 next([{ message: 'contract deployment failed after trying twice: ' + error.message, severity: 'error' }]); // IDE expects errors in array
119 else
120 next(null, compilationResult, contracts);
121 });
122 }
123 else
124 next([{ message: 'contract deployment failed: ' + err.message, severity: 'error' }]); // IDE expects errors in array
125 }
126 else
127 next(null, compilationResult, contracts);
128 });
129 },
130 function determineTestContractsToRun(compilationResult, contracts, next) {
131 var contractsToTest = [];
132 var contractsToTestDetails = [];
133 var _loop_1 = function (filename) {
134 if (!filename.endsWith('_test.sol')) {
135 return "continue";
136 }
137 Object.keys(compilationResult[filename]).forEach(function (contractName) {
138 contractsToTestDetails.push(compilationResult[filename][contractName]);
139 contractsToTest.push(contractName);
140 });
141 };
142 for (var filename in compilationResult) {
143 _loop_1(filename);
144 }
145 next(null, contractsToTest, contractsToTestDetails, contracts);
146 },
147 function runTests(contractsToTest, contractsToTestDetails, contracts, next) {
148 var totalPassing = 0;
149 var totalFailing = 0;
150 var totalTime = 0;
151 var errors = [];
152 var _testCallback = function (err, result) {
153 if (result.type === 'testFailure') {
154 errors.push(result);
155 }
156 testCallback(result);
157 };
158 var _resultsCallback = function (_err, result, cb) {
159 resultCallback(_err, result, function () { });
160 totalPassing += result.passingNum;
161 totalFailing += result.failureNum;
162 totalTime += result.timePassed;
163 cb();
164 };
165 async_1.default.eachOfLimit(contractsToTest, 1, function (contractName, index, cb) {
166 var fileAST = sourceASTs[contracts[contractName]['filename']];
167 testRunner_1.runTest(contractName, contracts[contractName], contractsToTestDetails[index], fileAST, { accounts: accounts }, _testCallback, function (err, result) {
168 if (err) {
169 return cb(err);
170 }
171 _resultsCallback(null, result, cb);
172 });
173 }, function (err) {
174 if (err) {
175 return next(err);
176 }
177 var finalResults = {
178 totalPassing: 0,
179 totalFailing: 0,
180 totalTime: 0,
181 errors: [],
182 };
183 finalResults.totalPassing = totalPassing || 0;
184 finalResults.totalFailing = totalFailing || 0;
185 finalResults.totalTime = totalTime || 0;
186 finalResults.errors = [];
187 errors.forEach(function (error, _index) {
188 finalResults.errors.push({ context: error.context, value: error.value, message: error.errMsg });
189 });
190 next(null, finalResults);
191 });
192 }
193 ], finalCallback);
194 return [2 /*return*/];
195 }
196 });
197 });
198}
199exports.runTestSources = runTestSources;
200//# sourceMappingURL=runTestSources.js.map
\No newline at end of file