-
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: define solidity test sources in config
- Loading branch information
Showing
4 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
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 testsPaths: string[]; | ||
|
||
// TODO: use isObject when the type narrowing issue is fixed | ||
if ( | ||
typeof userConfig.paths?.tests === "object" && | ||
userConfig.paths.tests.solidity !== undefined | ||
) { | ||
if (Array.isArray(userConfig.paths.tests.solidity)) { | ||
testsPaths = userConfig.paths.tests.solidity; | ||
} else { | ||
testsPaths = [userConfig.paths.tests.solidity]; | ||
} | ||
} else { | ||
testsPaths = resolvedConfig.paths.sources.solidity; | ||
} | ||
|
||
const resolvedPaths = testsPaths.map((p) => | ||
resolveFromRoot(resolvedConfig.paths.root, p), | ||
); | ||
|
||
return { | ||
...resolvedConfig, | ||
paths: { | ||
...resolvedConfig.paths, | ||
tests: { | ||
...resolvedConfig.paths.tests, | ||
solidity: resolvedPaths, | ||
}, | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
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
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 | 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