Skip to content

Commit 533a2e3

Browse files
committed
test(plugins/solc): add tests for plugins/solc
1 parent d578e54 commit 533a2e3

File tree

8 files changed

+221
-15
lines changed

8 files changed

+221
-15
lines changed

azure-pipelines.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ steps:
5757
architecture: x64
5858
versionSpec: $(pythonVersion)
5959

60+
- script: |
61+
mkdir -p ../Downloads
62+
cd ../Downloads
63+
curl -L -o "solc" "https://github.com/ethereum/solidity/releases/download/v0.6.4/solc-static-linux"
64+
chmod +x solc
65+
displayName: Linux install Solc
66+
condition: eq( variables['Agent.OS'], 'Linux' )
67+
68+
# NOTICE:
69+
# Solc installation disabled for now until solc provides binaries for download.
70+
# Installation on Mac via brew takes too long.
71+
#
72+
#- script: |
73+
# brew update
74+
# brew upgrade
75+
# brew tap ethereum/ethereum
76+
# brew install solidity
77+
# displayName: Mac install Solc
78+
# condition: eq( variables['Agent.OS'], 'Darwin' )
79+
80+
- powershell: |
81+
mkdir -p ../Downloads
82+
$client = new-object System.Net.WebClient
83+
$client.DownloadFile("https://github.com/ethereum/solidity/releases/download/v0.6.4/solidity-windows.zip", "..\Downloads\solidity-windows.zip")
84+
Expand-Archive –Path “..\Downloads\solidity-windows.zip” –Destination “..\Downloads\solidity-windows”
85+
displayName: Windows install Solc
86+
condition: eq( variables['Agent.OS'], 'Windows_NT' )
87+
6088
- bash: |
6189
mkdir -p ../Downloads
6290
cd ../Downloads
@@ -109,6 +137,7 @@ steps:
109137
pushd "$PWD" &>/dev/null
110138
cd ../Downloads
111139
export PATH="${PWD}/go-ipfs:${PATH}"
140+
export PATH="${PWD}:${PATH}"
112141
export PATH="${HOME}/.yarn/bin:${HOME}/.config/yarn/global/node_modules/.bin:${PATH}"
113142
yarn config set cache-folder "${YARN_CACHE_FOLDER}" &>/dev/null
114143
popd &>/dev/null
@@ -120,6 +149,7 @@ steps:
120149
pushd
121150
cd ..\Downloads
122151
$env:PATH="$($PWD.Path)\go-ipfs;$($env:PATH)"
152+
$env:PATH="$($PWD.Path)\solidity-windows;$($env:PATH)"
123153
$env:PATH="$($env:USERPROFILE)\.yarn\bin;$($env:USERPROFILE)\.config\yarn\global\node_modules\.bin;$($env:PATH)"
124154
yarn config set cache-folder "$($YARN_CACHE_FOLDER)" | out-null
125155
popd

packages/core/utils/src/file.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export class File {
2323
public basedir: string;
2424
public resolver: (callback: (content: string) => void) => void;
2525
public pluginPath: string;
26-
public storageConfig: any;
2726
public providerUrl: string;
2827
public importRemappings: ImportRemapping[] = [];
2928
public originalPath: string;
@@ -34,7 +33,6 @@ export class File {
3433
this.basedir = options.basedir || '';
3534
this.resolver = options.resolver;
3635
this.pluginPath = options.pluginPath ? options.pluginPath : '';
37-
this.storageConfig = options.storageConfig;
3836
this.providerUrl = '';
3937
this.originalPath = options.originalPath || '';
4038

packages/embark/src/test/config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ describe('embark.Config', function () {
288288
"basedir": "",
289289
"importRemappings": [],
290290
"resolver": undefined,
291-
"storageConfig": undefined,
292291
"providerUrl": ""
293292
},
294293
{
@@ -300,7 +299,6 @@ describe('embark.Config', function () {
300299
"basedir": "",
301300
"importRemappings": [],
302301
"resolver": undefined,
303-
"storageConfig": undefined,
304302
"providerUrl": ""
305303
},
306304
{
@@ -312,7 +310,6 @@ describe('embark.Config', function () {
312310
"basedir": "",
313311
"importRemappings": [],
314312
"resolver": undefined,
315-
"storageConfig": undefined,
316313
"providerUrl": ""
317314
}
318315
];

packages/plugins/solc/package.json

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,58 @@
3636
"_typecheck": "npm run solo -- typecheck",
3737
"ci": "npm run qa",
3838
"clean": "npm run reset",
39-
"lint": "eslint src/",
39+
"lint": "eslint src/ test/",
4040
"qa": "npm-run-all lint _typecheck _build",
4141
"reset": "npx rimraf dist embark-*.tgz package",
42-
"solo": "embark-solo"
42+
"solo": "embark-solo",
43+
"test": "jest"
4344
},
4445
"eslintConfig": {
45-
"extends": "../../../.eslintrc.json"
46+
"extends": [
47+
"../../../.eslintrc.json",
48+
"plugin:jest/recommended",
49+
"plugin:jest/style"
50+
]
4651
},
4752
"dependencies": {
4853
"@babel/runtime-corejs3": "7.8.4",
4954
"async": "3.2.0",
50-
"core-js": "3.4.3",
51-
"semver": "5.6.0",
55+
"core-js": "3.6.4",
56+
"global": "4.4.0",
57+
"semver": "7.1.3",
5258
"shelljs": "0.8.3"
5359
},
5460
"devDependencies": {
61+
"@babel/core": "7.8.6",
62+
"babel-jest": "25.1.0",
5563
"embark-solo": "^5.2.3",
64+
"embark-testing": "^5.3.0-nightly.14",
65+
"embark-utils": "^5.3.0-nightly.14",
5666
"eslint": "6.8.0",
67+
"eslint-plugin-jest": "22.5.1",
68+
"jest": "25.1.0",
5769
"npm-run-all": "4.1.5",
58-
"rimraf": "3.0.0"
70+
"rimraf": "3.0.0",
71+
"sinon": "7.4.2"
5972
},
6073
"engines": {
6174
"node": ">=10.17.0",
6275
"npm": ">=6.11.3",
6376
"yarn": ">=1.19.1"
77+
},
78+
"jest": {
79+
"collectCoverage": true,
80+
"testEnvironment": "node",
81+
"testMatch": [
82+
"**/test/**/*.js"
83+
],
84+
"transform": {
85+
"\\.js$": [
86+
"babel-jest",
87+
{
88+
"rootMode": "upward"
89+
}
90+
]
91+
}
6492
}
6593
}

packages/plugins/solc/src/lib/Compiler.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ const shelljs = require('shelljs');
33
const fs = require('fs');
44
const path = require('path');
55

6+
const isWin = process.platform === "win32";
7+
68
function compileSolcContract(logger, compileSettings, allowedDirectories, callback) {
7-
const command = `solc --standard-json --allow-paths ${allowedDirectories.join(',')}`;
9+
const binary = isWin ? 'solc.exe' : 'solc';
10+
const command = `${binary} --standard-json --allow-paths ${allowedDirectories.join(',')}`;
811

912
shelljs.ShellString(JSON.stringify(compileSettings)).exec(command, {silent: true}, (code, stdout, stderr) => {
1013
if (stderr) {
@@ -48,7 +51,7 @@ function compileSolc(embark, contractFiles, contractDirectories, options, callba
4851
}
4952

5053
const logger = embark.logger;
51-
const outputBinary = embark.pluginConfig.outputBinary;
54+
const outputBinary = embark.config.pluginConfig.outputBinary;
5255
const outputDir = embark.config.buildDir + embark.config.contractDirectories[0];
5356
const solcConfig = embark.config.embarkConfig.options.solc;
5457

@@ -188,12 +191,12 @@ function compileSolc(embark, contractFiles, contractDirectories, options, callba
188191
compiledObject[className].filename = filename;
189192
const normalized = path.normalize(filename);
190193
const origContract = contractFiles.find(contractFile => normalized.includes(path.normalize(contractFile.originalPath)));
194+
191195
if (origContract) {
192196
compiledObject[className].originalFilename = path.normalize(origContract.originalPath);
193197
}
194198
}
195199
}
196-
197200
next(null, compiledObject);
198201
}
199202

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
});

packages/plugins/solc/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,13 @@
88
"extends": "../../../tsconfig.base.json",
99
"include": [
1010
"src/**/*"
11+
],
12+
"references": [
13+
{
14+
"path": "../../core/utils"
15+
},
16+
{
17+
"path": "../../utils/testing"
18+
}
1119
]
1220
}

yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10936,7 +10936,7 @@ global-prefix@^3.0.0:
1093610936
kind-of "^6.0.2"
1093710937
which "^1.3.1"
1093810938

10939-
global@^4.3.2, global@^4.4.0:
10939+
global@4.4.0, global@^4.3.2, global@^4.4.0:
1094010940
version "4.4.0"
1094110941
resolved "https://registry.yarnpkg.com/global/-/global-4.4.0.tgz#3e7b105179006a323ed71aafca3e9c57a5cc6406"
1094210942
integrity sha512-wv/LAoHdRE3BeTGz53FAamhGlPLhlssK45usmGFThIi4XqnBmjKQ16u+RNbP7WvigRZDxUsM0J3gcQ5yicaL0w==
@@ -20105,6 +20105,11 @@ [email protected]:
2010520105
resolved "https://registry.yarnpkg.com/semver/-/semver-7.0.0.tgz#5f3ca35761e47e05b206c6daff2cf814f0316b8e"
2010620106
integrity sha512-+GB6zVA9LWh6zovYQLALHwv5rb2PHGlJi3lfiqIHxR0uuwCgefcOJc59v9fv1w8GbStwxuuqqAjI9NMAOOgq1A==
2010720107

20108+
20109+
version "7.1.3"
20110+
resolved "https://registry.yarnpkg.com/semver/-/semver-7.1.3.tgz#e4345ce73071c53f336445cfc19efb1c311df2a6"
20111+
integrity sha512-ekM0zfiA9SCBlsKa2X1hxyxiI4L3B6EbVJkkdgQXnSEEaHlGdvyodMruTiulSRWMMB4NeIuYNMC9rTKTz97GxA==
20112+
2010820113
semver@^7.1.1:
2010920114
version "7.1.1"
2011020115
resolved "https://registry.yarnpkg.com/semver/-/semver-7.1.1.tgz#29104598a197d6cbe4733eeecbe968f7b43a9667"

0 commit comments

Comments
 (0)