-
Notifications
You must be signed in to change notification settings - Fork 15
Implement envOr(...) cheatcodes
#1103
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
base: master
Are you sure you want to change the base?
Changes from 26 commits
d49cf2a
456a4de
f8290ff
9729876
97e462f
c58ae26
2d1e247
d89787e
163c3d2
6419baf
f4851d6
ffc5f6c
17339c3
96aa709
ac50e00
390d82c
9577cfd
d5ca799
48f4923
12636fc
3c2eae5
507e86d
f356062
5507a9d
d839d60
8b2110a
d2e90f5
a73c71c
68696dd
39d8161
3bb0378
42633f7
bc97e2a
b3df323
f60c446
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -43,6 +43,9 @@ module FOUNDRY-CHEAT-CODES | |||||||||||
| imports EVM-ABI | ||||||||||||
| imports FOUNDRY-ACCOUNTS | ||||||||||||
| imports INFINITE-GAS | ||||||||||||
| imports ID | ||||||||||||
| imports BYTES | ||||||||||||
| imports STRING | ||||||||||||
|
|
||||||||||||
| configuration | ||||||||||||
| <cheatcodes> | ||||||||||||
|
|
@@ -92,6 +95,7 @@ module FOUNDRY-CHEAT-CODES | |||||||||||
| <mockFunctionValues> .Map </mockFunctionValues> | ||||||||||||
| </mockFunction> | ||||||||||||
| </mockFunctions> | ||||||||||||
| <envVars> .Map </envVars> | ||||||||||||
| </cheatcodes> | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
|
|
@@ -642,6 +646,68 @@ function toString(int256) external returns (string memory); | |||||||||||
| [preserves-definedness] | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ### `envOr(...)` cheatcodes | ||||||||||||
|
|
||||||||||||
| The `envOr` cheatcodes in Foundry are used to read environment variables with a fallback. | ||||||||||||
| In Kontrol, `envOr` is currently only implemented for concrete execution, and we always | ||||||||||||
| returns the default value. | ||||||||||||
|
Comment on lines
+674
to
+675
|
||||||||||||
| In Kontrol, `envOr` is currently only implemented for concrete execution, and we always | |
| returns the default value. | |
| In Kontrol, `envOr` is currently only implemented for concrete execution. Kontrol first | |
| looks up and parses the requested environment variable, returning its value when present | |
| and well-typed; otherwise, it falls back to the provided default value. |
Copilot
AI
Mar 25, 2026
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.
Typo in the docstring: it refers to the <enVars> mapping, but the configuration cell is <envVars>. This is easy to misread when implementing/maintaining related rules; please rename it consistently to <envVars> in the documentation text.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -332,6 +332,42 @@ def decode_log_message(token: str, selector: int) -> str | None: | |
| return None | ||
|
|
||
|
|
||
| def parse_env_file(file_path: Path) -> dict[str, str]: | ||
| """Parse a .env file into a dictionary of key-value pairs. | ||
|
|
||
| :param file_path: Path to the .env file | ||
| :return: Dictionary with environment variable names as keys and their values as strings | ||
| """ | ||
| env_vars: dict[str, str] = {} | ||
| if not file_path.exists(): | ||
| return env_vars | ||
| with open(file_path, encoding='utf-8') as f: | ||
| for line in f: | ||
|
lisandrasilva marked this conversation as resolved.
|
||
| line = line.strip() | ||
| if not line or line.startswith('#'): | ||
| continue | ||
| if '=' not in line: | ||
| continue | ||
| key, value = line.split('=', 1) | ||
|
lisandrasilva marked this conversation as resolved.
Outdated
|
||
| key = key.strip() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lisandrasilva @anvacaru I think it may be better to use a library for parsing env files, like https://github.com/theskumar/python-dotenv. |
||
| value = value.strip() | ||
|
lisandrasilva marked this conversation as resolved.
Outdated
|
||
| # Remove surrounding quotes if present | ||
| if (value.startswith('"') and value.endswith('"')) or (value.startswith("'") and value.endswith("'")): | ||
| value = value[1:-1] | ||
| env_vars[key] = value | ||
| return env_vars | ||
|
|
||
|
|
||
| def parse_foundry_env(foundry_root: Path) -> dict[str, str]: | ||
| """Parse the .env file located in the Foundry root directory. | ||
|
|
||
| :param foundry_root: Path to the Foundry project root directory | ||
| :return: Dictionary with environment variable names as keys and their values as strings | ||
| """ | ||
| env_file = foundry_root / '.env' | ||
| return parse_env_file(env_file) | ||
|
|
||
|
|
||
| EMPTY_LOG_SELECTOR = 1368866505 | ||
| # a mapping from function selectors to the argument types used in the log functions from | ||
| # https://github.com/foundry-rs/forge-std/blob/ee93fdc45d1e5e4dee883afe0103109881a83549/src/console.sol | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| UINT256=100 | ||
| BADUINT256=-100 | ||
| INT256=-100 | ||
| BADINT256=notanint | ||
| ADDRESS=0x1234567890123456789012345678901234567890 | ||
| ADDRESSINT=7584896468543216876435687541650546890874 | ||
| BADADDRESS=0xGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHSL | ||
| BYTES32=0x123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0 | ||
| BADBYTES32=0xbadbytes32_badbytes32_badbytes32_badbytes32_badbytes32_badbytes3 | ||
| BOOLFALSE=false | ||
| BADBOOLFALSE=notaboolvalue | ||
| BOOLTRUE=true | ||
| BADBOOLTRUE=notaboolvalue | ||
| BYTES=0xdeadbeef | ||
| STRING=hello_world | ||
| UINT256ARRAY=0,1,2,3,4 | ||
| INT256ARRAY=-2,-1,0,1,2 | ||
| ADDRESSARRAY=0x1234567890123456789012345678901234567890,0x2345678901234567890123456789012345678901,0x3456789012345678901234567890123456789012 | ||
| BYTES32ARRAY=0x123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0,0x15864864541fe8548484c8548654189d4184864a48618c54615156d165651f65,0x7468697264d18561e8691a891968f189868c18969d1868641f1818861685a189 | ||
| BOOLARRAY=true,false,true,false | ||
| STRINGARRAY=one,two |
Uh oh!
There was an error while loading. Please reload this page.