-
Notifications
You must be signed in to change notification settings - Fork 486
/
Copy pathcli.ts
304 lines (277 loc) · 9.26 KB
/
cli.ts
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
import tape from 'tape';
import spawn from 'tape-spawn';
import rimraf from 'rimraf';
import tmp from 'tmp';
import fs from 'fs';
import * as path from 'path';
import solc from '../';
const dist = path.resolve(__dirname, '..');
const solcjs = path.join(dist, 'solc.js');
function cleanupArtifacts () {
rimraf.sync(`${dist}/*{.bin,.abi}`);
}
tape('CLI', function (t) {
t.test('--version', function (st) {
const spt = spawn(st, `node ${solcjs} --version`);
spt.stdout.match(solc.version() + '\n');
spt.stdout.match(/^\s*[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?\+commit\.[0-9a-f]+([a-zA-Z0-9.-]+)?\s*$/);
spt.stderr.empty();
spt.end();
});
t.test('no parameters', function (st) {
const spt = spawn(st, `node ${solcjs}`);
spt.stderr.match(/^Must provide a file/);
spt.end();
});
t.test('no mode specified', function (st) {
const spt = spawn(st, `node ${solcjs} test/resources/fixtureSmoke.sol`);
spt.stderr.match(/^Invalid option selected/);
spt.end();
});
t.test('--bin', function (st) {
const spt = spawn(st, `node ${solcjs} --bin test/resources/fixtureSmoke.sol`);
spt.stderr.empty();
spt.succeeds();
spt.end();
st.teardown(cleanupArtifacts);
});
t.test('--bin --optimize', function (st) {
const spt = spawn(st, `node ${solcjs} --bin --optimize test/resources/fixtureSmoke.sol`);
spt.stderr.empty();
spt.succeeds();
spt.end();
st.teardown(cleanupArtifacts);
});
t.test('--bin --optimize-runs 666', function (st) {
const spt = spawn(st, `node ${solcjs} --bin --optimize-runs 666 test/resources/fixtureSmoke.sol`);
spt.stderr.empty();
spt.succeeds();
spt.end();
st.teardown(cleanupArtifacts);
});
t.test('--bin --optimize-runs not-a-number', function (st) {
const spt = spawn(st, `node ${solcjs} --bin --optimize-runs not-a-number test/resources/fixtureSmoke.sol`);
spt.stderr.match(/^error: option '--optimize-runs <optimize-runs>' argument 'not-a-number' is invalid/);
spt.end();
});
t.test('invalid file specified', function (st) {
const spt = spawn(st, `node ${solcjs} --bin test/fileNotFound.sol`);
spt.stderr.match(/^Error reading /);
spt.end();
});
t.test('incorrect source source', function (st) {
const spt = spawn(st, `node ${solcjs} --bin test/resources/fixtureIncorrectSource.sol`);
spt.stderr.match(/SyntaxError: Invalid pragma "contract"/);
spt.end();
});
t.test('--abi', function (st) {
const spt = spawn(st, `node ${solcjs} --abi test/resources/fixtureSmoke.sol`);
spt.stderr.empty();
spt.succeeds();
spt.end();
st.teardown(cleanupArtifacts);
});
t.test('--bin --abi', function (st) {
const spt = spawn(st, `node ${solcjs} --bin --abi test/resources/fixtureSmoke.sol`);
spt.stderr.empty();
spt.succeeds();
spt.end();
st.teardown(cleanupArtifacts);
});
t.test('no base path', function (st) {
const spt = spawn(
st,
`node ${solcjs} --bin \
test/resources/importA.sol \
./test/resources//importA.sol \
${path.resolve('test/resources/importA.sol')}`
);
spt.stderr.empty();
spt.succeeds();
spt.end();
st.teardown(cleanupArtifacts);
});
t.test('relative base path', function (st) {
// NOTE: This and other base path tests rely on the relative ./importB.sol import in importA.sol.
// If base path is not stripped correctly from all source paths below, they will not be found
// by the import callback when it appends the base path back.
const spt = spawn(
st,
`node ${solcjs} --bin --base-path test/resources \
test/resources/importA.sol \
./test/resources//importA.sol \
${path.resolve('test/resources/importA.sol')}`
);
spt.stderr.empty();
spt.succeeds();
spt.end();
st.teardown(cleanupArtifacts);
});
t.test('relative non canonical base path', function (st) {
const spt = spawn(
st,
`node ${solcjs} --bin --base-path ./test/resources \
test/resources/importA.sol \
./test/resources//importA.sol \
${path.resolve('test/resources/importA.sol')}`
);
spt.stderr.empty();
spt.succeeds();
spt.end();
st.teardown(cleanupArtifacts);
});
t.test('absolute base path', function (st) {
const spt = spawn(
st,
`node ${solcjs} --bin --base-path ${path.resolve('test/resources')} \
test/resources/importA.sol \
./test/resources//importA.sol \
${path.resolve('test/resources/importA.sol')}`
);
spt.stderr.empty();
spt.succeeds();
spt.end();
st.teardown(cleanupArtifacts);
});
t.test('include paths', function (st) {
const spt = spawn(
st,
`node ${solcjs} --bin \
test/resources/importCallback/base/contractB.sol \
test/resources/importCallback/includeA/libY.sol \
./test/resources/importCallback/includeA//libY.sol \
${path.resolve('test/resources/importCallback/includeA/libY.sol')} \
--base-path test/resources/importCallback/base \
--include-path test/resources/importCallback/includeA \
--include-path ${path.resolve('test/resources/importCallback/includeB/')}`
);
spt.stderr.empty();
spt.succeeds();
spt.end();
st.teardown(cleanupArtifacts);
});
t.test('include paths without base path', function (st) {
const spt = spawn(
st,
`node ${solcjs} --bin \
test/resources/importCallback/contractC.sol \
--include-path test/resources/importCallback/includeA`
);
spt.stderr.match(/--include-path option requires a non-empty base path\./);
spt.fails();
spt.end();
});
t.test('empty include paths', function (st) {
const spt = spawn(
st,
`node ${solcjs} --bin \
test/resources/importCallback/contractC.sol \
--base-path test/resources/importCallback/base \
--include-path=`
);
spt.stderr.match(/Empty values are not allowed in --include-path\./);
spt.fails();
spt.end();
});
t.test('standard json', function (st) {
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode', 'userdoc']
}
}
},
sources: {
'Contract.sol': {
content: 'pragma solidity >=0.5.0; contract Contract { function f() pure public {} }'
}
}
};
const spt = spawn(st, `node ${solcjs} --standard-json`);
spt.stdin.setEncoding('utf-8');
spt.stdin.write(JSON.stringify(input));
spt.stdin.end();
spt.stdin.on('finish', function () {
spt.stderr.empty();
spt.stdout.match(/Contract.sol/);
spt.stdout.match(/userdoc/);
spt.succeeds();
spt.end();
});
});
t.test('standard json base path', function (st) {
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['metadata']
}
}
},
sources: {
'importA.sol': {
content: 'import "./importB.sol";'
}
}
};
const spt = spawn(st, `node ${solcjs} --standard-json --base-path test/resources`);
spt.stdin.setEncoding('utf-8');
spt.stdin.write(JSON.stringify(input));
spt.stdin.end();
spt.stdin.on('finish', function () {
spt.stderr.empty();
spt.stdout.match(/{"contracts":{"importB.sol":{"D":{"metadata":/);
spt.succeeds();
spt.end();
});
});
t.test('standard json include paths', function (st) {
const input = {
language: 'Solidity',
sources: {
'contractB.sol': {
content:
'// SPDX-License-Identifier: GPL-3.0\n' +
'pragma solidity >=0.0;\n' +
'import "./contractA.sol";\n'
}
}
};
const spt = spawn(
st,
`node ${solcjs} --standard-json \
--base-path test/resources/importCallback/base \
--include-path test/resources/importCallback/includeA \
--include-path ${path.resolve('test/resources/importCallback/includeB/')}`
);
spt.stdin.setEncoding('utf-8');
spt.stdin.write(JSON.stringify(input));
spt.stdin.end();
spt.stdin.on('finish', function () {
spt.stderr.empty();
spt.stdout.match(/"sources":{"contractA.sol":{"id":0},"contractB.sol":{"id":1},"libX.sol":{"id":2},"libY.sol":{"id":3},"libZ.sol":{"id":4},"utils.sol":{"id":5}}}/);
spt.succeeds();
spt.end();
});
});
t.test('attempt to overwrite without --overwrite flag', function (st) {
const cwd = tmp.dirSync({ unsafeCleanup: true }).name;
// create a fake C.bin to cause name collision
fs.openSync(`${cwd}/C.bin`, 'w');
const spt = spawn(st, `node ${solcjs} --bin ${dist}/test/resources/fixtureSmoke.sol`, { cwd });
spt.stderr.match(/^Refusing to overwrite existing file C\.bin \(use --overwrite to force\)\./);
spt.end();
});
t.test('--overwrite', function (st) {
const cwd = tmp.dirSync({ unsafeCleanup: true }).name;
// create a fake C.bin to cause name collision
fs.openSync(`${cwd}/C.bin`, 'w');
const spt = spawn(st, `node ${solcjs} --bin ${dist}/test/resources/fixtureSmoke.sol --overwrite`, { cwd });
spt.stderr.empty();
spt.succeeds();
spt.end();
});
});