1
- use std:: { env, fs:: OpenOptions , io:: Write , path:: Path , process:: Command } ;
1
+ use std:: {
2
+ env,
3
+ fs:: { self , read_to_string} ,
4
+ path:: Path ,
5
+ process:: Command ,
6
+ } ;
2
7
3
8
use eyre:: Result ;
9
+ use itertools:: Itertools ;
4
10
use tempfile:: tempdir;
5
11
6
12
#[ test]
@@ -124,6 +130,7 @@ fn test_cli_init_build() -> Result<()> {
124
130
let manifest_path = temp_path. join ( "Cargo.toml" ) ;
125
131
run_cmd ( "cargo" , & [ "install" , "--path" , "." , "--force" ] ) ?;
126
132
133
+ // Cargo will not respect patches if run within a workspace
127
134
run_cmd (
128
135
"cargo" ,
129
136
& [
@@ -135,7 +142,7 @@ fn test_cli_init_build() -> Result<()> {
135
142
] ,
136
143
) ?;
137
144
if matches ! ( env:: var( "USE_LOCAL_OPENVM" ) , Ok ( x) if x == "1" ) {
138
- append_patch_to_cargo_toml ( & manifest_path) ?;
145
+ replace_with_local_openvm ( & manifest_path) ?;
139
146
}
140
147
141
148
run_cmd (
@@ -175,25 +182,30 @@ fn run_cmd(program: &str, args: &[&str]) -> Result<()> {
175
182
Ok ( ( ) )
176
183
}
177
184
178
- fn append_patch_to_cargo_toml ( file_path : impl AsRef < Path > ) -> Result < ( ) > {
185
+ fn replace_with_local_openvm ( file_path : impl AsRef < Path > ) -> Result < ( ) > {
179
186
const MANIFEST_DIR : & str = env ! ( "CARGO_MANIFEST_DIR" ) ;
180
187
let openvm_path = Path :: new ( MANIFEST_DIR )
181
188
. parent ( )
182
189
. unwrap ( )
183
190
. join ( "toolchain" )
184
191
. join ( "openvm" ) ;
185
- let mut file = OpenOptions :: new ( )
186
- . create ( false )
187
- . append ( true )
188
- . open ( file_path) ?;
189
-
190
- // Add a newline first to ensure proper formatting
191
- writeln ! ( file) ?;
192
- writeln ! (
193
- file,
194
- r#"[patch."https://github.com/openvm-org/openvm.git"]"#
195
- ) ?;
196
- writeln ! ( file, r#"openvm = {{ path = "{}" }}"# , openvm_path. display( ) ) ?;
192
+ let content = read_to_string ( & file_path) ?;
193
+ let lines = content. lines ( ) . collect :: < Vec < _ > > ( ) ;
194
+ let new_content = lines
195
+ . iter ( )
196
+ . map ( |line| {
197
+ if line. starts_with ( "openvm = { git = \" https://github.com/openvm-org/openvm.git\" " ) {
198
+ format ! (
199
+ r#"openvm = {{ path = "{}", features = ["std"] }}"# ,
200
+ openvm_path. display( )
201
+ )
202
+ } else {
203
+ line. to_string ( )
204
+ }
205
+ } )
206
+ . join ( "\n " ) ;
207
+
208
+ fs:: write ( file_path, new_content) ?;
197
209
198
210
Ok ( ( ) )
199
211
}
0 commit comments