Skip to content

Commit 29bed4f

Browse files
authored
feat(cheats): json/toml --> consolidate write and writeUpsert (#11447)
1 parent 6e6341b commit 29bed4f

File tree

7 files changed

+54
-96
lines changed

7 files changed

+54
-96
lines changed

crates/cheatcodes/assets/cheatcodes.json

Lines changed: 2 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cheatcodes/spec/src/vm.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,15 +2552,10 @@ interface Vm {
25522552

25532553
/// Write a serialized JSON object to an **existing** JSON file, replacing a value with key = <value_key.>
25542554
/// This is useful to replace a specific value of a JSON file, without having to parse the entire thing.
2555+
/// This cheatcode will create new keys if they didn't previously exist.
25552556
#[cheatcode(group = Json)]
25562557
function writeJson(string calldata json, string calldata path, string calldata valueKey) external;
25572558

2558-
/// Write a serialized JSON object to an **existing** JSON file, replacing a value with key = <value_key.>
2559-
/// This is useful to replace a specific value of a JSON file, without having to parse the entire thing.
2560-
/// Unlike `writeJson`, this cheatcode will create new keys if they didn't previously exist.
2561-
#[cheatcode(group = Toml)]
2562-
function writeJsonUpsert(string calldata json, string calldata path, string calldata valueKey) external;
2563-
25642559
// ======== TOML Parsing and Manipulation ========
25652560

25662561
// -------- Reading --------
@@ -2663,15 +2658,10 @@ interface Vm {
26632658

26642659
/// Takes serialized JSON, converts to TOML and write a serialized TOML table to an **existing** TOML file, replacing a value with key = <value_key.>
26652660
/// This is useful to replace a specific value of a TOML file, without having to parse the entire thing.
2661+
/// This cheatcode will create new keys if they didn't previously exist.
26662662
#[cheatcode(group = Toml)]
26672663
function writeToml(string calldata json, string calldata path, string calldata valueKey) external;
26682664

2669-
/// Takes serialized JSON, converts to TOML and write a serialized TOML table to an **existing** TOML file, replacing a value with key = <value_key.>
2670-
/// This is useful to replace a specific value of a TOML file, without having to parse the entire thing.
2671-
/// Unlike `writeToml`, this cheatcode will create new keys if they didn't previously exist.
2672-
#[cheatcode(group = Toml)]
2673-
function writeTomlUpsert(string calldata json, string calldata path, string calldata valueKey) external;
2674-
26752665
// ======== Cryptography ========
26762666

26772667
// -------- Key Management --------

crates/cheatcodes/src/json.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -344,24 +344,6 @@ impl Cheatcode for writeJson_0Call {
344344
}
345345

346346
impl Cheatcode for writeJson_1Call {
347-
fn apply(&self, state: &mut Cheatcodes) -> Result {
348-
let Self { json, path, valueKey } = self;
349-
let json = serde_json::from_str(json).unwrap_or_else(|_| Value::String(json.to_owned()));
350-
351-
let data_path = state.config.ensure_path_allowed(path, FsAccessKind::Read)?;
352-
let data_s = fs::read_to_string(data_path)?;
353-
let data = serde_json::from_str(&data_s)?;
354-
let value =
355-
jsonpath_lib::replace_with(data, &canonicalize_json_path(valueKey), &mut |_| {
356-
Some(json.clone())
357-
})?;
358-
359-
let json_string = serde_json::to_string_pretty(&value)?;
360-
super::fs::write_file(state, path.as_ref(), json_string.as_bytes())
361-
}
362-
}
363-
364-
impl Cheatcode for writeJsonUpsertCall {
365347
fn apply(&self, state: &mut Cheatcodes) -> Result {
366348
let Self { json: value, path, valueKey } = self;
367349

crates/cheatcodes/src/toml.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::{
44
Cheatcode, Cheatcodes, Result,
55
Vm::*,
66
json::{
7-
canonicalize_json_path, check_json_key_exists, parse_json, parse_json_coerce,
8-
parse_json_keys, resolve_type, upsert_json_value,
7+
check_json_key_exists, parse_json, parse_json_coerce, parse_json_keys, resolve_type,
8+
upsert_json_value,
99
},
1010
};
1111
use alloy_dyn_abi::DynSolType;
@@ -175,26 +175,6 @@ impl Cheatcode for writeToml_0Call {
175175
}
176176

177177
impl Cheatcode for writeToml_1Call {
178-
fn apply(&self, state: &mut Cheatcodes) -> Result {
179-
let Self { json, path, valueKey } = self;
180-
let json =
181-
serde_json::from_str(json).unwrap_or_else(|_| JsonValue::String(json.to_owned()));
182-
183-
let data_path = state.config.ensure_path_allowed(path, FsAccessKind::Read)?;
184-
let toml_data = fs::read_to_string(data_path)?;
185-
let json_data: JsonValue =
186-
toml::from_str(&toml_data).map_err(|e| fmt_err!("failed parsing TOML: {e}"))?;
187-
let value =
188-
jsonpath_lib::replace_with(json_data, &canonicalize_json_path(valueKey), &mut |_| {
189-
Some(json.clone())
190-
})?;
191-
192-
let toml_string = format_json_to_toml(value)?;
193-
super::fs::write_file(state, path.as_ref(), toml_string.as_bytes())
194-
}
195-
}
196-
197-
impl Cheatcode for writeTomlUpsertCall {
198178
fn apply(&self, state: &mut Cheatcodes) -> Result {
199179
let Self { json: value, path, valueKey } = self;
200180

testdata/cheats/Vm.sol

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/default/cheats/Json.t.sol

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,4 +491,28 @@ contract WriteJsonTest is DSTest {
491491
address decodedAddress = abi.decode(data, (address));
492492
assertEq(decodedAddress, ex);
493493
}
494+
495+
function test_writeJson_createKeys() public {
496+
string memory path = "fixtures/Json/write_test.json";
497+
string memory json = vm.readFile(path);
498+
499+
bool exists = vm.keyExistsJson(json, ".parent");
500+
assertTrue(!exists);
501+
exists = vm.keyExistsJson(json, ".parent.child");
502+
assertTrue(!exists);
503+
exists = vm.keyExistsJson(json, ".parent.child.value");
504+
assertTrue(!exists);
505+
506+
// Write to nested path, creating intermediate keys
507+
vm.writeJson(vm.toString(uint256(42)), path, ".parent.child.value");
508+
509+
// Verify the value was written and intermediate keys were created
510+
json = vm.readFile(path);
511+
uint256 value = abi.decode(vm.parseJson(json, ".parent.child.value"), (uint256));
512+
assertEq(value, 42);
513+
514+
// Clean up the test file by removing the parent key we added
515+
vm.removeFile(path);
516+
vm.writeJson("{\"a\": 123, \"b\": \"0x000000000000000000000000000000000000bEEF\"}", path);
517+
}
494518
}

testdata/default/cheats/Toml.t.sol

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,28 @@ contract WriteTomlTest is DSTest {
421421
address decodedAddress = abi.decode(data, (address));
422422
assertEq(decodedAddress, ex);
423423
}
424+
425+
function test_writeToml_createKeys() public {
426+
string memory path = "fixtures/Toml/write_test.toml";
427+
string memory toml = vm.readFile(path);
428+
429+
bool exists = vm.keyExistsToml(toml, ".parent");
430+
assertTrue(!exists);
431+
exists = vm.keyExistsToml(toml, ".parent.child");
432+
assertTrue(!exists);
433+
exists = vm.keyExistsToml(toml, ".parent.child.value");
434+
assertTrue(!exists);
435+
436+
// Write to nested path, creating intermediate keys
437+
vm.writeToml(vm.toString(uint256(42)), path, ".parent.child.value");
438+
439+
// Verify the value was written and intermediate keys were created
440+
toml = vm.readFile(path);
441+
uint256 value = abi.decode(vm.parseToml(toml, ".parent.child.value"), (uint256));
442+
assertEq(value, 42);
443+
444+
// Clean up the test file by removing the parent key we added
445+
vm.removeFile(path);
446+
vm.writeToml("{\"a\": 123, \"b\": \"0x000000000000000000000000000000000000bEEF\"}", path);
447+
}
424448
}

0 commit comments

Comments
 (0)