rust-analyzer / expect-test

Minimalistic snapshot testing for Rust.
Apache License 2.0
248 stars 22 forks source link

"No such file or directory" when updating #35

Closed zjp-CN closed 2 years ago

zjp-CN commented 2 years ago

The source file is here:

RUST_BACKTRACE=1 UPDATE_EXPECT=1 cargo r

updating: testing.rs:10:5
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', /root/.cargo/registry
/src/rsproxy.cn-8f6827c7555bfaf8/expect-test-1.4.0/src/lib.rs:539:55
stack backtrace:
   0: rust_begin_unwind
             at /rustc/0b79f758c9aa6646606662a6d623a0752286cd17/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/0b79f758c9aa6646606662a6d623a0752286cd17/library/core/src/panicking.rs:142:14
   2: core::result::unwrap_failed
             at /rustc/0b79f758c9aa6646606662a6d623a0752286cd17/library/core/src/result.rs:1814:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/0b79f758c9aa6646606662a6d623a0752286cd17/library/core/src/result.rs:1107:23
   4: expect_test::FileRuntime::new
             at /root/.cargo/registry/src/rsproxy.cn-8f6827c7555bfaf8/expect-test-1.4.0/src/lib.rs:539:29
   5: expect_test::Runtime::fail_expect::{{closure}}
             at /root/.cargo/registry/src/rsproxy.cn-8f6827c7555bfaf8/expect-test-1.4.0/src/lib.rs:478:36
   6: std::collections::hash::map::Entry<K,V>::or_insert_with
             at /rustc/0b79f758c9aa6646606662a6d623a0752286cd17/library/std/src/collections/hash/map.rs:2549:43
   7: expect_test::Runtime::fail_expect
             at /root/.cargo/registry/src/rsproxy.cn-8f6827c7555bfaf8/expect-test-1.4.0/src/lib.rs:476:13
   8: expect_test::Expect::assert_eq
             at /root/.cargo/registry/src/rsproxy.cn-8f6827c7555bfaf8/expect-test-1.4.0/src/lib.rs:285:9
   9: testing::main
             at ./testing.rs:10:5
  10: core::ops::function::FnOnce::call_once
             at /rustc/0b79f758c9aa6646606662a6d623a0752286cd17/library/core/src/ops/function.rs:248:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

When I create a new cargo bin project, expect-test can update the result successfully. But for this it can't.

I tried to debug it, and I got:

(gdb) s
alloc::string::{impl#37}::deref (self=0x7fffffffdfa8) at /rustc/0b79f758c9aa6646606662a6d623a0752286cd17/library/alloc/src/string.rs:2413
2413    /rustc/0b79f758c9aa6646606662a6d623a0752286cd17/library/alloc/src/string.rs: No such file or directory.
bjorn3 commented 2 years ago

I believe you are meant to use expect! inside tests, not the main executable. cargo run doesn't set an env var expect! needs, only cargo test does.

zjp-CN commented 2 years ago

@bjorn3 Probably not I guess. I put expect! in a simple main.rs generated by cargo new, and it can automatically update the result with env set up. And I turn the main function that I link into a test function, the same error pops up.

bjorn3 commented 2 years ago

The error happens at https://github.com/rust-analyzer/expect-test/blob/9d2aa5d01b485c432b0b16c6ea6a540fcc04b5c2/src/lib.rs#L539, which could fail if to_abs_ws_path returns an incorrect path.

zjp-CN commented 2 years ago

Thank you for pointing that out! I should have run the command with RUST_BACKTRACE=full :)

I've read to_abs_ws_path and used

[env]
CARGO_WORKSPACE_DIR = { value = "", relative = true }

in .cargo/config.toml, and expect! goes well.