rust-lang / getopts

The getopts repo maintained by the rust-lang project
https://docs.rs/getopts
Apache License 2.0
233 stars 62 forks source link

test-threads option is unrecognised #113

Open emixa-d opened 2 years ago

emixa-d commented 2 years ago

When building rust-getopts with tests enabled on Guix, I got the following error message:

Running .guix-tests/smoke

running 1 test
test main ... FAILED

failures:

---- main stdout ----
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: UnrecognizedOption("test-threads")', tests/smoke.rs:7:48
stack backtrace:
   0:     0x7ffff7e4795b - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h190bc4e71a974fd5
   1:     0x7ffff7ea2cfc - core::fmt::write::h5ab597cdd0c00db2
   2:     0x7ffff7e00701 - <unknown>
   3:     0x7ffff7e23c6e - <unknown>
   4:     0x7ffff7e23952 - std::panicking::default_hook::hcffd5d909668cc52
   5:     0x7ffff7e242ae - std::panicking::rust_panic_with_hook::h67db5ff340e389bf
   6:     0x7ffff7e48277 - <unknown>
   7:     0x7ffff7e47a74 - <unknown>
   8:     0x7ffff7e23db2 - rust_begin_unwind
   9:     0x7ffff7df5ff3 - core::panicking::panic_fmt::h4b9cfd185dab4fab
  10:     0x7ffff7df6343 - core::result::unwrap_failed::h1d2e795ba7bc1e45
  11:     0x5555555588f4 - core::result::Result<T,E>::unwrap::hc3c37c10d473db0e
                               at /tmp/guix-build-rust-1.60.0.drv-0/rustc-1.60.0-src/library/core/src/result.rs:1065:23
  12:     0x5555555588f4 - smoke::main::hddc6ec79ca47762a
                               at /tmp/guix-build-rust-getopts-0.2.21.drv-0/getopts-0.2.21/tests/smoke.rs:7:5
  13:     0x5555555588f4 - smoke::main::{{closure}}::h14f4e8f31826c44b
                               at /tmp/guix-build-rust-getopts-0.2.21.drv-0/getopts-0.2.21/tests/smoke.rs:6:1
  14:     0x5555555588f4 - core::ops::function::FnOnce::call_once::hd8dc59dd743cb5eb
                               at /tmp/guix-build-rust-1.60.0.drv-0/rustc-1.60.0-src/library/core/src/ops/function.rs:227:5
  15:     0x7ffff7f68723 - <unknown>
  16:     0x7ffff7f688e4 - <unknown>
  17:     0x7ffff7f5b80d - <unknown>
  18:     0x7ffff7f7cd2f - <unknown>
  19:     0x7ffff7e1c233 - <unknown>
  20:     0x7ffff7bfe3aa - start_thread
  21:     0x7ffff7c7ef7c - clone3
  22:                0x0 - <unknown>

failures:
    main

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: in phase 'check': uncaught exception:
%exception #<&invoke-error program: ".guix-tests/smoke" arguments: ("--test-threads" "16") exit-status: 101 term-signal: #f stop-signal: #f> 
phase `check' failed after 0.0 seconds
command ".guix-tests/smoke" "--test-threads" "16" failed with status 101

Looks like --test-threads isn't supported yet.

correabuscar commented 3 months ago

To not fail there, the smoke test would have to be modified to accept all args that the test harness can accept[^1], or better yet, have its logic modified to not fail when any args are given to it, like the following (I'd make a PR but it's unclear if it was intended that argv[0] aka exe name be passed to parse() or not, even though in practice it should never be)

diff --git a/tests/smoke.rs b/tests/smoke.rs
index a46f9c0167ab307f..cf1dd1ca487db0e0 100644
--- a/tests/smoke.rs
+++ b/tests/smoke.rs
@@ -4,5 +4,9 @@ use std::env;

 #[test]
 fn main() {
-    getopts::Options::new().parse(env::args()).unwrap();
+    // The test harness can be called with extra args which would panic here,
+    // such as --test-threads=16 -q
+    // thus pretending that it was called with no args, as usual, won't panic,
+    // ergo keeping the program name aka argument at index 0, just as before:
+    getopts::Options::new().parse(env::args().nth(0)).unwrap();
 }
While you can modify it to accept `--test-threads` like for example: ```diff diff --git a/tests/smoke.rs b/tests/smoke.rs index a46f9c0167ab307f..b0545cb74e28fefd 100644 --- a/tests/smoke.rs +++ b/tests/smoke.rs @@ -1,8 +1,10 @@ extern crate getopts; use std::env; +use getopts::{Occur, HasArg}; + #[test] fn main() { - getopts::Options::new().parse(env::args()).unwrap(); + getopts::Options::new().opt("", "test-threads","allows this smoke test to pass while the test harness accepts this arg", "NUM", HasArg::Yes, Occur::Optional).parse(env::args()).unwrap(); } ``` it only works until you decide to use other args like, for example, `-q`: ``` $ cargo test --test smoke -- --test-threads=1 -q Finished `test` profile [unoptimized + debuginfo] target(s) in 0.05s Running tests/smoke.rs (target/debug/deps/smoke-a05f567bdd4b3df2) running 1 test main --- FAILED failures: ---- main stdout ---- thread 'main' panicked at /home/user/1tmp/getopts/tests/smoke.rs:9:182: called `Result::unwrap()` on an `Err` value: UnrecognizedOption("q") stack backtrace: 0: rust_begin_unwind at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:652:5 1: core::panicking::panic_fmt at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:72:14 2: core::result::unwrap_failed at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/result.rs:1654:5 3: core::result::Result::unwrap at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/result.rs:1077:23 4: smoke::main at ./tests/smoke.rs:9:5 5: smoke::main::{{closure}} at ./tests/smoke.rs:8:10 6: core::ops::function::FnOnce::call_once at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ops/function.rs:250:5 7: core::ops::function::FnOnce::call_once at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ops/function.rs:250:5 note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. failures: main test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.05s error: test failed, to rerun pass `--test smoke` ```

[^1]: which are those you can see via $ cargo test -- --help