-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
164 additions
and
229 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
148 changes: 98 additions & 50 deletions
148
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,114 @@ | ||
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 { | ||
CompilationJobCreationError, | ||
FailedFileBuildResult, | ||
FileBuildResult, | ||
} 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"; | ||
import { readJsonFile } from "@ignored/hardhat-vnext-utils/fs"; | ||
|
||
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 { FileBuildResultType } from "../../../types/solidity/build-system.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, | ||
}; | ||
type SolidityBuildResults = | ||
| Map<string, FileBuildResult> | ||
| CompilationJobCreationError; | ||
type SuccessfulSolidityBuildResults = Map< | ||
string, | ||
Exclude<FileBuildResult, FailedFileBuildResult> | ||
>; | ||
|
||
const artifact = { id, contract }; | ||
artifacts.push(artifact); | ||
export function throwIfSolidityBuildFailed( | ||
results: SolidityBuildResults, | ||
): asserts results is SuccessfulSolidityBuildResults { | ||
if ("reason" in results) { | ||
throw new HardhatError( | ||
HardhatError.ERRORS.SOLIDITY.COMPILATION_JOB_CREATION_ERROR, | ||
{ | ||
reason: results.formattedReason, | ||
rootFilePath: results.rootFilePath, | ||
buildProfile: results.buildProfile, | ||
}, | ||
); | ||
} | ||
|
||
return artifacts; | ||
const sucessful = [...results.values()].every( | ||
({ type }) => | ||
type === FileBuildResultType.CACHE_HIT || | ||
type === FileBuildResultType.BUILD_SUCCESS, | ||
); | ||
|
||
if (!sucessful) { | ||
throw new HardhatError(HardhatError.ERRORS.SOLIDITY.BUILD_FAILED); | ||
} | ||
} | ||
|
||
export async function isTestArtifact( | ||
root: string, | ||
artifact: Artifact, | ||
): Promise<boolean> { | ||
const { source } = artifact.id; | ||
export async function getArtifacts( | ||
results: SuccessfulSolidityBuildResults, | ||
artifactsRootPath: string, | ||
): Promise<EdrArtifact[]> { | ||
const artifacts: EdrArtifact[] = []; | ||
|
||
if (!source.endsWith(".t.sol")) { | ||
return false; | ||
} | ||
for (const [source, 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(artifactsRootPath, "build-info", `${result.buildId}.json`), | ||
); | ||
|
||
const id = { | ||
name: artifact.contractName, | ||
solcVersion: buildInfo.solcVersion, | ||
source, | ||
}; | ||
|
||
// 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 contract = { | ||
abi: JSON.stringify(artifact.abi), | ||
bytecode: artifact.bytecode, | ||
deployedBytecode: artifact.deployedBytecode, | ||
}; | ||
|
||
if (!sourceExists) { | ||
return false; | ||
artifacts.push({ | ||
id, | ||
contract, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
return artifacts; | ||
} | ||
|
||
export async function getTestSuiteIds( | ||
artifacts: EdrArtifact[], | ||
rootFilePaths: string[], | ||
projectRoot: string, | ||
): Promise<EdrArtifactId[]> { | ||
const testSources = rootFilePaths | ||
.filter((p) => { | ||
return p.endsWith(".t.sol"); | ||
}) | ||
.map((p) => path.relative(projectRoot, p)); | ||
|
||
return artifacts | ||
.map(({ id }) => id) | ||
.filter(({ source }) => testSources.includes(source)); | ||
} |
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.