@@ -123,7 +123,12 @@ pub(crate) fn post_create(path: &Path) -> Result<()> {
123
123
Ok ( v) => Some ( v) ,
124
124
// Only 1 error means that CWD isn't a cargo project.
125
125
Err ( cargo_metadata:: Error :: CargoMetadata { .. } ) => None ,
126
- Err ( err) => return Err ( Error :: CargoMetadata ( err) ) ,
126
+ Err ( err) => {
127
+ return Err ( Error :: Other ( anyhow:: anyhow!(
128
+ "Couldn't retrieve cargo metadata: {:?}" ,
129
+ err
130
+ ) ) ) ;
131
+ }
127
132
}
128
133
} ;
129
134
@@ -199,21 +204,31 @@ fn remove_triple_newlines(string: &str) -> String {
199
204
200
205
#[ cfg( test) ]
201
206
pub ( crate ) mod tests {
202
- use assert_cmd :: Command ;
207
+ use escargot :: { CargoBuild , CargoRun } ;
203
208
use std:: fs:: { create_dir_all, read_to_string} ;
204
209
use std:: path:: { Path , PathBuf } ;
210
+ use std:: process:: Command ;
211
+ use std:: sync:: LazyLock ;
205
212
use tempfile:: tempdir;
206
213
use toml:: Value ;
207
214
215
+ static BINARY : LazyLock < CargoRun > = LazyLock :: new ( || {
216
+ CargoBuild :: new ( )
217
+ . bin ( env ! ( "CARGO_BIN_NAME" ) )
218
+ . current_release ( )
219
+ . run ( )
220
+ . expect ( "Couldn't build the binary for tests." )
221
+ } ) ;
222
+
208
223
// Note: tests below (at least 6 of them) were written to mainly test
209
224
// correctness of project's directory and its name, because previously it
210
225
// was broken and tests bring a peace of mind. And also so that I don't have
211
226
// to run my local hand-made tests every time.
212
227
213
228
pub ( crate ) type Result < T > = std:: result:: Result < T , Box < dyn std:: error:: Error > > ;
214
229
215
- pub ( crate ) fn subcommand ( name : & str ) -> Result < Command > {
216
- let mut command = Command :: cargo_bin ( env ! ( "CARGO_BIN_NAME" ) ) ? ;
230
+ pub ( crate ) fn subcommand ( name : & str ) -> Command {
231
+ let mut command = BINARY . command ( ) ;
217
232
command
218
233
. arg ( name)
219
234
. arg ( "--yes" ) // Skip any questions by choosing default answers.
@@ -222,7 +237,7 @@ pub(crate) mod tests {
222
237
// either `--subtemplate` or `--option`.
223
238
// Maybe a simple template in tests/ dir?
224
239
. arg ( "Fullstack" ) ;
225
- Ok ( command)
240
+ command
226
241
}
227
242
228
243
pub ( crate ) fn get_cargo_toml_path ( project_path : & Path ) -> PathBuf {
@@ -240,7 +255,7 @@ pub(crate) mod tests {
240
255
. to_string ( ) )
241
256
}
242
257
243
- fn subcommand_new ( ) -> Result < Command > {
258
+ fn subcommand_new ( ) -> Command {
244
259
subcommand ( "new" )
245
260
}
246
261
@@ -256,11 +271,11 @@ pub(crate) mod tests {
256
271
let project_path = & current_dir;
257
272
assert ! ( project_path. exists( ) ) ;
258
273
259
- subcommand_new ( ) ?
274
+ assert ! ( subcommand_new( )
260
275
. arg( "." )
261
276
. current_dir( & current_dir)
262
- . assert ( )
263
- . success ( ) ;
277
+ . status ( )
278
+ . is_ok ( ) ) ;
264
279
265
280
let cargo_toml_path = get_cargo_toml_path ( project_path) ;
266
281
assert ! ( cargo_toml_path. exists( ) ) ;
@@ -275,11 +290,11 @@ pub(crate) mod tests {
275
290
276
291
let current_dir = tempdir ( ) ?;
277
292
278
- subcommand_new ( ) ?
293
+ assert ! ( subcommand_new( )
279
294
. arg( project_dir)
280
295
. current_dir( & current_dir)
281
- . assert ( )
282
- . success ( ) ;
296
+ . status ( )
297
+ . is_ok ( ) ) ;
283
298
284
299
let project_path = current_dir. path ( ) . join ( project_dir) ;
285
300
let cargo_toml_path = get_cargo_toml_path ( & project_path) ;
@@ -296,11 +311,11 @@ pub(crate) mod tests {
296
311
297
312
let current_dir = tempdir ( ) ?;
298
313
299
- subcommand_new ( ) ?
314
+ assert ! ( subcommand_new( )
300
315
. arg( project_dir)
301
316
. current_dir( & current_dir)
302
- . assert ( )
303
- . success ( ) ;
317
+ . status ( )
318
+ . is_ok ( ) ) ;
304
319
305
320
let project_path = current_dir. path ( ) . join ( project_dir) ;
306
321
let cargo_toml_path = get_cargo_toml_path ( & project_path) ;
@@ -322,13 +337,13 @@ pub(crate) mod tests {
322
337
let project_path = & current_dir;
323
338
assert ! ( project_path. exists( ) ) ;
324
339
325
- subcommand_new ( ) ?
340
+ assert ! ( subcommand_new( )
326
341
. arg( "--name" )
327
342
. arg( project_name)
328
343
. arg( "." )
329
344
. current_dir( & current_dir)
330
- . assert ( )
331
- . success ( ) ;
345
+ . status ( )
346
+ . is_ok ( ) ) ;
332
347
333
348
let cargo_toml_path = get_cargo_toml_path ( project_path) ;
334
349
assert ! ( cargo_toml_path. exists( ) ) ;
@@ -343,13 +358,13 @@ pub(crate) mod tests {
343
358
344
359
let current_dir = tempdir ( ) ?;
345
360
346
- subcommand_new ( ) ?
361
+ assert ! ( subcommand_new( )
347
362
. arg( project_dir)
348
363
. arg( "--name" )
349
364
. arg( project_name)
350
365
. current_dir( & current_dir)
351
- . assert ( )
352
- . success ( ) ;
366
+ . status ( )
367
+ . is_ok ( ) ) ;
353
368
354
369
let project_path = current_dir. path ( ) . join ( project_dir) ;
355
370
let cargo_toml_path = get_cargo_toml_path ( & project_path) ;
@@ -366,13 +381,13 @@ pub(crate) mod tests {
366
381
367
382
let current_dir = tempdir ( ) ?;
368
383
369
- subcommand_new ( ) ?
384
+ assert ! ( subcommand_new( )
370
385
. arg( project_dir)
371
386
. arg( "--name" )
372
387
. arg( project_name)
373
388
. current_dir( & current_dir)
374
- . assert ( )
375
- . success ( ) ;
389
+ . status ( )
390
+ . is_ok ( ) ) ;
376
391
377
392
let project_path = current_dir. path ( ) . join ( project_dir) ;
378
393
let cargo_toml_path = get_cargo_toml_path ( & project_path) ;
0 commit comments