Skip to content

Commit f7ebe23

Browse files
committed
Add the ability to ignore tests by compiler config
[test] [ignore(cfg(target_os = "win32"))]
1 parent 2e0593d commit f7ebe23

File tree

4 files changed

+65
-21
lines changed

4 files changed

+65
-21
lines changed

src/comp/front/attr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export require_unique_names;
1818
export get_attr_name;
1919
export get_meta_item_name;
2020
export get_meta_item_value_str;
21+
export get_meta_item_list;
2122
export mk_name_value_item_str;
2223
export mk_name_value_item;
2324
export mk_list_item;
@@ -85,6 +86,13 @@ fn get_meta_item_value_str(meta: @ast::meta_item) -> option::t<str> {
8586
}
8687
}
8788

89+
fn get_meta_item_list(meta: @ast::meta_item) -> option::t<[@ast::meta_item]> {
90+
alt meta.node {
91+
ast::meta_list(_, l) { option::some(l) }
92+
_ { option::none }
93+
}
94+
}
95+
8896
fn attr_meta(attr: ast::attribute) -> @ast::meta_item { @attr.node.value }
8997

9098
// Get the meta_items from inside a vector of attributes

src/comp/front/config.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import syntax::{ast, fold};
33
import attr;
44

55
export strip_unconfigured_items;
6+
export metas_in_cfg;
67

78
// Support conditional compilation by transforming the AST, stripping out
89
// any items that do not belong in the current configuration
@@ -88,31 +89,24 @@ fn native_item_in_cfg(cfg: ast::crate_cfg, item: @ast::native_item) -> bool {
8889
// Determine if an item should be translated in the current crate
8990
// configuration based on the item's attributes
9091
fn in_cfg(cfg: ast::crate_cfg, attrs: [ast::attribute]) -> bool {
92+
metas_in_cfg(cfg, attr::attr_metas(attrs))
93+
}
94+
95+
fn metas_in_cfg(cfg: ast::crate_cfg, metas: [@ast::meta_item]) -> bool {
9196

9297
// The "cfg" attributes on the item
93-
let item_cfg_attrs = attr::find_attrs_by_name(attrs, "cfg");
94-
let item_has_cfg_attrs = vec::len(item_cfg_attrs) > 0u;
95-
if !item_has_cfg_attrs { ret true; }
98+
let cfg_metas = attr::find_meta_items_by_name(metas, "cfg");
9699

97100
// Pull the inner meta_items from the #[cfg(meta_item, ...)] attributes,
98101
// so we can match against them. This is the list of configurations for
99102
// which the item is valid
100-
let item_cfg_metas = {
101-
fn extract_metas(&&inner_items: [@ast::meta_item],
102-
&&cfg_item: @ast::meta_item) -> [@ast::meta_item] {
103-
alt cfg_item.node {
104-
ast::meta_list(name, items) {
105-
assert (name == "cfg");
106-
inner_items + items
107-
}
108-
_ { inner_items }
109-
}
110-
}
111-
let cfg_metas = attr::attr_metas(item_cfg_attrs);
112-
vec::foldl(extract_metas, [], cfg_metas)
113-
};
103+
let cfg_metas = vec::concat(vec::filter_map(
104+
{|&&i| attr::get_meta_item_list(i)}, cfg_metas));
105+
106+
let has_cfg_metas = vec::len(cfg_metas) > 0u;
107+
if !has_cfg_metas { ret true; }
114108

115-
for cfg_mi: @ast::meta_item in item_cfg_metas {
109+
for cfg_mi: @ast::meta_item in cfg_metas {
116110
if attr::contains(cfg, cfg_mi) { ret true; }
117111
}
118112

src/comp/front/test.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type test = {span: span, path: [ast::ident], ignore: bool};
1818

1919
type test_ctxt =
2020
@{sess: session::session,
21+
crate: @ast::crate,
2122
next_node_id: node_id_gen,
2223
mutable path: [ast::ident],
2324
mutable testfns: [test]};
@@ -41,6 +42,7 @@ fn modify_for_testing(sess: session::session,
4142

4243
let cx: test_ctxt =
4344
@{sess: sess,
45+
crate: crate,
4446
next_node_id: next_node_id_fn,
4547
mutable path: [],
4648
mutable testfns: []};
@@ -102,7 +104,8 @@ fn fold_item(cx: test_ctxt, &&i: @ast::item, fld: fold::ast_fold) ->
102104
}
103105
_ {
104106
log "this is a test function";
105-
let test = {span: i.span, path: cx.path, ignore: is_ignored(i)};
107+
let test = {span: i.span,
108+
path: cx.path, ignore: is_ignored(cx, i)};
106109
cx.testfns += [test];
107110
log #fmt["have %u test functions", vec::len(cx.testfns)];
108111
}
@@ -133,8 +136,16 @@ fn is_test_fn(i: @ast::item) -> bool {
133136
ret has_test_attr && has_test_signature(i);
134137
}
135138

136-
fn is_ignored(i: @ast::item) -> bool {
137-
attr::contains_name(attr::attr_metas(i.attrs), "ignore")
139+
fn is_ignored(cx: test_ctxt, i: @ast::item) -> bool {
140+
let ignoreattrs = attr::find_attrs_by_name(i.attrs, "ignore");
141+
let ignoreitems = attr::attr_metas(ignoreattrs);
142+
let cfg_metas = vec::concat(vec::filter_map(
143+
{|&&i| attr::get_meta_item_list(i)}, ignoreitems));
144+
ret if vec::is_not_empty(ignoreitems) {
145+
config::metas_in_cfg(cx.crate.node.config, cfg_metas)
146+
} else {
147+
false
148+
}
138149
}
139150

140151
fn add_test_module(cx: test_ctxt, m: ast::_mod) -> ast::_mod {

src/test/run-pass/test-ignore-cfg.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// compile-flags: --test --cfg ignorecfg
2+
// xfail-fast
3+
// xfail-pretty
4+
5+
use std;
6+
import std::option;
7+
import std::vec;
8+
9+
#[test]
10+
#[ignore(cfg(ignorecfg))]
11+
fn shouldignore() {
12+
}
13+
14+
#[test]
15+
#[ignore(cfg(noignorecfg))]
16+
fn shouldnotignore() {
17+
}
18+
19+
#[test]
20+
fn checktests() {
21+
// Pull the tests out of the secret test module
22+
let tests = __test::tests();
23+
24+
let shouldignore = option::get(
25+
vec::find({|t| t.name == "shouldignore"}, tests));
26+
assert shouldignore.ignore == true;
27+
28+
let shouldnotignore = option::get(
29+
vec::find({|t| t.name == "shouldnotignore"}, tests));
30+
assert shouldnotignore.ignore == false;
31+
}

0 commit comments

Comments
 (0)