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

Commit

Permalink
fix some types and tests, and add a check for the browser environment
Browse files Browse the repository at this point in the history
  • Loading branch information
eggplantzzz committed Jan 10, 2023
1 parent f665753 commit 97a2ed3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ let fakeReturn = {
describe("CompilerSupplier", () => {
beforeEach(function () {
supplierOptions = {
events: config.events
events: config.events,
cache: "fileSystem"
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import Config from "@truffle/config";
const config = Config.default();
let versionRangeOptions = {
events: config.events,
solcConfig: config.compilers.solc
solcConfig: config.compilers.solc,
cache: "fileSystem"
};
const instance = new LoadingStrategies.VersionRange(versionRangeOptions);
let fileName, expectedResult;
let fileName: string, expectedResult: any;
const compilerFileNames = [
"soljson-v0.4.22+commit.124ca40d.js",
"soljson-v0.4.23+commit.1534a40d.js",
Expand Down Expand Up @@ -105,32 +106,32 @@ describe("VersionRange loading strategy", () => {

describe("when a version constraint is specified", () => {
beforeEach(() => {
sinon.stub(instance, "getAndCacheSolcByUrl");
sinon.stub(instance.cache, "has").returns(false);
sinon.stub(instance, "getAndCacheSoljsonByUrl");
sinon.stub(instance.cache!, "has").returns(false);
});
afterEach(() => {
unStub(instance, "getAndCacheSolcByUrl");
unStub(instance.cache, "has");
unStub(instance, "getAndCacheSoljsonByUrl");
unStub(instance.cache!, "has");
});

it("calls findNewstValidVersion to determine which version to fetch", async () => {
await instance.getSolcFromCacheOrUrl("^0.5.0");
assert.isTrue(
// @ts-ignore
instance.getAndCacheSolcByUrl.calledWith(
instance.getAndCacheSoljsonByUrl.calledWith(
"soljson-v0.5.4+commit.9549d8ff.js"
),
"getAndCacheSolcByUrl not called with the compiler file name"
"getAndCacheSoljsonByUrl not called with the compiler file name"
);
});
});

describe("when the version is cached", () => {
beforeEach(() => {
sinon.stub(instance.cache, "has").returns(true);
sinon.stub(instance.cache!, "has").returns(true);
});
afterEach(() => {
unStub(instance.cache, "has");
unStub(instance.cache!, "has");
});

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

describe("when the version is not cached", () => {
beforeEach(() => {
sinon.stub(instance.cache, "has").returns(false);
sinon.stub(instance.cache, "add");
sinon.stub(instance, "compilerFromString").returns("compiler");
sinon.stub(instance.cache!, "has").returns(false);
sinon.stub(instance.cache!, "add");
sinon
.stub(instance, "compilerFromString")
.returns(Promise.resolve("compiler"));
});
afterEach(() => {
unStub(instance.cache, "has");
unStub(instance.cache, "add");
unStub(instance.cache!, "has");
unStub(instance.cache!, "add");
unStub(instance, "compilerFromString");
});

Expand All @@ -178,16 +181,16 @@ describe("VersionRange loading strategy", () => {
sinon
.stub(instance, "compilerFromString")
.withArgs("requestReturn")
.returns("success");
.returns(Promise.resolve("success"));
});
afterEach(() => {
unStub(axios, "get");
unStub(instance.cache, "add");
unStub(instance.cache!, "add");
unStub(instance, "compilerFromString");
});

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

describe("versionIsCached(version)", () => {
beforeEach(() => {
sinon.stub(instance.cache, "list").returns(compilerFileNames);
sinon.stub(instance.cache!, "list").returns(compilerFileNames);
});
afterEach(() => {
unStub(instance.cache, "list");
unStub(instance.cache!, "list");
});

describe("when a cached version of the compiler is present", () => {
Expand All @@ -246,11 +249,11 @@ describe("VersionRange loading strategy", () => {
describe("getCachedSolcByVersionRange(version)", () => {
beforeEach(() => {
expectedResult = "soljson-v0.4.23+commit.1534a40d.js";
sinon.stub(instance.cache, "list").returns(compilerFileNames);
sinon.stub(instance.cache!, "list").returns(compilerFileNames);
sinon.stub(instance, "getCachedSolcByFileName");
});
afterEach(() => {
unStub(instance.cache, "list");
unStub(instance.cache!, "list");
unStub(instance, "getCachedSolcByFileName");
});

Expand Down
8 changes: 4 additions & 4 deletions packages/compile-solidity/src/compilerSupplier/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-env node, browser */
import path from "path";
import fs from "fs";
import semver from "semver";
import { StrategyOptions } from "./types";
import { Docker, Local, Native, VersionRange } from "./loadingStrategies";

const defaultSolcVersion = "0.5.16";

type CompilerSupplierConstructorArgs = {
Expand All @@ -13,7 +13,7 @@ type CompilerSupplierConstructorArgs = {
docker?: boolean;
compilerRoots?: string[];
dockerTagsUrl?: string;
spawn: any;
spawn?: any;
};
cache?: string;
};
Expand Down Expand Up @@ -45,12 +45,12 @@ export class CompilerSupplier {

getStrategy() {
const userSpecification = this.version;

let strategy: CompilerSupplierStrategy;
const useDocker = this.docker;
const useNative = userSpecification === "native";
let useSpecifiedLocal: boolean | string | undefined;
if (!userSpecification) {
// don't attempt file system access in browser environment
if (typeof window === "undefined") {
useSpecifiedLocal =
userSpecification &&
(fs.existsSync(userSpecification) ||
Expand Down

0 comments on commit 97a2ed3

Please sign in to comment.