-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: define solidity test sources in config #5870
Open
galargh
wants to merge
7
commits into
v-next
Choose a base branch
from
feat/solidity-test-compilation
base: v-next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6dea780
feat: define solidity test sources in config
galargh 0ebcfbf
feat: compile solidity test sources only
galargh 63e0215
docs: add inline comments to solidity test helpers functions
galargh 4fca1da
Merge remote-tracking branch 'origin/v-next' into feat/solidity-test-…
galargh a4a5b61
fix: ran pnpm prettier --write to fix linting error
galargh 67daa57
Merge branch 'v-next' into feat/solidity-test-compilation
galargh 18dca31
chore: restore the semantics of paths.tests.solidity
galargh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
File renamed without changes.
151 changes: 101 additions & 50 deletions
151
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,117 @@ | ||
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 { HardhatError } from "@ignored/hardhat-vnext-errors"; | ||
import { exists } from "@ignored/hardhat-vnext-utils/fs"; | ||
import { resolveFromRoot } from "@ignored/hardhat-vnext-utils/path"; | ||
import path from "node:path"; | ||
|
||
export async function getArtifacts( | ||
hardhatArtifacts: ArtifactsManager, | ||
): Promise<Artifact[]> { | ||
const fqns = await hardhatArtifacts.getAllFullyQualifiedNames(); | ||
const artifacts: Artifact[] = []; | ||
import { HardhatError } from "@ignored/hardhat-vnext-errors"; | ||
import { readJsonFile } from "@ignored/hardhat-vnext-utils/fs"; | ||
|
||
for (const fqn of fqns) { | ||
const hardhatArtifact = await hardhatArtifacts.readArtifact(fqn); | ||
const buildInfo = await hardhatArtifacts.getBuildInfo(fqn); | ||
import { FileBuildResultType } from "../../../types/solidity/build-system.js"; | ||
|
||
if (buildInfo === undefined) { | ||
throw new HardhatError( | ||
HardhatError.ERRORS.SOLIDITY_TESTS.BUILD_INFO_NOT_FOUND_FOR_CONTRACT, | ||
{ | ||
fqn, | ||
}, | ||
); | ||
} | ||
type SolidityBuildResults = | ||
| Map<string, FileBuildResult> | ||
| CompilationJobCreationError; | ||
type SuccessfulSolidityBuildResults = Map< | ||
string, | ||
Exclude<FileBuildResult, FailedFileBuildResult> | ||
>; | ||
|
||
const id = { | ||
name: hardhatArtifact.contractName, | ||
solcVersion: buildInfo.solcVersion, | ||
source: hardhatArtifact.sourceName, | ||
}; | ||
/** | ||
* This function asserts that the given Solidity build results are successful. | ||
* It throws a HardhatError if the build results indicate that the compilation | ||
* job failed. | ||
*/ | ||
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, | ||
}, | ||
); | ||
} | ||
|
||
const contract = { | ||
abi: JSON.stringify(hardhatArtifact.abi), | ||
bytecode: hardhatArtifact.bytecode, | ||
deployedBytecode: hardhatArtifact.deployedBytecode, | ||
}; | ||
const sucessful = [...results.values()].every( | ||
({ type }) => | ||
type === FileBuildResultType.CACHE_HIT || | ||
type === FileBuildResultType.BUILD_SUCCESS, | ||
); | ||
|
||
const artifact = { id, contract }; | ||
artifacts.push(artifact); | ||
if (!sucessful) { | ||
throw new HardhatError(HardhatError.ERRORS.SOLIDITY.BUILD_FAILED); | ||
} | ||
|
||
return artifacts; | ||
} | ||
|
||
export async function isTestArtifact( | ||
root: string, | ||
artifact: Artifact, | ||
): Promise<boolean> { | ||
const { source } = artifact.id; | ||
/** | ||
* This function returns the artifacts generated during the compilation associated | ||
* with the given Solidity build results. It relies on the fact that each successful | ||
* build result has a corresponding artifact generated property. | ||
*/ | ||
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()) { | ||
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`), | ||
); | ||
|
||
// 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 id = { | ||
name: artifact.contractName, | ||
solcVersion: buildInfo.solcVersion, | ||
source, | ||
}; | ||
|
||
if (!sourceExists) { | ||
return false; | ||
const contract = { | ||
abi: JSON.stringify(artifact.abi), | ||
bytecode: artifact.bytecode, | ||
deployedBytecode: artifact.deployedBytecode, | ||
}; | ||
|
||
artifacts.push({ | ||
id, | ||
contract, | ||
}); | ||
} | ||
} | ||
|
||
return true; | ||
return artifacts; | ||
} | ||
|
||
/** | ||
* This function returns the test suite ids associated with the given artifacts. | ||
* The test suite ID is the relative path of the test file, relative to the | ||
* project root. | ||
*/ | ||
export async function getTestSuiteIds( | ||
artifacts: EdrArtifact[], | ||
rootTestFilePaths: string[], | ||
projectRoot: string, | ||
): Promise<EdrArtifactId[]> { | ||
const testSources = rootTestFilePaths.map((p) => | ||
path.relative(projectRoot, p), | ||
); | ||
|
||
return artifacts | ||
.map(({ id }) => id) | ||
.filter(({ source }) => testSources.includes(source)); | ||
} |
61 changes: 61 additions & 0 deletions
61
v-next/hardhat/src/internal/builtin-plugins/solidity-test/hook-handlers/config.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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { ConfigHooks } from "@ignored/hardhat-vnext/types/hooks"; | ||
|
||
import { isObject } from "@ignored/hardhat-vnext-utils/lang"; | ||
import { resolveFromRoot } from "@ignored/hardhat-vnext-utils/path"; | ||
import { | ||
conditionalUnionType, | ||
validateUserConfigZodType, | ||
} from "@ignored/hardhat-vnext-zod-utils"; | ||
import { z } from "zod"; | ||
|
||
const userConfigType = z.object({ | ||
paths: z | ||
.object({ | ||
test: conditionalUnionType( | ||
[ | ||
[isObject, z.object({ solidity: z.string().optional() })], | ||
[(data) => typeof data === "string", z.string()], | ||
], | ||
"Expected a string or an object with an optional 'solidity' property", | ||
).optional(), | ||
}) | ||
.optional(), | ||
}); | ||
|
||
export default async (): Promise<Partial<ConfigHooks>> => { | ||
const handlers: Partial<ConfigHooks> = { | ||
validateUserConfig: async (userConfig) => { | ||
return validateUserConfigZodType(userConfig, userConfigType); | ||
}, | ||
resolveUserConfig: async ( | ||
userConfig, | ||
resolveConfigurationVariable, | ||
next, | ||
) => { | ||
const resolvedConfig = await next( | ||
userConfig, | ||
resolveConfigurationVariable, | ||
); | ||
|
||
let testsPath = userConfig.paths?.tests; | ||
|
||
// TODO: use isObject when the type narrowing issue is fixed | ||
testsPath = | ||
typeof testsPath === "object" ? testsPath.solidity : testsPath; | ||
testsPath ??= "test"; | ||
|
||
return { | ||
...resolvedConfig, | ||
paths: { | ||
...resolvedConfig.paths, | ||
tests: { | ||
...resolvedConfig.paths.tests, | ||
solidity: resolveFromRoot(resolvedConfig.paths.root, testsPath), | ||
}, | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
return handlers; | ||
}; |
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
11 changes: 11 additions & 0 deletions
11
v-next/hardhat/src/internal/builtin-plugins/solidity-test/type-extensions.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import "@ignored/hardhat-vnext/types/config"; | ||
|
||
declare module "@ignored/hardhat-vnext/types/config" { | ||
export interface TestPathsUserConfig { | ||
solidity?: string; | ||
} | ||
|
||
export interface TestPathsConfig { | ||
solidity: string; | ||
} | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a future PR: we should use the same implementation here and during
compile
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mostly to have a consistent ui