Skip to content

Commit ba5f530

Browse files
committed
auto merge of #13948 : huonw/rust/test-regex-filter, r=alexcrichton
This allows writing a regex to filter tests more precisely, rather than having to list long paths e.g. ``` $ ./stdtest-x86_64-unknown-linux-gnu 'vec.*clone' running 2 tests test vec::tests::test_clone ... ok test vec::tests::test_clone_from ... ok test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured ``` The regex change is fully backwards compatible, since test names are Rust identifiers + `:`, and hence not special regex characters. (See commits for details.)
2 parents fbd8f4a + 18c13de commit ba5f530

File tree

6 files changed

+138
-86
lines changed

6 files changed

+138
-86
lines changed

Diff for: mk/crates.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ DEPS_collections := std rand
8080
DEPS_fourcc := syntax std
8181
DEPS_hexfloat := syntax std
8282
DEPS_num := std rand
83-
DEPS_test := std collections getopts serialize term time
83+
DEPS_test := std collections getopts serialize term time regex
8484
DEPS_time := std serialize
8585
DEPS_rand := std
8686
DEPS_url := std collections

Diff for: src/compiletest/common.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use std::from_str::FromStr;
1212
use std::fmt;
13+
use regex::Regex;
1314

1415
#[deriving(Clone, Eq)]
1516
pub enum Mode {
@@ -88,7 +89,7 @@ pub struct Config {
8889
pub run_ignored: bool,
8990

9091
// Only run tests that match this filter
91-
pub filter: Option<~str>,
92+
pub filter: Option<Regex>,
9293

9394
// Write out a parseable log of tests that were run
9495
pub logfile: Option<Path>,

Diff for: src/compiletest/compiletest.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ extern crate log;
2323
extern crate green;
2424
extern crate rustuv;
2525

26+
extern crate regex;
27+
2628
use std::os;
2729
use std::io;
2830
use std::io::fs;
@@ -113,6 +115,19 @@ pub fn parse_config(args: Vec<~str> ) -> Config {
113115
Path::new(m.opt_str(nm).unwrap())
114116
}
115117

118+
let filter = if !matches.free.is_empty() {
119+
let s = matches.free.get(0).as_slice();
120+
match regex::Regex::new(s) {
121+
Ok(re) => Some(re),
122+
Err(e) => {
123+
println!("failed to parse filter /{}/: {}", s, e);
124+
fail!()
125+
}
126+
}
127+
} else {
128+
None
129+
};
130+
116131
Config {
117132
compile_lib_path: matches.opt_str("compile-lib-path").unwrap(),
118133
run_lib_path: matches.opt_str("run-lib-path").unwrap(),
@@ -125,12 +140,7 @@ pub fn parse_config(args: Vec<~str> ) -> Config {
125140
stage_id: matches.opt_str("stage-id").unwrap(),
126141
mode: FromStr::from_str(matches.opt_str("mode").unwrap()).expect("invalid mode"),
127142
run_ignored: matches.opt_present("ignored"),
128-
filter:
129-
if !matches.free.is_empty() {
130-
Some((*matches.free.get(0)).clone())
131-
} else {
132-
None
133-
},
143+
filter: filter,
134144
logfile: matches.opt_str("logfile").map(|s| Path::new(s)),
135145
save_metrics: matches.opt_str("save-metrics").map(|s| Path::new(s)),
136146
ratchet_metrics:
@@ -169,7 +179,7 @@ pub fn log_config(config: &Config) {
169179
logv(c, format!("stage_id: {}", config.stage_id));
170180
logv(c, format!("mode: {}", config.mode));
171181
logv(c, format!("run_ignored: {}", config.run_ignored));
172-
logv(c, format!("filter: {}", opt_str(&config.filter)));
182+
logv(c, format!("filter: {}", opt_str(&config.filter.as_ref().map(|re| re.to_str()))));
173183
logv(c, format!("runtool: {}", opt_str(&config.runtool)));
174184
logv(c, format!("host-rustcflags: {}", opt_str(&config.host_rustcflags)));
175185
logv(c, format!("target-rustcflags: {}", opt_str(&config.target_rustcflags)));
@@ -238,7 +248,7 @@ pub fn test_opts(config: &Config) -> test::TestOpts {
238248
test::TestOpts {
239249
filter: match config.filter {
240250
None => None,
241-
Some(ref filter) => Some(filter.to_strbuf()),
251+
Some(ref filter) => Some(filter.clone()),
242252
},
243253
run_ignored: config.run_ignored,
244254
logfile: config.logfile.clone(),

Diff for: src/doc/guide-testing.md

+27-7
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,15 @@ fn test_out_of_bounds_failure() {
9090
~~~
9191

9292
A test runner built with the `--test` flag supports a limited set of
93-
arguments to control which tests are run: the first free argument
94-
passed to a test runner specifies a filter used to narrow down the set
95-
of tests being run; the `--ignored` flag tells the test runner to run
96-
only tests with the `ignore` attribute.
93+
arguments to control which tests are run:
94+
95+
- the first free argument passed to a test runner is interpreted as a
96+
regular expression
97+
([syntax reference](regex/index.html#syntax))
98+
and is used to narrow down the set of tests being run. Note: a plain
99+
string is a valid regular expression that matches itself.
100+
- the `--ignored` flag tells the test runner to run only tests with the
101+
`ignore` attribute.
97102

98103
## Parallelism
99104

@@ -146,16 +151,31 @@ result: FAILED. 1 passed; 1 failed; 0 ignored
146151

147152
### Running a subset of tests
148153

154+
Using a plain string:
155+
156+
~~~ {.notrust}
157+
$ mytests mytest23
158+
159+
running 1 tests
160+
running driver::tests::mytest23 ... ok
161+
162+
result: ok. 1 passed; 0 failed; 0 ignored
163+
~~~
164+
165+
Using some regular expression features:
166+
149167
~~~ {.notrust}
150-
$ mytests mytest1
168+
$ mytests 'mytest[145]'
151169
152-
running 11 tests
170+
running 13 tests
153171
running driver::tests::mytest1 ... ok
172+
running driver::tests::mytest4 ... ok
173+
running driver::tests::mytest5 ... ok
154174
running driver::tests::mytest10 ... ignored
155175
... snip ...
156176
running driver::tests::mytest19 ... ok
157177
158-
result: ok. 11 passed; 0 failed; 1 ignored
178+
result: ok. 13 passed; 0 failed; 1 ignored
159179
~~~
160180

161181
# Microbenchmarking

Diff for: src/libstd/unstable/dynamic_lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ A simple wrapper over the platform's dynamic library facilities
1616
1717
*/
1818

19+
1920
use c_str::ToCStr;
20-
use iter::Iterator;
2121
use mem;
2222
use ops::*;
2323
use option::*;
2424
use os;
2525
use path::GenericPath;
2626
use path;
2727
use result::*;
28-
use slice::{Vector,OwnedVector};
28+
use slice::Vector;
2929
use str;
3030
use vec::Vec;
3131

@@ -85,10 +85,12 @@ impl DynamicLibrary {
8585
} else {
8686
("LD_LIBRARY_PATH", ':' as u8)
8787
};
88-
let newenv = os::getenv_as_bytes(envvar).unwrap_or(box []);
89-
let mut newenv = newenv.move_iter().collect::<Vec<_>>();
90-
newenv.push_all(&[sep]);
91-
newenv.push_all(path.as_vec());
88+
let mut newenv = Vec::from_slice(path.as_vec());
89+
newenv.push(sep);
90+
match os::getenv_as_bytes(envvar) {
91+
Some(bytes) => newenv.push_all(bytes),
92+
None => {}
93+
}
9294
os::setenv(envvar, str::from_utf8(newenv.as_slice()).unwrap());
9395
}
9496

0 commit comments

Comments
 (0)