-
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
8 changed files
with
144 additions
and
232 deletions.
There are no files selected for viewing
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
144 changes: 92 additions & 52 deletions
144
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,106 @@ | ||
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 { | ||
BuildOptions, | ||
SolidityBuildSystem, | ||
} from "../../../types/solidity/build-system.js"; | ||
import type { | ||
ArtifactId as EdrArtifactId, | ||
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 { readJsonFile } from "@ignored/hardhat-vnext-utils/fs"; | ||
|
||
import { FileBuildResultType } from "../../../types/solidity/build-system.js"; | ||
import { shouldMergeCompilationJobs } from "../solidity/build-profiles.js"; | ||
|
||
const id = { | ||
name: hardhatArtifact.contractName, | ||
solcVersion: buildInfo.solcVersion, | ||
source: hardhatArtifact.sourceName, | ||
}; | ||
export interface TestCompileResult { | ||
artifacts: EdrArtifact[]; | ||
testSuiteIds: EdrArtifactId[]; | ||
} | ||
|
||
const contract = { | ||
abi: JSON.stringify(hardhatArtifact.abi), | ||
bytecode: hardhatArtifact.bytecode, | ||
deployedBytecode: hardhatArtifact.deployedBytecode, | ||
}; | ||
export async function testCompile( | ||
solidity: SolidityBuildSystem, | ||
rootFilePaths: string[], | ||
testFilePaths: string[], | ||
projectRoot: string, | ||
artifactsPath: string, | ||
buildProfile: string, | ||
): Promise<TestCompileResult> { | ||
const buildOptions: BuildOptions = { | ||
force: false, | ||
buildProfile, | ||
mergeCompilationJobs: shouldMergeCompilationJobs(buildProfile), | ||
quiet: false, | ||
}; | ||
|
||
const artifact = { id, contract }; | ||
artifacts.push(artifact); | ||
} | ||
const results = await solidity.build(rootFilePaths, buildOptions); | ||
|
||
return artifacts; | ||
} | ||
if ("reason" in results) { | ||
throw new HardhatError( | ||
HardhatError.ERRORS.SOLIDITY.COMPILATION_JOB_CREATION_ERROR, | ||
{ | ||
reason: results.formattedReason, | ||
rootFilePath: results.rootFilePath, | ||
buildProfile: results.buildProfile, | ||
}, | ||
); | ||
} | ||
|
||
export async function isTestArtifact( | ||
root: string, | ||
artifact: Artifact, | ||
): Promise<boolean> { | ||
const { source } = artifact.id; | ||
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 testCompileResult: TestCompileResult = { | ||
artifacts: [], | ||
testSuiteIds: [], | ||
}; | ||
|
||
for (const [filePath, result] of results.entries()) { | ||
if ( | ||
result.type === FileBuildResultType.BUILD_SUCCESS || | ||
result.type === FileBuildResultType.CACHE_HIT | ||
) { | ||
for (const artifactPath of result.contractArtifactsGenerated) { | ||
const artifact: HardhatArtifact = await readJsonFile(artifactPath); | ||
const buildInfo: BuildInfo = await readJsonFile( | ||
path.join(artifactsPath, "build-info", `${result.buildId}.json`), | ||
); | ||
|
||
const id = { | ||
name: artifact.contractName, | ||
solcVersion: buildInfo.solcVersion, | ||
source: artifact.sourceName, | ||
}; | ||
|
||
if (!sourceExists) { | ||
return false; | ||
const contract = { | ||
abi: JSON.stringify(artifact.abi), | ||
bytecode: artifact.bytecode, | ||
deployedBytecode: artifact.deployedBytecode, | ||
}; | ||
|
||
testCompileResult.artifacts.push({ | ||
id, | ||
contract, | ||
}); | ||
|
||
if (testFilePaths.includes(path.join(projectRoot, filePath))) { | ||
testCompileResult.testSuiteIds.push(id); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
return testCompileResult; | ||
} |
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
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
Oops, something went wrong.