Skip to content

Commit d5f53c7

Browse files
committed
Provide more useful messages when tests are given -h or --help
Progress on rust-lang#7824
1 parent 9db1903 commit d5f53c7

File tree

1 file changed

+52
-8
lines changed

1 file changed

+52
-8
lines changed

src/libextra/test.rs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818

1919
use getopts;
20+
use getopts::groups;
2021
use json::ToJson;
2122
use json;
2223
use serialize::Decodable;
@@ -28,6 +29,7 @@ use time::precise_time_ns;
2829
use treemap::TreeMap;
2930

3031
use std::comm::{stream, SharedChan};
32+
use std::libc;
3133
use std::either;
3234
use std::io;
3335
use std::result;
@@ -156,22 +158,64 @@ pub struct TestOpts {
156158

157159
type OptRes = Either<TestOpts, ~str>;
158160

161+
fn optgroups() -> ~[getopts::groups::OptGroup] {
162+
~[groups::optflag("", "ignored", "Run ignored tests"),
163+
groups::optflag("", "test", "Run tests and not benchmarks"),
164+
groups::optflag("", "bench", "Run benchmarks instead of tests"),
165+
groups::optflag("h", "help", "Display this message (longer with --help)"),
166+
groups::optopt("", "save-metrics", "Location to save bench metrics",
167+
"PATH"),
168+
groups::optopt("", "ratchet-metrics",
169+
"Location to load and save metrics from. The metrics \
170+
loaded are cause benchmarks to fail if they run too \
171+
slowly", "PATH"),
172+
groups::optopt("", "ratchet-noise-percent",
173+
"Tests within N% of the recorded metrics will be \
174+
considered as passing", "PERCENTAGE"),
175+
groups::optopt("", "logfile", "Write logs to the specified file instead \
176+
of stdout", "PATH")]
177+
}
178+
179+
fn usage(binary: &str, helpstr: &str) -> ! {
180+
let message = fmt!("Usage: %s [OPTIONS] [FILTER]", binary);
181+
println(groups::usage(message, optgroups()));
182+
if helpstr == "help" {
183+
println("\
184+
The FILTER is matched against the name of all tests to run, and if any tests
185+
have a substring match, only those tests are run.
186+
187+
By default, all tests are run in parallel. This can be altered with the
188+
RUST_THREADS environment variable when running tests (set it to 1).
189+
190+
Test Attributes:
191+
192+
#[test] - Indicates a function is a test to be run. This function
193+
takes no arguments.
194+
#[bench] - Indicates a function is a benchmark to be run. This
195+
function takes one argument (extra::test::BenchHarness).
196+
#[should_fail] - This function (also labeled with #[test]) will only pass if
197+
the code causes a failure (an assertion failure or fail!)
198+
#[ignore] - When applied to a function which is already attributed as a
199+
test, then the test runner will ignore these tests during
200+
normal test runs. Running with --ignored will run these
201+
tests. This may also be written as #[ignore(cfg(...))] to
202+
ignore the test on certain configurations.");
203+
}
204+
unsafe { libc::exit(0) }
205+
}
206+
159207
// Parses command line arguments into test options
160208
pub fn parse_opts(args: &[~str]) -> OptRes {
161209
let args_ = args.tail();
162-
let opts = ~[getopts::optflag("ignored"),
163-
getopts::optflag("test"),
164-
getopts::optflag("bench"),
165-
getopts::optopt("save-metrics"),
166-
getopts::optopt("ratchet-metrics"),
167-
getopts::optopt("ratchet-noise-percent"),
168-
getopts::optopt("logfile")];
169210
let matches =
170-
match getopts::getopts(args_, opts) {
211+
match groups::getopts(args_, optgroups()) {
171212
Ok(m) => m,
172213
Err(f) => return either::Right(getopts::fail_str(f))
173214
};
174215

216+
if getopts::opt_present(&matches, "h") { usage(args[0], "h"); }
217+
if getopts::opt_present(&matches, "help") { usage(args[0], "help"); }
218+
175219
let filter =
176220
if matches.free.len() > 0 {
177221
Some(copy (matches).free[0])

0 commit comments

Comments
 (0)