Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 9da31b8

Browse files
committed
fix some types and tests, and add a check for the browser environment
1 parent cdd6c2e commit 9da31b8

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

packages/compile-solidity-tests/test/compilerSupplier/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ let fakeReturn = {
6464
describe("CompilerSupplier", () => {
6565
beforeEach(function () {
6666
supplierOptions = {
67-
events: config.events
67+
events: config.events,
68+
cache: "fileSystem"
6869
};
6970
});
7071

packages/compile-solidity-tests/test/compilerSupplier/loadingStrategies/VersionRange.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import Config from "@truffle/config";
88
const config = Config.default();
99
let versionRangeOptions = {
1010
events: config.events,
11-
solcConfig: config.compilers.solc
11+
solcConfig: config.compilers.solc,
12+
cache: "fileSystem"
1213
};
1314
const instance = new LoadingStrategies.VersionRange(versionRangeOptions);
14-
let fileName, expectedResult;
15+
let fileName: string, expectedResult: any;
1516
const compilerFileNames = [
1617
"soljson-v0.4.22+commit.124ca40d.js",
1718
"soljson-v0.4.23+commit.1534a40d.js",
@@ -105,32 +106,32 @@ describe("VersionRange loading strategy", () => {
105106

106107
describe("when a version constraint is specified", () => {
107108
beforeEach(() => {
108-
sinon.stub(instance, "getAndCacheSolcByUrl");
109-
sinon.stub(instance.cache, "has").returns(false);
109+
sinon.stub(instance, "getAndCacheSoljsonByUrl");
110+
sinon.stub(instance.cache!, "has").returns(false);
110111
});
111112
afterEach(() => {
112-
unStub(instance, "getAndCacheSolcByUrl");
113-
unStub(instance.cache, "has");
113+
unStub(instance, "getAndCacheSoljsonByUrl");
114+
unStub(instance.cache!, "has");
114115
});
115116

116117
it("calls findNewstValidVersion to determine which version to fetch", async () => {
117118
await instance.getSolcFromCacheOrUrl("^0.5.0");
118119
assert.isTrue(
119120
// @ts-ignore
120-
instance.getAndCacheSolcByUrl.calledWith(
121+
instance.getAndCacheSoljsonByUrl.calledWith(
121122
"soljson-v0.5.4+commit.9549d8ff.js"
122123
),
123-
"getAndCacheSolcByUrl not called with the compiler file name"
124+
"getAndCacheSoljsonByUrl not called with the compiler file name"
124125
);
125126
});
126127
});
127128

128129
describe("when the version is cached", () => {
129130
beforeEach(() => {
130-
sinon.stub(instance.cache, "has").returns(true);
131+
sinon.stub(instance.cache!, "has").returns(true);
131132
});
132133
afterEach(() => {
133-
unStub(instance.cache, "has");
134+
unStub(instance.cache!, "has");
134135
});
135136

136137
it("calls getCachedSolcByFileName", async () => {
@@ -146,13 +147,15 @@ describe("VersionRange loading strategy", () => {
146147

147148
describe("when the version is not cached", () => {
148149
beforeEach(() => {
149-
sinon.stub(instance.cache, "has").returns(false);
150-
sinon.stub(instance.cache, "add");
151-
sinon.stub(instance, "compilerFromString").returns("compiler");
150+
sinon.stub(instance.cache!, "has").returns(false);
151+
sinon.stub(instance.cache!, "add");
152+
sinon
153+
.stub(instance, "compilerFromString")
154+
.returns(Promise.resolve("compiler"));
152155
});
153156
afterEach(() => {
154-
unStub(instance.cache, "has");
155-
unStub(instance.cache, "add");
157+
unStub(instance.cache!, "has");
158+
unStub(instance.cache!, "add");
156159
unStub(instance, "compilerFromString");
157160
});
158161

@@ -178,16 +181,16 @@ describe("VersionRange loading strategy", () => {
178181
sinon
179182
.stub(instance, "compilerFromString")
180183
.withArgs("requestReturn")
181-
.returns("success");
184+
.returns(Promise.resolve("success"));
182185
});
183186
afterEach(() => {
184187
unStub(axios, "get");
185-
unStub(instance.cache, "add");
188+
unStub(instance.cache!, "add");
186189
unStub(instance, "compilerFromString");
187190
});
188191

189192
it("calls add with the response and the file name", async () => {
190-
const result = await instance.getAndCacheSolcByUrl(fileName, 0);
193+
const result = await instance.getAndCacheSoljsonByUrl(fileName, 0);
191194
assert.isTrue(
192195
// @ts-ignore
193196
instance.cache.add.calledWith("requestReturn", "someSolcFile")
@@ -216,10 +219,10 @@ describe("VersionRange loading strategy", () => {
216219

217220
describe("versionIsCached(version)", () => {
218221
beforeEach(() => {
219-
sinon.stub(instance.cache, "list").returns(compilerFileNames);
222+
sinon.stub(instance.cache!, "list").returns(compilerFileNames);
220223
});
221224
afterEach(() => {
222-
unStub(instance.cache, "list");
225+
unStub(instance.cache!, "list");
223226
});
224227

225228
describe("when a cached version of the compiler is present", () => {
@@ -246,11 +249,11 @@ describe("VersionRange loading strategy", () => {
246249
describe("getCachedSolcByVersionRange(version)", () => {
247250
beforeEach(() => {
248251
expectedResult = "soljson-v0.4.23+commit.1534a40d.js";
249-
sinon.stub(instance.cache, "list").returns(compilerFileNames);
252+
sinon.stub(instance.cache!, "list").returns(compilerFileNames);
250253
sinon.stub(instance, "getCachedSolcByFileName");
251254
});
252255
afterEach(() => {
253-
unStub(instance.cache, "list");
256+
unStub(instance.cache!, "list");
254257
unStub(instance, "getCachedSolcByFileName");
255258
});
256259

packages/compile-solidity/src/compilerSupplier/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
/* eslint-env node, browser */
12
import path from "path";
23
import fs from "fs";
34
import semver from "semver";
45
import { StrategyOptions } from "./types";
56
import { Docker, Local, Native, VersionRange } from "./loadingStrategies";
6-
77
const defaultSolcVersion = "0.5.16";
88

99
type CompilerSupplierConstructorArgs = {
@@ -13,7 +13,7 @@ type CompilerSupplierConstructorArgs = {
1313
docker?: boolean;
1414
compilerRoots?: string[];
1515
dockerTagsUrl?: string;
16-
spawn: any;
16+
spawn?: any;
1717
};
1818
cache?: string;
1919
};
@@ -45,12 +45,12 @@ export class CompilerSupplier {
4545

4646
getStrategy() {
4747
const userSpecification = this.version;
48-
4948
let strategy: CompilerSupplierStrategy;
5049
const useDocker = this.docker;
5150
const useNative = userSpecification === "native";
5251
let useSpecifiedLocal: boolean | string | undefined;
53-
if (!userSpecification) {
52+
// don't attempt file system access in browser environment
53+
if (typeof window === "undefined") {
5454
useSpecifiedLocal =
5555
userSpecification &&
5656
(fs.existsSync(userSpecification) ||

0 commit comments

Comments
 (0)