Skip to content

Commit 256faff

Browse files
committed
pass features to doctest binary
1 parent 1384050 commit 256faff

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

src/cargo/ops/cargo_rustc/compilation.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::collections::{HashMap, HashSet};
22
use std::dynamic_lib::DynamicLibrary;
33
use std::ffi::CString;
44
use std::old_path::BytesContainer;
@@ -42,6 +42,9 @@ pub struct Compilation {
4242

4343
/// Top-level package that was compiled
4444
pub package: Package,
45+
46+
/// Features enabled during this compilation.
47+
pub features: HashSet<String>,
4548
}
4649

4750
impl Compilation {
@@ -55,6 +58,7 @@ impl Compilation {
5558
binaries: Vec::new(),
5659
extra_env: HashMap::new(),
5760
package: pkg.clone(),
61+
features: HashSet::new(),
5862
}
5963
}
6064

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ pub fn compile_targets<'a, 'b>(env: &str,
189189
let out_dir = cx.layout(pkg, Kind::Target).build_out(pkg)
190190
.display().to_string();
191191
cx.compilation.extra_env.insert("OUT_DIR".to_string(), Some(out_dir));
192+
193+
if let Some(feats) = cx.resolve.features(pkg.package_id()) {
194+
cx.compilation.features.extend(feats.iter().cloned());
195+
}
196+
192197
for (&(ref pkg, _), output) in cx.build_state.outputs.lock().unwrap().iter() {
193198
let any_dylib = output.library_links.iter().any(|l| {
194199
!l.ends_with(":static") && !l.ends_with(":framework")

src/cargo/ops/cargo_test.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ pub fn run_tests(manifest_path: &Path,
7272
p = p.arg("--test-args").arg(test_args.connect(" "));
7373
}
7474

75+
for feat in compile.features.iter() {
76+
p = p.arg("--cfg").arg(format!("feature=\"{}\"", feat));
77+
}
78+
7579
for (pkg, libs) in compile.libraries.iter() {
7680
for lib in libs.iter() {
7781
let mut arg = pkg.name().as_bytes().to_vec();

tests/test_cargo_test.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,3 +1352,40 @@ no example target named `foo` to run
13521352
no bin target named `foo` to run
13531353
"));
13541354
});
1355+
1356+
test!(doctest_feature {
1357+
let p = project("foo")
1358+
.file("Cargo.toml", r#"
1359+
[package]
1360+
name = "foo"
1361+
version = "0.0.1"
1362+
authors = []
1363+
[features]
1364+
bar = []
1365+
"#)
1366+
.file("src/lib.rs", r#"
1367+
/// ```rust
1368+
/// assert_eq!(foo::foo(), 1);
1369+
/// ```
1370+
#[cfg(feature = "bar")]
1371+
pub fn foo() -> i32 { 1 }
1372+
"#);
1373+
1374+
assert_that(p.cargo_process("test").arg("--features").arg("bar"),
1375+
execs().with_status(0).with_stdout(format!("\
1376+
{compiling} foo [..]
1377+
{running} target[..]foo[..]
1378+
1379+
running 0 tests
1380+
1381+
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
1382+
1383+
{doctest} foo
1384+
1385+
running 1 test
1386+
test foo_0 ... ok
1387+
1388+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
1389+
1390+
", compiling = COMPILING, running = RUNNING, doctest = DOCTEST)))
1391+
});

0 commit comments

Comments
 (0)