|
| 1 | +import type { CompileCache } from "../../../../../../src/internal/builtin-plugins/solidity/build-system/cache.js"; |
| 2 | + |
1 | 3 | import assert from "node:assert/strict"; |
2 | 4 | import { writeFile } from "node:fs/promises"; |
3 | 5 | import path from "node:path"; |
4 | 6 | import { describe, it } from "node:test"; |
5 | 7 |
|
| 8 | +import { readJsonFile, writeJsonFile } from "@nomicfoundation/hardhat-utils/fs"; |
| 9 | +import { isObject } from "micro-eth-signer/utils"; |
| 10 | + |
6 | 11 | import { FileBuildResultType } from "../../../../../../src/types/solidity/build-system.js"; |
7 | 12 | import { useTestProjectTemplate } from "../resolver/helpers.js"; |
8 | 13 |
|
@@ -171,5 +176,83 @@ contract Foo {}`, |
171 | 176 | FileBuildResultType.BUILD_SUCCESS, |
172 | 177 | ); |
173 | 178 | }); |
| 179 | + |
| 180 | + it("should return BUILD_SUCCESS when compilerType in cache differs", async () => { |
| 181 | + await using project = await useTestProjectTemplate({ |
| 182 | + name: "test-project", |
| 183 | + version: "1.0.0", |
| 184 | + files: { |
| 185 | + "contracts/Foo.sol": `// SPDX-License-Identifier: MIT |
| 186 | +pragma solidity ^0.8.0; |
| 187 | +contract Foo {}`, |
| 188 | + }, |
| 189 | + }); |
| 190 | + |
| 191 | + const hre = await getHRE(project); |
| 192 | + const filePath = path.join(project.path, "contracts/Foo.sol"); |
| 193 | + |
| 194 | + // First build |
| 195 | + await hre.solidity.build([filePath], { quiet: true }); |
| 196 | + |
| 197 | + // Tamper with the cache to simulate a different compiler type |
| 198 | + const cachePath = path.join(project.path, "cache", "compile-cache.json"); |
| 199 | + const cache: CompileCache = await readJsonFile(cachePath); |
| 200 | + for (const key of Object.keys(cache)) { |
| 201 | + cache[key].compilerType = "different-compiler"; |
| 202 | + } |
| 203 | + await writeJsonFile(cachePath, cache); |
| 204 | + |
| 205 | + // Second build should be a cache miss due to compiler type mismatch |
| 206 | + const result = await hre.solidity.build([filePath], { quiet: true }); |
| 207 | + assert( |
| 208 | + hre.solidity.isSuccessfulBuildResult(result), |
| 209 | + "Build should succeed", |
| 210 | + ); |
| 211 | + |
| 212 | + assert.equal( |
| 213 | + result.get(filePath)?.type, |
| 214 | + FileBuildResultType.BUILD_SUCCESS, |
| 215 | + ); |
| 216 | + }); |
| 217 | + |
| 218 | + it("should return BUILD_SUCCESS when compilerType is missing from cache (old format)", async () => { |
| 219 | + await using project = await useTestProjectTemplate({ |
| 220 | + name: "test-project", |
| 221 | + version: "1.0.0", |
| 222 | + files: { |
| 223 | + "contracts/Foo.sol": `// SPDX-License-Identifier: MIT |
| 224 | +pragma solidity ^0.8.0; |
| 225 | +contract Foo {}`, |
| 226 | + }, |
| 227 | + }); |
| 228 | + |
| 229 | + const hre = await getHRE(project); |
| 230 | + const filePath = path.join(project.path, "contracts/Foo.sol"); |
| 231 | + |
| 232 | + // First build |
| 233 | + await hre.solidity.build([filePath], { quiet: true }); |
| 234 | + |
| 235 | + // Remove compilerType from cache to simulate old cache format |
| 236 | + const cachePath = path.join(project.path, "cache", "compile-cache.json"); |
| 237 | + const cache = await readJsonFile(cachePath); |
| 238 | + assert.ok(isObject(cache), "Cache should be an object"); |
| 239 | + |
| 240 | + for (const key of Object.keys(cache)) { |
| 241 | + delete cache[key].compilerType; |
| 242 | + } |
| 243 | + await writeJsonFile(cachePath, cache); |
| 244 | + |
| 245 | + // Second build should be a cache miss due to missing compiler type |
| 246 | + const result = await hre.solidity.build([filePath], { quiet: true }); |
| 247 | + assert( |
| 248 | + hre.solidity.isSuccessfulBuildResult(result), |
| 249 | + "Build should succeed", |
| 250 | + ); |
| 251 | + |
| 252 | + assert.equal( |
| 253 | + result.get(filePath)?.type, |
| 254 | + FileBuildResultType.BUILD_SUCCESS, |
| 255 | + ); |
| 256 | + }); |
174 | 257 | }); |
175 | 258 | }); |
0 commit comments