|
| 1 | +import sinon from 'sinon'; |
| 2 | +import assert from 'assert'; |
| 3 | +import { fakeEmbark } from 'embark-testing'; |
| 4 | +import Compiler from '../src/lib/Compiler'; |
| 5 | + |
| 6 | +function MockFile(options) { |
| 7 | + this.path = options.path; |
| 8 | + this.type = options.type; |
| 9 | + this.originalPath = options.originalPath; |
| 10 | + this.importRemappings = []; |
| 11 | +} |
| 12 | + |
| 13 | +const isMac = process.platform === 'darwin'; |
| 14 | + |
| 15 | +describe('plugin/solc', () => { |
| 16 | + |
| 17 | + let embark, originalCompileSolcContract; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + |
| 21 | + const testBed = fakeEmbark({ |
| 22 | + pluginConfig: { |
| 23 | + outputBinary: false |
| 24 | + }, |
| 25 | + embarkConfig: { |
| 26 | + options: { |
| 27 | + solc: {} |
| 28 | + } |
| 29 | + }, |
| 30 | + contractDirectories: [] |
| 31 | + }); |
| 32 | + |
| 33 | + embark = testBed.embark; |
| 34 | + originalCompileSolcContract = Compiler.compileSolcContract; |
| 35 | + Compiler.compileSolcContract = sinon.spy((logger, _settings, _directories, cb) => { |
| 36 | + cb(null, 'compileString'); |
| 37 | + }); |
| 38 | + |
| 39 | + MockFile.prototype.prepareForCompilation = sinon.spy(() => Promise.resolve('')); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + Compiler.compileSolcContract = originalCompileSolcContract; |
| 44 | + embark.teardown(); |
| 45 | + sinon.restore(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should ensure testsuite has at least one test', () => { |
| 49 | + expect(true).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + // TODO(pascal): |
| 53 | + // Remove this condition once there's a `solc` binary provided by the |
| 54 | + // solidity project. We need to turn off tests for Mac on CI because |
| 55 | + // we don't install `solc` on that platform. Reason being is that it |
| 56 | + // takes too long to install via brew. |
| 57 | + if (!isMac) { |
| 58 | + it('should get solc version', () => { |
| 59 | + return new Promise(done => { |
| 60 | + Compiler.getSolcVersion(embark.logger, (err, version) => { |
| 61 | + assert(version); |
| 62 | + done(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should compile solc contract', () => { |
| 68 | + const EMPTY_TEST_CONTRACT = ` |
| 69 | + pragma solidity ^0.6.0; |
| 70 | +
|
| 71 | + contract TestContract { |
| 72 | +
|
| 73 | + } |
| 74 | + `; |
| 75 | + |
| 76 | + MockFile.prototype.prepareForCompilation = sinon.spy(_isCoverage => { |
| 77 | + return Promise.resolve(EMPTY_TEST_CONTRACT); |
| 78 | + }); |
| 79 | + |
| 80 | + let mockFile = new MockFile({ |
| 81 | + type: 'dapp_file', |
| 82 | + path: 'test_file.sol', |
| 83 | + originalPath: 'test_file.sol' |
| 84 | + }); |
| 85 | + |
| 86 | + const contractFiles = [mockFile]; |
| 87 | + |
| 88 | + const contractDirectories = ['contracts']; |
| 89 | + const options = {}; |
| 90 | + |
| 91 | + return new Promise(done => { |
| 92 | + Compiler.compileSolc(embark, contractFiles, contractDirectories, options, (err, result) => { |
| 93 | + assert(mockFile.prepareForCompilation.called); |
| 94 | + |
| 95 | + assert(result.TestContract); |
| 96 | + assert(result.TestContract.code); |
| 97 | + assert(result.TestContract.runtimeBytecode); |
| 98 | + assert(result.TestContract.realRuntimeBytecode); |
| 99 | + assert(result.TestContract.swarmHash); |
| 100 | + done(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should emit error when compilation fails', () => { |
| 106 | + |
| 107 | + const ERROR_CONTRACT = ` |
| 108 | +
|
| 109 | + ontract ErrorContract { |
| 110 | +
|
| 111 | + } |
| 112 | + `; |
| 113 | + |
| 114 | + MockFile.prototype.prepareForCompilation = sinon.spy(_isCoverage => { |
| 115 | + return Promise.resolve(ERROR_CONTRACT); |
| 116 | + }); |
| 117 | + |
| 118 | + let mockFile = new MockFile({ |
| 119 | + type: 'dapp_file', |
| 120 | + path: 'test_file.sol', |
| 121 | + originalPath: 'test_file.sol' |
| 122 | + }); |
| 123 | + |
| 124 | + const contractFiles = [mockFile]; |
| 125 | + |
| 126 | + const contractDirectories = ['contracts']; |
| 127 | + const options = {}; |
| 128 | + |
| 129 | + return new Promise(done => { |
| 130 | + Compiler.compileSolc(embark, contractFiles, contractDirectories, options, (err, _result) => { |
| 131 | + assert(err); |
| 132 | + done(); |
| 133 | + }); |
| 134 | + }); |
| 135 | + }); |
| 136 | + } |
| 137 | +}); |
0 commit comments