forked from NomicFoundation/hardhat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.ts
320 lines (260 loc) · 11.1 KB
/
tests.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { assert, expect, use } from "chai";
import chaiAsPromised from "chai-as-promised";
import * as fsExtra from "fs-extra";
import path from "path";
import { TASK_COMPILE } from "hardhat/builtin-tasks/task-names";
import fs from "node:fs";
import { VYPER_FILES_CACHE_FILENAME } from "../src/constants";
import {
useEnvironment,
useFixtureProject,
assertFileExists,
expectVyperErrorAsync,
} from "./helpers";
use(chaiAsPromised);
describe("Vyper plugin", function () {
beforeEach(function () {
fsExtra.removeSync("artifacts");
fsExtra.removeSync(path.join("cache", VYPER_FILES_CACHE_FILENAME));
});
describe("project with single file", function () {
useFixtureProject("compilation-single-file");
useEnvironment();
it("should compile and emit artifacts", async function () {
await this.env.run(TASK_COMPILE);
assertFileExists(path.join("artifacts", "contracts", "A.vy", "A.json"));
});
});
describe("project with two files with different compiler versions", function () {
useFixtureProject("compilation-two-files-different-versions");
useEnvironment();
it("should compile and emit artifacts", async function () {
await this.env.run(TASK_COMPILE);
assertFileExists(path.join("artifacts", "contracts", "A.vy", "A.json"));
assertFileExists(path.join("artifacts", "contracts", "B.vy", "B.json"));
});
});
describe("vyper settings", function () {
describe("compilation with different settings", function () {
useFixtureProject("compilation-with-vyper-settings");
useEnvironment();
it("should compile and emit artifacts", async function () {
await this.env.run(TASK_COMPILE);
assertFileExists(path.join("artifacts", "contracts", "A.vy", "A.json"));
assertFileExists(path.join("artifacts", "contracts", "B.vy", "B.json"));
});
});
describe("optimize, as boolean type, can always be set to false in versions >= 0.3.10 (flag --optimize none)", function () {
useFixtureProject(
"compilation-with-settings-option-variants/optimize-set-to-false-always-available-new-versions"
);
useEnvironment();
it("should compile successfully", async function () {
await this.env.run(TASK_COMPILE);
assertFileExists(path.join("artifacts", "contracts", "A.vy", "A.json"));
});
});
describe("optimize, as boolean type, can always be set to false in versions 0.3.0 < v < 0.3.10 (flag --no-optimize)", function () {
useFixtureProject(
"compilation-with-settings-option-variants/optimize-set-to-false-always-available-old-versions-after-0.3.0"
);
useEnvironment();
it("should compile successfully", async function () {
await this.env.run(TASK_COMPILE);
assertFileExists(path.join("artifacts", "contracts", "A.vy", "A.json"));
});
});
describe("optimize, as boolean type, cannot be set to false in versions <= 0.3.0", function () {
useFixtureProject(
"compilation-with-settings-option-variants/optimize-set-to-false-not-available-old-versions"
);
useEnvironment();
it("should fail the compilation", async function () {
await expect(this.env.run(TASK_COMPILE)).to.be.rejectedWith(
Error,
"The 'optimize' setting with value 'false' is not supported for versions of the Vyper compiler older than or equal to 0.3.0. You are currently using version 0.3.0."
);
});
});
describe("optimize setting set to true in supported versions", function () {
useFixtureProject(
"compilation-with-settings-option-variants/optimize-set-to-true"
);
useEnvironment();
it("should compile successfully", async function () {
await this.env.run(TASK_COMPILE);
assertFileExists(path.join("artifacts", "contracts", "A.vy", "A.json"));
});
});
describe("optimize set to true is not available for versions >= 0.3.10", function () {
useFixtureProject(
"compilation-with-settings-option-variants/optimize-true-not-available-new-versions"
);
useEnvironment();
it("should fail the compilation", async function () {
await expect(this.env.run(TASK_COMPILE)).to.be.rejectedWith(
Error,
"The 'optimize' setting with value 'true' is not supported for versions of the Vyper compiler older than or equal to 0.3.0 or newer than or equal to 0.3.10. You are currently using version 0.3.10."
);
});
});
describe("optimize set to true is not available for versions <= 0.3.0", function () {
useFixtureProject(
"compilation-with-settings-option-variants/optimize-true-not-available-old-versions"
);
useEnvironment();
it("should fail the compilation", async function () {
await expect(this.env.run(TASK_COMPILE)).to.be.rejectedWith(
Error,
"The 'optimize' setting with value 'true' is not supported for versions of the Vyper compiler older than or equal to 0.3.0 or newer than or equal to 0.3.10. You are currently using version 0.3.0."
);
});
});
describe("optimize setting cannot be a string for version < 0.3.10", function () {
useFixtureProject(
"compilation-with-settings-option-variants/optimize-string-not-available-old-versions"
);
useEnvironment();
it("should fail the compilation", async function () {
await expect(this.env.run(TASK_COMPILE)).to.be.rejectedWith(
Error,
"The 'optimize' setting, when specified as a string value, is available only starting from the Vyper compiler version 0.3.10. You are currently using version 0.3.9."
);
});
});
describe("optimize setting must be a string or boolean type", function () {
useFixtureProject(
"compilation-with-settings-option-variants/optimize-invalid-type"
);
useEnvironment();
it("should fail the compilation", async function () {
await expect(this.env.run(TASK_COMPILE)).to.be.rejectedWith(
Error,
"The 'optimize' setting has an invalid type value: number. Type should be either string or boolean."
);
});
});
});
describe("caching mechanism", function () {
describe("caching mechanism without vyper settings", function () {
useFixtureProject("compilation-single-file");
useEnvironment();
it("should not re-compile the contract because of the cache", async function () {
await this.env.run(TASK_COMPILE);
const stats1 = fs.statSync(
path.join("artifacts", "contracts", "A.vy", "A.json")
);
// it should not compile again so the contract should not be modified
await this.env.run(TASK_COMPILE);
const stats2 = fs.statSync(
path.join("artifacts", "contracts", "A.vy", "A.json")
);
assert.equal(stats1.mtimeMs, stats2.mtimeMs);
});
});
describe("caching mechanism with vyper settings", function () {
useFixtureProject("compilation-with-vyper-settings");
useEnvironment();
it("should not re-compile the contract because of the cache", async function () {
await this.env.run(TASK_COMPILE);
const stats1A = fs.statSync(
path.join("artifacts", "contracts", "A.vy", "A.json")
);
const stats1B = fs.statSync(
path.join("artifacts", "contracts", "B.vy", "B.json")
);
// it should not compile again so the contracts should not be modified
await this.env.run(TASK_COMPILE);
const stats2A = fs.statSync(
path.join("artifacts", "contracts", "A.vy", "A.json")
);
const stats2B = fs.statSync(
path.join("artifacts", "contracts", "B.vy", "B.json")
);
assert.equal(stats1A.mtimeMs, stats2A.mtimeMs);
assert.equal(stats1B.mtimeMs, stats2B.mtimeMs);
});
});
});
describe("old versions of vyper", function () {
useFixtureProject("old-vyper-versions");
describe("project with an old version of vyper", function () {
useEnvironment("old-vyper-version.js");
it("should throw an error", async function () {
await expectVyperErrorAsync(async () => {
await this.env.run(TASK_COMPILE);
}, "Unsupported vyper version: 0.1.0-beta.15");
});
});
describe("project with an old version of vyper (multiple compilers)", function () {
useEnvironment("old-vyper-version-multiple-compilers.js");
it("should throw an error", async function () {
await expectVyperErrorAsync(async () => {
await this.env.run(TASK_COMPILE);
}, "Unsupported vyper version: 0.1.0-beta.15");
});
});
});
describe("Mixed language", function () {
useFixtureProject("mixed-language");
useEnvironment();
it("Should successfully compile the contracts", async function () {
await this.env.run(TASK_COMPILE);
assert.equal(
this.env.artifacts.readArtifactSync("test").contractName,
"test"
);
assert.equal(
this.env.artifacts.readArtifactSync("Greeter").contractName,
"Greeter"
);
});
});
describe("project with file that cannot be compiled", function () {
useFixtureProject("unmatched-compiler-version");
useEnvironment();
it("should throw an error", async function () {
await expectVyperErrorAsync(async () => {
await this.env.run(TASK_COMPILE);
}, "The Vyper version pragma statement in this file doesn't match any of the configured compilers in your config.");
});
});
describe("project produces abi without gas field", function () {
useFixtureProject("generates-gas-field");
useEnvironment();
it("Should remove the gas field", async function () {
await this.env.run(TASK_COMPILE);
assert.isUndefined(
JSON.parse(JSON.stringify(this.env.artifacts.readArtifactSync("A").abi))
.gas
);
});
});
describe("project should not compile", function () {
useFixtureProject("compilation-single-file-test-directive");
useEnvironment();
it("should throw an error because a test directive is present in the source file", async function () {
const filePath = path.join(
__dirname,
"fixture-projects",
"compilation-single-file-test-directive",
"contracts",
"A.vy"
);
await expect(this.env.run(TASK_COMPILE)).to.be.rejectedWith(
`We found a test directive in the file at path ${filePath}.` +
` Test directives are a Brownie feature not supported by Hardhat.` +
` Learn more at https://hardhat.org/hardhat-runner/plugins/nomiclabs-hardhat-vyper#test-directives`
);
});
});
describe("compile project with different ouput identifiers returned from the vyper compiler", function () {
useFixtureProject("compilation-with-vyper-output-breakable-version");
useEnvironment();
it("Should successfully compile the contracts for versions >= 0.4.0", async function () {
await this.env.run(TASK_COMPILE);
assert.equal(this.env.artifacts.readArtifactSync("A").contractName, "A");
assert.equal(this.env.artifacts.readArtifactSync("B").contractName, "B");
});
});
});