Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests fail due to missing CARGO_PKG_NAME when running without cargo #14

Closed
konstin opened this issue Jun 5, 2019 · 3 comments · Fixed by #176
Closed

Tests fail due to missing CARGO_PKG_NAME when running without cargo #14

konstin opened this issue Jun 5, 2019 · 3 comments · Fixed by #176

Comments

@konstin
Copy link

konstin commented Jun 5, 2019

When running test binaries directly, trybuild tests fail because CARGO_PKG_NAME is missing. This is e.g. the case when collecting coverage data (example travis build: https://travis-ci.org/PyO3/pyo3/jobs/541071250).

Line that requires CARGO_PKG_NAME:

let crate_name = env::var("CARGO_PKG_NAME").map_err(Error::PkgName)?;

Full backtrace
$ RUST_BACKTRACE=1 target/debug/deps/test_compile_error-8cb93379bda46ad2

running 1 test
ERROR: failed to detect CARGO_PKG_NAME: environment variable not found

test test_compile_errors ... FAILED

failures:

---- test_compile_errors stdout ----
thread 'test_compile_errors' panicked at 'tests failed', /home/konsti/.cargo/registry/src/github.com-1ecc6299db9ec823/trybuild-1.0.4/src/run.rs:37:13
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.25/src/backtrace/libunwind.rs:97
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.25/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:47
   3: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:36
   4: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:197
   5: std::panicking::default_hook
             at src/libstd/panicking.rs:208
   6: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:474
   7: std::panicking::begin_panic
             at /rustc/5d8f59f4b1473217c2de7e02330b5aaae70a1668/src/libstd/panicking.rs:408
   8: trybuild::run::<impl trybuild::Runner>::run::{{closure}}
             at /home/konsti/.cargo/registry/src/github.com-1ecc6299db9ec823/trybuild-1.0.4/src/run.rs:37
   9: core::result::Result<T,E>::unwrap_or_else
             at /rustc/5d8f59f4b1473217c2de7e02330b5aaae70a1668/src/libcore/result.rs:766
  10: trybuild::run::<impl trybuild::Runner>::run
             at /home/konsti/.cargo/registry/src/github.com-1ecc6299db9ec823/trybuild-1.0.4/src/run.rs:35
  11: <trybuild::TestCases as core::ops::drop::Drop>::drop
             at /home/konsti/.cargo/registry/src/github.com-1ecc6299db9ec823/trybuild-1.0.4/src/lib.rs:272
  12: core::ptr::real_drop_in_place
             at /rustc/5d8f59f4b1473217c2de7e02330b5aaae70a1668/src/libcore/ptr/mod.rs:197
  13: test_compile_error::test_compile_errors
             at tests/test_compile_error.rs:5
  14: test_compile_error::test_compile_errors::{{closure}}
             at tests/test_compile_error.rs:2
  15: core::ops::function::FnOnce::call_once
             at /rustc/5d8f59f4b1473217c2de7e02330b5aaae70a1668/src/libcore/ops/function.rs:231
  16: <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once
             at /rustc/5d8f59f4b1473217c2de7e02330b5aaae70a1668/src/liballoc/boxed.rs:746
  17: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:85
  18: std::panicking::try
             at /rustc/5d8f59f4b1473217c2de7e02330b5aaae70a1668/src/libstd/panicking.rs:272
  19: std::panic::catch_unwind
             at /rustc/5d8f59f4b1473217c2de7e02330b5aaae70a1668/src/libstd/panic.rs:388
  20: test::run_test::run_test_inner::{{closure}}
             at src/libtest/lib.rs:1466
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.


failures:
    test_compile_errors

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
@dtolnay
Copy link
Owner

dtolnay commented Jun 5, 2019

Thanks, I would accept a PR to support non-Cargo test runs as long as it is not too disruptive to the existing implementation. Really I think the coverage runner should define these env vars.

@CreepySkeleton
Copy link

CreepySkeleton commented Aug 16, 2019

Can't we just name such crates as "<unnamed crate>" and be done with it? I mean

let crate_name = env::var("CARGO_PKG_NAME").unwrap_or("<nameless crate>".into());

@taqtiqa-mark
Copy link

taqtiqa-mark commented Apr 6, 2022

Another use case is when debugging an integration test. This could be addressed with a documentation note to set the environment variable? To... anything?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants