Skip to content

Commit

Permalink
Improve and test remappings parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Aug 28, 2023
1 parent 845255c commit aa49e8a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 16 deletions.
45 changes: 30 additions & 15 deletions packages/hardhat-foundry/src/foundry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,39 @@ export function getForgeConfig() {
return JSON.parse(runCmdSync("forge config --json"));
}

export async function getRemappings() {
// Get remappings only once
if (cachedRemappings === undefined) {
cachedRemappings = runCmd("forge remappings").then((remappingsTxt) => {
const remappings: Remappings = {};
const remappingLines = remappingsTxt.split(/\r\n|\r|\n/);
for (const remappingLine of remappingLines) {
const fromTo = remappingLine.split("=");
export function parseRemappings(remappingsTxt: string): Remappings {
const remappings: Remappings = {};
const remappingLines = remappingsTxt.split(/\r\n|\r|\n/);
for (const remappingLine of remappingLines) {
if (remappingLine.includes(":")) {
throw new HardhatFoundryError(
`Invalid remapping '${remappingLine}', remapping contexts are not allowed`
);
}

if (fromTo.length !== 2 || fromTo[0].includes(":")) {
continue;
}
if (!remappingLine.includes("=")) {
throw new HardhatFoundryError(
`Invalid remapping '${remappingLine}', remappings without a target are not allowed`
);
}

const fromTo = remappingLine.split("=");

remappings[fromTo[0]] = fromTo[1];
}
// if the remapping already exists, we ignore it because the first one wins
if (remappings[fromTo[0]] !== undefined) {
continue;
}

return remappings;
});
remappings[fromTo[0]] = fromTo[1];
}

return remappings;
}

export async function getRemappings() {
// Get remappings only once
if (cachedRemappings === undefined) {
cachedRemappings = runCmd("forge remappings").then(parseRemappings);
}

return cachedRemappings;
Expand Down
1 change: 0 additions & 1 deletion packages/hardhat-foundry/test/dummy.ts

This file was deleted.

36 changes: 36 additions & 0 deletions packages/hardhat-foundry/test/foundry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect } from "chai";
import { HardhatFoundryError, parseRemappings } from "../src/foundry";

describe("foundry module", function () {
describe("parseRemappings", function () {
it("should parse simple remappings", async function () {
const remappings = parseRemappings("a=b\nb=c\nc=d");

expect(remappings).to.deep.equal({
a: "b",
b: "c",
c: "d",
});
});

it("should throw if a remapping has a context", async function () {
expect(() => parseRemappings("a:b=c")).to.throw(
"Invalid remapping 'a:b=c', remapping contexts are not allowed"
);
});

it("should throw if a remapping doesn't have a target", async function () {
expect(() => parseRemappings("a")).to.throw(
"Invalid remapping 'a', remappings without a target are not allowed"
);
});

it("should use the first remapping if more than one has the same prefix", async function () {
const remappings = parseRemappings("a=b\na=c");

expect(remappings).to.deep.equal({
a: "b",
});
});
});
});

0 comments on commit aa49e8a

Please sign in to comment.