Skip to content

Commit

Permalink
add tests for foundry's resolveImport
Browse files Browse the repository at this point in the history
  • Loading branch information
antico5 committed Dec 12, 2022
1 parent a9a162f commit da66e42
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/src/frameworks/Foundry/FoundryProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class FoundryProject extends Project {
public async resolveImportPath(file: string, importPath: string) {
let transformedPath = importPath;

// Apply remappings to importPath if it's not a relative import
if (!importPath.startsWith(".")) {
for (const { from, to } of this.remappings) {
if (importPath.startsWith(from)) {
Expand Down
85 changes: 85 additions & 0 deletions server/test/frameworks/foundry/FoundryProject.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { expect } from "chai";
import path from "path";
import { stub } from "sinon";
import * as basicCompilation from "../../../src/frameworks/shared/buildBasicCompilation";
import { FoundryProject } from "../../../src/frameworks/Foundry/FoundryProject";
import { ServerState } from "../../../src/types";

describe("FoundryProject", function () {
let project: FoundryProject;
const serverStateMock = {
logger: {},
} as ServerState;

beforeEach(async () => {
project = new FoundryProject(
serverStateMock,
path.join(__dirname, "test_project"),
path.join(__dirname, "test_project", "foundry.toml")
);
});

describe("resolveImportPath", function () {
it("resolves relative imports", async () => {
const foundImport = await project.resolveImportPath(
path.join(project.basePath, "src", "A.sol"),
"./B.sol"
);
const notFoundImport = await project.resolveImportPath(
path.join(project.basePath, "src", "A.sol"),
"./C.sol"
);
expect(foundImport).to.eq(path.join(project.basePath, "src", "B.sol"));
expect(notFoundImport).to.eq(undefined);
});

it("resolves root imports", async () => {
const importFromSameLevel = await project.resolveImportPath(
path.join(project.basePath, "src", "nested", "D.sol"),
"nested/E.sol"
);
const importFromParent = await project.resolveImportPath(
path.join(project.basePath, "src", "nested", "D.sol"),
"src/A.sol"
);
const importFromLib = await project.resolveImportPath(
path.join(project.basePath, "src", "nested", "D.sol"),
"lib/C.sol"
);
const illegalImport = await project.resolveImportPath(
path.join(project.basePath, "src", "A.sol"),
"foundry/Illegal.sol"
);

expect(importFromSameLevel).to.eq(
path.join(project.basePath, "src", "nested", "E.sol")
);
expect(importFromParent).to.eq(
path.join(project.basePath, "src", "A.sol")
);
expect(importFromLib).to.eq(path.join(project.basePath, "lib", "C.sol"));
expect(illegalImport).to.eq(undefined);
});
});

describe("buildCompilation", function () {
it("replaces absolute paths provided by buildBasicCompilation with root-relative paths", async () => {
const sourceUri = path.join(project.basePath, "src", "A.sol");

stub(basicCompilation, "buildBasicCompilation").resolves({
input: {
sources: {
[sourceUri]: { content: "" },
},
settings: {},
},
} as any);

const compilation = await project.buildCompilation(sourceUri, []);
expect(compilation.input.sources).to.deep.eq({
"src/A.sol": { content: "" },
});
});
});
});
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit da66e42

Please sign in to comment.