Skip to content

Commit 788fcf0

Browse files
authored
Fix fork caches on windows (#3113)
<!-- Reference any GitHub issues resolved by this PR --> Closes #2303 ## Introduced changes <!-- A brief description of the changes --> - Fixed fork caches on windows. - Restored forking related tests on windows ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md`
1 parent 1953628 commit 788fcf0

File tree

6 files changed

+23
-10
lines changed

6 files changed

+23
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- gas is now reported using resource bounds triplet (l1_gas, l1_data_gas and l2_gas)
2121
- `available_gas` now accepts named arguments denoting resource bounds (eg #[available_gas(l1_gas: 1, l1_data_gas: 2, l2_gas: 3)])
2222

23+
#### Fixed
24+
25+
- Bug with file locking that prevented forking from working on Windows
26+
2327
### Cast
2428

2529
#### Added

crates/cheatnet/src/forking/cache.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use starknet_api::state::StorageKey;
1111
use starknet_types_core::felt::Felt;
1212
use std::collections::HashMap;
1313
use std::fs;
14-
use std::fs::OpenOptions;
15-
use std::io::{Read, Write};
14+
use std::fs::{File, OpenOptions};
15+
use std::io::{Read, Seek, Write};
1616
use std::string::ToString;
1717
use url::Url;
1818

@@ -99,6 +99,18 @@ impl Drop for ForkCache {
9999
}
100100
}
101101

102+
trait FileTruncateExtension {
103+
fn clear(&mut self) -> Result<()>;
104+
}
105+
106+
impl FileTruncateExtension for File {
107+
fn clear(&mut self) -> Result<()> {
108+
self.set_len(0).context("Failed to truncate file")?;
109+
self.rewind().context("Failed to rewind file")?;
110+
Ok(())
111+
}
112+
}
113+
102114
impl ForkCache {
103115
pub(crate) fn load_or_new(
104116
url: &Url,
@@ -134,15 +146,17 @@ impl ForkCache {
134146
fn save(&self) {
135147
let mut file = OpenOptions::new()
136148
.write(true)
149+
.read(true)
137150
.create(true)
138151
.truncate(false)
139152
.open(&self.cache_file)
140153
.unwrap();
141154

142155
file.lock_exclusive().expect("Could not lock on cache file");
143156

144-
let cache_file_content =
145-
fs::read_to_string(&self.cache_file).expect("Should have been able to read the cache");
157+
let mut cache_file_content = String::new();
158+
file.read_to_string(&mut cache_file_content)
159+
.expect("Should have been able to read the cache");
146160

147161
let output = if cache_file_content.is_empty() {
148162
self.fork_cache_content.to_string()
@@ -152,6 +166,7 @@ impl ForkCache {
152166
fs_fork_cache_content.to_string()
153167
};
154168

169+
file.clear().expect("Failed to clear file");
155170
file.write_all(output.as_bytes())
156171
.expect("Could not write cache to file");
157172

crates/forge/tests/e2e/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ mod debugging;
1717
mod docs_snippets_validation;
1818
mod env;
1919
mod features;
20-
#[cfg(not(target_os = "windows"))]
2120
mod fork_warning;
22-
#[cfg(not(target_os = "windows"))]
2321
mod forking;
2422
mod fuzzing;
2523
mod io_operations;

crates/forge/tests/integration/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ mod cheat_block_number;
55
mod cheat_block_timestamp;
66
mod cheat_caller_address;
77
mod cheat_execution_info;
8-
#[cfg(not(target_os = "windows"))]
98
mod cheat_fork;
109
mod cheat_sequencer_address;
1110
mod declare;
@@ -26,7 +25,6 @@ mod pure_cairo;
2625
mod replace_bytecode;
2726
mod resources;
2827
mod runtime;
29-
#[cfg(not(target_os = "windows"))]
3028
mod setup_fork;
3129
mod should_panic;
3230
mod signing;

crates/forge/tests/integration/spy_events.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,6 @@ fn assert_not_emitted_fails() {
632632
assert_case_output_contains(&result, "assert_not_emitted_fails", "keys was emitted");
633633
}
634634

635-
#[cfg(not(target_os = "windows"))]
636635
#[test]
637636
fn capture_cairo0_event() {
638637
let test = test_case!(

crates/forge/tests/integration/store_load.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,6 @@ fn store_load_felt_to_felt() {
561561
assert_passed(&result);
562562
}
563563

564-
#[cfg(not(target_os = "windows"))]
565564
#[test]
566565
fn fork_store_load() {
567566
let test = test_utils::test_case!(formatdoc!(

0 commit comments

Comments
 (0)