Skip to content

Commit babf741

Browse files
committed
auto merge of #7840 : alexcrichton/rust/better-test-help, r=huonw
Progress on #7824, closes #7825
2 parents 929b75e + d5f53c7 commit babf741

File tree

1 file changed

+52
-8
lines changed

1 file changed

+52
-8
lines changed

src/libextra/test.rs

+52-8
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;
@@ -29,6 +30,7 @@ use treemap::TreeMap;
2930

3031
use std::clone::Clone;
3132
use std::comm::{stream, SharedChan};
33+
use std::libc;
3234
use std::either;
3335
use std::io;
3436
use std::result;
@@ -169,22 +171,64 @@ pub struct TestOpts {
169171

170172
type OptRes = Either<TestOpts, ~str>;
171173

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

229+
if getopts::opt_present(&matches, "h") { usage(args[0], "h"); }
230+
if getopts::opt_present(&matches, "help") { usage(args[0], "help"); }
231+
188232
let filter =
189233
if matches.free.len() > 0 {
190234
Some((matches).free[0].clone())

0 commit comments

Comments
 (0)