Skip to content

Commit fc2e236

Browse files
committed
test: implement dependency graph building tests
1 parent 01c3fec commit fc2e236

File tree

10 files changed

+130
-0
lines changed

10 files changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.0;
3+
pragma solidity *;
4+
5+
import "./B.sol";
6+
7+
contract A is B {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.0;
3+
4+
import "@openzeppelin/contracts/access/Ownable.sol";
5+
6+
contract B {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.0;
3+
4+
import "./B.sol";
5+
6+
contract C {}
7+
8+
contract C2 {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.22;
3+
4+
import "./C.sol";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.22;
3+
4+
import "./C.sol";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
2+
pragma solidity ^0.7.0;
3+
4+
contract NoImports {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
2+
pragma solidity ^0.8.0;
3+
4+
import "remapped/Ownable.sol";

v-next/hardhat/test/fixture-projects/solidity/example-project/node_modules/@openzeppelin/contracts/access/Ownable.sol

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v-next/hardhat/test/fixture-projects/solidity/example-project/node_modules/@openzeppelin/contracts/package.json

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import assert from "node:assert/strict";
2+
import path from "node:path";
3+
import { describe, it } from "node:test";
4+
5+
import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils";
6+
7+
import { buildDependencyGraph } from "../../../../../src/internal/builtin-plugins/solidity/build-system/dependency-graph-building.js";
8+
9+
describe("buildDependencyGraph", () => {
10+
useFixtureProject("solidity/example-project");
11+
12+
it("should return an empty graph if no files are provided", async () => {
13+
const { dependencyGraph, resolver } = await buildDependencyGraph(
14+
[],
15+
process.cwd(),
16+
[],
17+
);
18+
19+
assert.equal(dependencyGraph.getRoots().size, 0);
20+
assert.equal(Array.from(dependencyGraph.getAllFiles()).length, 0);
21+
22+
assert.equal(resolver.getRemappings().length, 0);
23+
});
24+
25+
it("should build a graph from given files", async () => {
26+
const rootSourceNames = [
27+
"contracts/A.sol",
28+
"contracts/D.sol",
29+
"contracts/NoImports.sol",
30+
"contracts/UserRemappedImport.sol",
31+
];
32+
const dependencySourceNames = [
33+
"contracts/B.sol",
34+
"contracts/C.sol",
35+
"npm/@openzeppelin/[email protected]/access/Ownable.sol",
36+
];
37+
38+
const { dependencyGraph, resolver } = await buildDependencyGraph(
39+
rootSourceNames.map((sourceName) => path.join(process.cwd(), sourceName)),
40+
process.cwd(),
41+
["remapped/=npm/@openzeppelin/[email protected]/access/"],
42+
);
43+
44+
const roots = dependencyGraph.getRoots();
45+
assert.equal(
46+
roots.size,
47+
rootSourceNames.length,
48+
`Should have ${rootSourceNames.length} roots`,
49+
);
50+
for (const sourceName of rootSourceNames) {
51+
assert.ok(roots.has(sourceName), `Should have root ${sourceName}`);
52+
}
53+
54+
const files = Array.from(dependencyGraph.getAllFiles()).map(
55+
(file) => file.sourceName,
56+
);
57+
assert.equal(files.length, 7, "Should have 7 files");
58+
for (const sourceName of rootSourceNames.concat(dependencySourceNames)) {
59+
assert.ok(files.includes(sourceName), `Should have file ${sourceName}`);
60+
}
61+
62+
const remappings = resolver.getRemappings();
63+
assert.equal(remappings.length, 2, "Should have 2 remappings");
64+
assert.ok(
65+
remappings.some(
66+
(r) =>
67+
r.context === "" &&
68+
r.prefix === "remapped/" &&
69+
r.target === "npm/@openzeppelin/[email protected]/access/",
70+
),
71+
"Should have remapping for Ownable.sol",
72+
);
73+
assert.ok(
74+
remappings.some(
75+
(r) =>
76+
r.context === "" &&
77+
r.prefix === "@openzeppelin/contracts/" &&
78+
r.target === "npm/@openzeppelin/[email protected]/",
79+
),
80+
"Should have remapping for @openzeppelin/contracts",
81+
);
82+
});
83+
});

0 commit comments

Comments
 (0)