@@ -4245,3 +4245,140 @@ fn bin_env_for_test() {
4245
4245
p. cargo ( "test --test check_env" ) . run ( ) ;
4246
4246
p. cargo ( "check --test check_env" ) . run ( ) ;
4247
4247
}
4248
+
4249
+ #[ cargo_test]
4250
+ fn test_workspaces_cwd ( ) {
4251
+ // This tests that all the different test types are executed from the
4252
+ // crate directory (manifest_dir), and not from the workspace root.
4253
+
4254
+ let make_lib_file = |expected| {
4255
+ format ! (
4256
+ r#"
4257
+ //! ```
4258
+ //! assert_eq!("{expected}", std::fs::read_to_string("file.txt").unwrap());
4259
+ //! assert_eq!(
4260
+ //! std::path::PathBuf::from(std::env!("CARGO_MANIFEST_DIR")),
4261
+ //! std::env::current_dir().unwrap(),
4262
+ //! );
4263
+ //! ```
4264
+
4265
+ #[test]
4266
+ fn test_unit_{expected}_cwd() {{
4267
+ assert_eq!("{expected}", std::fs::read_to_string("file.txt").unwrap());
4268
+ assert_eq!(
4269
+ std::path::PathBuf::from(std::env!("CARGO_MANIFEST_DIR")),
4270
+ std::env::current_dir().unwrap(),
4271
+ );
4272
+ }}
4273
+ "# ,
4274
+ expected = expected
4275
+ )
4276
+ } ;
4277
+ let make_test_file = |expected| {
4278
+ format ! (
4279
+ r#"
4280
+ #[test]
4281
+ fn test_integration_{expected}_cwd() {{
4282
+ assert_eq!("{expected}", std::fs::read_to_string("file.txt").unwrap());
4283
+ assert_eq!(
4284
+ std::path::PathBuf::from(std::env!("CARGO_MANIFEST_DIR")),
4285
+ std::env::current_dir().unwrap(),
4286
+ );
4287
+ }}
4288
+ "# ,
4289
+ expected = expected
4290
+ )
4291
+ } ;
4292
+
4293
+ let p = project ( )
4294
+ . file (
4295
+ "Cargo.toml" ,
4296
+ r#"
4297
+ [package]
4298
+ name = "root-crate"
4299
+ version = "0.0.0"
4300
+
4301
+ [workspace]
4302
+ members = [".", "nested-crate", "very/deeply/nested/deep-crate"]
4303
+ "# ,
4304
+ )
4305
+ . file ( "file.txt" , "root" )
4306
+ . file ( "src/lib.rs" , & make_lib_file ( "root" ) )
4307
+ . file ( "tests/integration.rs" , & make_test_file ( "root" ) )
4308
+ . file (
4309
+ "nested-crate/Cargo.toml" ,
4310
+ r#"
4311
+ [package]
4312
+ name = "nested-crate"
4313
+ version = "0.0.0"
4314
+ "# ,
4315
+ )
4316
+ . file ( "nested-crate/file.txt" , "nested" )
4317
+ . file ( "nested-crate/src/lib.rs" , & make_lib_file ( "nested" ) )
4318
+ . file (
4319
+ "nested-crate/tests/integration.rs" ,
4320
+ & make_test_file ( "nested" ) ,
4321
+ )
4322
+ . file (
4323
+ "very/deeply/nested/deep-crate/Cargo.toml" ,
4324
+ r#"
4325
+ [package]
4326
+ name = "deep-crate"
4327
+ version = "0.0.0"
4328
+ "# ,
4329
+ )
4330
+ . file ( "very/deeply/nested/deep-crate/file.txt" , "deep" )
4331
+ . file (
4332
+ "very/deeply/nested/deep-crate/src/lib.rs" ,
4333
+ & make_lib_file ( "deep" ) ,
4334
+ )
4335
+ . file (
4336
+ "very/deeply/nested/deep-crate/tests/integration.rs" ,
4337
+ & make_test_file ( "deep" ) ,
4338
+ )
4339
+ . build ( ) ;
4340
+
4341
+ p. cargo ( "test --workspace --all" )
4342
+ . with_stderr_contains ( "[DOCTEST] root-crate" )
4343
+ . with_stderr_contains ( "[DOCTEST] nested-crate" )
4344
+ . with_stderr_contains ( "[DOCTEST] deep-crate" )
4345
+ . with_stdout_contains ( "test test_unit_root_cwd ... ok" )
4346
+ . with_stdout_contains ( "test test_unit_nested_cwd ... ok" )
4347
+ . with_stdout_contains ( "test test_unit_deep_cwd ... ok" )
4348
+ . with_stdout_contains ( "test test_integration_root_cwd ... ok" )
4349
+ . with_stdout_contains ( "test test_integration_nested_cwd ... ok" )
4350
+ . with_stdout_contains ( "test test_integration_deep_cwd ... ok" )
4351
+ . run ( ) ;
4352
+
4353
+ p. cargo ( "test -p root-crate --all" )
4354
+ . with_stderr_contains ( "[DOCTEST] root-crate" )
4355
+ . with_stdout_contains ( "test test_unit_root_cwd ... ok" )
4356
+ . with_stdout_contains ( "test test_integration_root_cwd ... ok" )
4357
+ . run ( ) ;
4358
+
4359
+ p. cargo ( "test -p nested-crate --all" )
4360
+ . with_stderr_contains ( "[DOCTEST] nested-crate" )
4361
+ . with_stdout_contains ( "test test_unit_nested_cwd ... ok" )
4362
+ . with_stdout_contains ( "test test_integration_nested_cwd ... ok" )
4363
+ . run ( ) ;
4364
+
4365
+ p. cargo ( "test -p deep-crate --all" )
4366
+ . with_stderr_contains ( "[DOCTEST] deep-crate" )
4367
+ . with_stdout_contains ( "test test_unit_deep_cwd ... ok" )
4368
+ . with_stdout_contains ( "test test_integration_deep_cwd ... ok" )
4369
+ . run ( ) ;
4370
+
4371
+ p. cargo ( "test --all" )
4372
+ . cwd ( "nested-crate" )
4373
+ . with_stderr_contains ( "[DOCTEST] nested-crate" )
4374
+ . with_stdout_contains ( "test test_unit_nested_cwd ... ok" )
4375
+ . with_stdout_contains ( "test test_integration_nested_cwd ... ok" )
4376
+ . run ( ) ;
4377
+
4378
+ p. cargo ( "test --all" )
4379
+ . cwd ( "very/deeply/nested/deep-crate" )
4380
+ . with_stderr_contains ( "[DOCTEST] deep-crate" )
4381
+ . with_stdout_contains ( "test test_unit_deep_cwd ... ok" )
4382
+ . with_stdout_contains ( "test test_integration_deep_cwd ... ok" )
4383
+ . run ( ) ;
4384
+ }
0 commit comments