|
| 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