rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.73k stars 2.42k forks source link

BUG: The output results of cargo test and cargo run seem to be different #14345

Open yymm120 opened 2 months ago

yymm120 commented 2 months ago

bug description

I placed the configuration folder in the root path and when I cargo run everything works fine. But cargo test keeps failing. It wants me to put the configuration folder inside src-axum.

Then I found that cargo test and cargo run output different results

cargo run

image image

cargo test

image image

environment

in workspace image

os: ubuntu !❯ rustup --version rustup 1.27.1 (54dd3d00f 2024-04-24) info: This is the version for the rustup toolchain manager, not the rustc compiler. info: The currently active rustc version is `rustc 1.80.0 (051478957 2024-07-21)

yymm120 commented 2 months ago

image

In this case I had to place two configuration folders to make everything look normal. After this error, I'm not sure whether cargo test is trustworthy anymore. Should I always cargo run

fmease commented 2 months ago

@rustbot label -needs-triage @rustbot transfer cargo

epage commented 2 months ago

As the reproduction steps involve third-party commands that I'm not as familiar with and is focusing on inferring the problem from screenshots rather than explicitly calling things out, this is going to be a guess as to what is going on .

It looks like the concern is with cargo run running the binary in the current working directory while cargo test is running the test harness in the package root.

This is expected behavior. cargo run is trying to act as if you ran the binary directly. cargo test needs a predictable location to run the test for accessing fixtures, independent of what your current working directory is.

epage commented 2 months ago

I'm not too sure how config fits into this. In general, config is environmental configuration and is dependent on the current working directory that cargo was run in.

weihanglo commented 2 months ago

@rustbot transfer cargo

TIL 😬

This is expected behavior. cargo run is trying to act as if you ran the binary directly. cargo test needs a predictable location to run the test for accessing fixtures, independent of what your current working directory is.

For the reference, see the cargo-test doc:

The working directory when running each unit and integration test is set to the root directory of the package the test belongs to. Setting the working directory of tests to the package’s root directory makes it possible for tests to reliably access the package’s files using relative paths, regardless from where cargo test was executed from.