-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: compile solidity test sources only
- Loading branch information
Showing
4 changed files
with
149 additions
and
72 deletions.
There are no files selected for viewing
168 changes: 116 additions & 52 deletions
168
v-next/hardhat/src/internal/builtin-plugins/solidity-test/helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,130 @@ | ||
import type { ArtifactsManager } from "../../../types/artifacts.js"; | ||
import type { Artifact } from "@ignored/edr"; | ||
import type { | ||
Artifact as HardhatArtifact, | ||
BuildInfo, | ||
} from "../../../types/artifacts.js"; | ||
import type { Artifact as EdrArtifact } from "@ignored/edr"; | ||
|
||
import path from "node:path"; | ||
|
||
import { HardhatError } from "@ignored/hardhat-vnext-errors"; | ||
import { exists } from "@ignored/hardhat-vnext-utils/fs"; | ||
import { resolveFromRoot } from "@ignored/hardhat-vnext-utils/path"; | ||
|
||
export async function getArtifacts( | ||
hardhatArtifacts: ArtifactsManager, | ||
): Promise<Artifact[]> { | ||
const fqns = await hardhatArtifacts.getAllFullyQualifiedNames(); | ||
const artifacts: Artifact[] = []; | ||
|
||
for (const fqn of fqns) { | ||
const hardhatArtifact = await hardhatArtifacts.readArtifact(fqn); | ||
const buildInfo = await hardhatArtifacts.getBuildInfo(fqn); | ||
|
||
if (buildInfo === undefined) { | ||
throw new HardhatError( | ||
HardhatError.ERRORS.SOLIDITY_TESTS.BUILD_INFO_NOT_FOUND_FOR_CONTRACT, | ||
{ | ||
fqn, | ||
}, | ||
); | ||
} | ||
import { | ||
getAllFilesMatching, | ||
readJsonFile, | ||
} from "@ignored/hardhat-vnext-utils/fs"; | ||
|
||
const id = { | ||
name: hardhatArtifact.contractName, | ||
solcVersion: buildInfo.solcVersion, | ||
source: hardhatArtifact.sourceName, | ||
}; | ||
import { | ||
FileBuildResultType, | ||
type BuildOptions, | ||
type SolidityBuildSystem, | ||
} from "../../../types/solidity/build-system.js"; | ||
import { shouldMergeCompilationJobs } from "../solidity/build-profiles.js"; | ||
import { buildDependencyGraph } from "../solidity/build-system/dependency-graph-building.js"; | ||
import { | ||
isNpmRootPath, | ||
npmModuleToNpmRootPath, | ||
} from "../solidity/build-system/root-paths-utils.js"; | ||
|
||
const contract = { | ||
abi: JSON.stringify(hardhatArtifact.abi), | ||
bytecode: hardhatArtifact.bytecode, | ||
deployedBytecode: hardhatArtifact.deployedBytecode, | ||
}; | ||
export interface TestCompileOptions { | ||
projectRoot: string; | ||
artifactsPath: string; | ||
dependenciesToCompile: string[]; | ||
remappings: string[]; | ||
buildProfile: string; | ||
} | ||
|
||
const artifact = { id, contract }; | ||
artifacts.push(artifact); | ||
} | ||
export async function testCompile( | ||
solidity: SolidityBuildSystem, | ||
testFilePaths: string[], | ||
options: TestCompileOptions, | ||
): Promise<EdrArtifact[]> { | ||
const { dependencyGraph } = await buildDependencyGraph( | ||
testFilePaths.toSorted(), // We sort them to have a deterministic order | ||
options.projectRoot, | ||
options.remappings, | ||
); | ||
|
||
return artifacts; | ||
} | ||
const localFilesToCompile = [...dependencyGraph.getAllFiles()].map( | ||
({ fsPath }) => fsPath, | ||
); | ||
|
||
const dependenciesToCompile = options.dependenciesToCompile.map( | ||
npmModuleToNpmRootPath, | ||
); | ||
|
||
const rootFilePaths = [...localFilesToCompile, ...dependenciesToCompile]; | ||
|
||
const buildOptions: BuildOptions = { | ||
force: false, | ||
buildProfile: options.buildProfile, | ||
mergeCompilationJobs: shouldMergeCompilationJobs(options.buildProfile), | ||
quiet: false, | ||
}; | ||
|
||
const results = await solidity.build(rootFilePaths, buildOptions); | ||
|
||
export async function isTestArtifact( | ||
root: string, | ||
artifact: Artifact, | ||
): Promise<boolean> { | ||
const { source } = artifact.id; | ||
if ("reason" in results) { | ||
throw new HardhatError( | ||
HardhatError.ERRORS.SOLIDITY.COMPILATION_JOB_CREATION_ERROR, | ||
{ | ||
reason: results.formattedReason, | ||
rootFilePath: results.rootFilePath, | ||
buildProfile: results.buildProfile, | ||
}, | ||
); | ||
} | ||
|
||
const sucessful = [...results.values()].every( | ||
({ type }) => | ||
type === FileBuildResultType.CACHE_HIT || | ||
type === FileBuildResultType.BUILD_SUCCESS, | ||
); | ||
|
||
if (!source.endsWith(".t.sol")) { | ||
return false; | ||
if (!sucessful) { | ||
throw new HardhatError(HardhatError.ERRORS.SOLIDITY.BUILD_FAILED); | ||
} | ||
|
||
// NOTE: We also check whether the file exists in the workspace to filter out | ||
// the artifacts from node modules. | ||
const sourcePath = resolveFromRoot(root, source); | ||
const sourceExists = await exists(sourcePath); | ||
const artifacts: EdrArtifact[] = []; | ||
|
||
for (const rootFilePath of rootFilePaths) { | ||
const sourceName: string = isNpmRootPath(rootFilePath) | ||
? rootFilePath.replace(/^npm:/, "") | ||
: path.relative(options.projectRoot, rootFilePath); | ||
|
||
if (!sourceExists) { | ||
return false; | ||
const fileFolder = path.join(options.artifactsPath, sourceName); | ||
|
||
const contractArtifactPaths = await getAllFilesMatching(fileFolder, (f) => | ||
f.endsWith(".json"), | ||
); | ||
|
||
for (const contractArtifactPath of contractArtifactPaths) { | ||
const contractArtifact: HardhatArtifact = | ||
await readJsonFile(contractArtifactPath); | ||
const buildInfo: BuildInfo = await readJsonFile( | ||
path.join( | ||
options.artifactsPath, | ||
"build-info", | ||
`${contractArtifact.buildInfoId}.json`, | ||
), | ||
); | ||
|
||
const id = { | ||
name: contractArtifact.contractName, | ||
solcVersion: buildInfo.solcVersion, | ||
source: contractArtifact.sourceName, | ||
}; | ||
|
||
const contract = { | ||
abi: JSON.stringify(contractArtifact.abi), | ||
bytecode: contractArtifact.bytecode, | ||
deployedBytecode: contractArtifact.deployedBytecode, | ||
}; | ||
|
||
artifacts.push({ | ||
id, | ||
contract, | ||
}); | ||
} | ||
} | ||
|
||
return true; | ||
return artifacts; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters