Skip to content

Commit 9e61b47

Browse files
committed
auto merge of #341 : alexcrichton/cargo/doc-test-tweaks, r=wycats
2 parents 0f61149 + 20f01bf commit 9e61b47

File tree

9 files changed

+121
-49
lines changed

9 files changed

+121
-49
lines changed

src/cargo/ops/cargo_rustc/compilation.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ pub struct Compilation {
3030

3131
/// Output directory for rust dependencies
3232
pub deps_output: Path,
33+
34+
/// Extra environment variables that were passed to compilations and should
35+
/// be passed to future invocations of programs.
36+
pub extra_env: HashMap<String, Option<String>>,
3337
}
3438

3539
impl Compilation {
@@ -41,6 +45,7 @@ impl Compilation {
4145
deps_output: Path::new("/"),
4246
tests: Vec::new(),
4347
binaries: Vec::new(),
48+
extra_env: HashMap::new(),
4449
}
4550
}
4651

@@ -54,7 +59,11 @@ impl Compilation {
5459
search_path.push(self.root_output.clone());
5560
search_path.push(self.deps_output.clone());
5661
let search_path = os::join_paths(search_path.as_slice()).unwrap();
57-
util::process(cmd).env(DynamicLibrary::envvar(),
58-
Some(search_path.as_slice()))
62+
let mut cmd = util::process(cmd).env(DynamicLibrary::envvar(),
63+
Some(search_path.as_slice()));
64+
for (k, v) in self.extra_env.iter() {
65+
cmd = cmd.env(k.as_slice(), v.as_ref().map(|s| s.as_slice()));
66+
}
67+
return cmd;
5968
}
6069
}

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::{HashMap, HashSet};
22
use std::str;
3+
use semver::Version;
34

45
use core::{SourceMap, Package, PackageId, PackageSet, Resolve, Target};
56
use util;
@@ -127,7 +128,32 @@ impl<'a, 'b> Context<'a, 'b> {
127128
self.compilation.root_output = self.layout(KindTarget).proxy().dest().clone();
128129
self.compilation.deps_output = self.layout(KindTarget).proxy().deps().clone();
129130

130-
Ok(())
131+
let env = &mut self.compilation.extra_env;
132+
env.insert("CARGO_PKG_VERSION_MAJOR".to_string(),
133+
Some(pkg.get_version().major.to_string()));
134+
env.insert("CARGO_PKG_VERSION_MINOR".to_string(),
135+
Some(pkg.get_version().minor.to_string()));
136+
env.insert("CARGO_PKG_VERSION_PATCH".to_string(),
137+
Some(pkg.get_version().patch.to_string()));
138+
env.insert("CARGO_PKG_VERSION_PRE".to_string(),
139+
pre_version_component(pkg.get_version()));
140+
141+
return Ok(());
142+
143+
fn pre_version_component(v: &Version) -> Option<String> {
144+
if v.pre.is_empty() {
145+
return None;
146+
}
147+
148+
let mut ret = String::new();
149+
150+
for (i, x) in v.pre.iter().enumerate() {
151+
if i != 0 { ret.push_char('.') };
152+
ret.push_str(x.to_string().as_slice());
153+
}
154+
155+
Some(ret)
156+
}
131157
}
132158

133159
fn build_requirements(&mut self, pkg: &'a Package, target: &'a Target,

src/cargo/ops/cargo_rustc/fingerprint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn prepare_target(cx: &mut Context, pkg: &Package, target: &Target,
8282
cx.compilation.tests.push(dst.clone());
8383
} else if target.is_bin() {
8484
cx.compilation.binaries.push(dst.clone());
85-
} else if target.is_lib() && target.get_profile().is_compile() {
85+
} else if target.is_lib() {
8686
let pkgid = pkg.get_package_id().clone();
8787
cx.compilation.libraries.find_or_insert(pkgid, Vec::new())
8888
.push(root.join(filename));

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ use std::collections::HashSet;
22
use std::dynamic_lib::DynamicLibrary;
33
use std::io::{fs, UserRWX};
44
use std::os;
5-
use semver::Version;
65

76
use core::{SourceMap, Package, PackageId, PackageSet, Target, Resolve};
8-
use util;
97
use util::{CargoResult, ProcessBuilder, CargoError, human, caused_human};
108
use util::{Config, internal, ChainError, Fresh, profile};
119

@@ -433,26 +431,9 @@ pub fn process<T: ToCStr>(cmd: T, pkg: &Package, cx: &Context) -> ProcessBuilder
433431
search_path.push(cx.layout(KindPlugin).deps().clone());
434432
let search_path = os::join_paths(search_path.as_slice()).unwrap();
435433

436-
util::process(cmd)
437-
.cwd(pkg.get_root())
438-
.env(DynamicLibrary::envvar(), Some(search_path.as_slice()))
439-
.env("CARGO_PKG_VERSION_MAJOR", Some(pkg.get_version().major.to_string()))
440-
.env("CARGO_PKG_VERSION_MINOR", Some(pkg.get_version().minor.to_string()))
441-
.env("CARGO_PKG_VERSION_PATCH", Some(pkg.get_version().patch.to_string()))
442-
.env("CARGO_PKG_VERSION_PRE", pre_version_component(pkg.get_version()))
443-
}
444-
445-
fn pre_version_component(v: &Version) -> Option<String> {
446-
if v.pre.is_empty() {
447-
return None;
448-
}
449-
450-
let mut ret = String::new();
451-
452-
for (i, x) in v.pre.iter().enumerate() {
453-
if i != 0 { ret.push_char('.') };
454-
ret.push_str(x.to_string().as_slice());
455-
}
456-
457-
Some(ret)
434+
// We want to use the same environment and such as normal processes, but we
435+
// want to override the dylib search path with the one we just calculated.
436+
cx.compilation.process(cmd).cwd(pkg.get_root())
437+
.env(DynamicLibrary::envvar(),
438+
Some(search_path.as_slice()))
458439
}

src/cargo/ops/cargo_test.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ pub fn run_tests(manifest_path: &Path,
2121
Some(path) => path,
2222
None => exe.clone(),
2323
};
24-
try!(options.shell.status("Running", to_display.display()));
25-
match compile.process(exe).args(args).exec() {
24+
let cmd = compile.process(exe).args(args);
25+
try!(options.shell.concise(|shell| {
26+
shell.status("Running", to_display.display().to_string())
27+
}));
28+
try!(options.shell.verbose(|shell| {
29+
shell.status("Running", cmd.to_string())
30+
}));
31+
match cmd.exec() {
2632
Ok(()) => {}
2733
Err(e) => return Ok(Some(e))
2834
}
@@ -58,6 +64,9 @@ pub fn run_tests(manifest_path: &Path,
5864
}
5965
}
6066

67+
try!(options.shell.verbose(|shell| {
68+
shell.status("Running", p.to_string())
69+
}));
6170
match p.exec() {
6271
Ok(()) => {}
6372
Err(e) => return Ok(Some(e)),

src/snapshots.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2014-08-06
2+
linux-i386 eb7c2a87b30db077f6f1c4ea724ebd0e5cc07d1c
3+
linux-x86_64 1672657adb9012df2912bbb2f43466f1c6817e55
4+
macos-i386 1224207bbfa9f46796940512ac8a7a9ab9f5665b
5+
macos-x86_64 da4afea32d7336a0a91b8fe160d38896385d4ae2
6+
winnt-i386 2b6b2efe9ec77d3d456c943bb2e54f2281309ef1
7+
18
2014-08-04
29
linux-i386 49032ce8c5c2b94d73e298dcbdb09e0b2fbe573c
310
linux-x86_64 98c83ecc7cac3765d62f5e8b19bdc506e01f3cab

tests/test_cargo_compile.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -787,28 +787,32 @@ test!(crate_version_env_vars {
787787
let p = project("foo")
788788
.file("Cargo.toml", r#"
789789
[project]
790-
791790
name = "foo"
792791
version = "0.5.1-alpha.1"
793792
authors = ["[email protected]"]
794-
795-
[[bin]]
796-
name = "foo"
797793
"#)
798-
.file("src/foo.rs", r#"
799-
use std::os;
794+
.file("src/main.rs", r#"
795+
extern crate foo;
800796
801797
static VERSION_MAJOR: &'static str = env!("CARGO_PKG_VERSION_MAJOR");
802798
static VERSION_MINOR: &'static str = env!("CARGO_PKG_VERSION_MINOR");
803799
static VERSION_PATCH: &'static str = env!("CARGO_PKG_VERSION_PATCH");
804800
static VERSION_PRE: &'static str = env!("CARGO_PKG_VERSION_PRE");
805801
806802
fn main() {
807-
println!("{}-{}-{} @ {}",
808-
VERSION_MAJOR,
809-
VERSION_MINOR,
810-
VERSION_PATCH,
811-
VERSION_PRE);
803+
let s = format!("{}-{}-{} @ {}", VERSION_MAJOR, VERSION_MINOR,
804+
VERSION_PATCH, VERSION_PRE);
805+
assert_eq!(s, foo::version());
806+
println!("{}", s);
807+
}
808+
"#)
809+
.file("src/lib.rs", r#"
810+
pub fn version() -> String {
811+
format!("{}-{}-{} @ {}",
812+
env!("CARGO_PKG_VERSION_MAJOR"),
813+
env!("CARGO_PKG_VERSION_MINOR"),
814+
env!("CARGO_PKG_VERSION_PATCH"),
815+
env!("CARGO_PKG_VERSION_PRE"))
812816
}
813817
"#);
814818

@@ -817,6 +821,8 @@ test!(crate_version_env_vars {
817821
assert_that(
818822
process(p.bin("foo")),
819823
execs().with_stdout("0-5-1 @ alpha.1\n"));
824+
825+
assert_that(p.process(cargo_dir().join("cargo-test")), execs().with_status(0));
820826
})
821827

822828
test!(custom_build_in_dependency {

tests/test_cargo_compile_git_deps.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,9 +714,6 @@ test!(dep_with_submodule {
714714
pub fn foo() { dep1::dep() }
715715
");
716716

717-
let root = project.url();
718-
let git_root = git_project.url();
719-
720717
assert_that(project.cargo_process("cargo-build"),
721718
execs().with_stderr("").with_status(0));
722719
})

tests/test_cargo_test.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n",
4646
RUNNING)));
4747
})
4848

49+
test!(cargo_test_verbose {
50+
let p = project("foo")
51+
.file("Cargo.toml", basic_bin_manifest("foo").as_slice())
52+
.file("src/foo.rs", r#"
53+
fn main() {}
54+
#[test] fn test_hello() {}
55+
"#);
56+
57+
assert_that(p.cargo_process("cargo-test").arg("-v").arg("hello"),
58+
execs().with_stdout(format!("\
59+
{running} `rustc src[..]foo.rs [..]`
60+
{compiling} foo v0.5.0 ({url})
61+
{running} `[..]target[..]test[..]foo-[..] hello`
62+
63+
running 1 test
64+
test test_hello ... ok
65+
66+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n",
67+
compiling = COMPILING, url = p.url(), running = RUNNING)));
68+
})
69+
4970
test!(many_similar_names {
5071
let p = project("foo")
5172
.file("Cargo.toml", r#"
@@ -498,13 +519,15 @@ test!(lib_with_standard_name {
498519
name = "syntax"
499520
version = "0.0.1"
500521
authors = []
501-
502-
[[lib]]
503-
name = "syntax"
504-
test = false
505522
"#)
506523
.file("src/lib.rs", "
524+
/// ```
525+
/// syntax::foo();
526+
/// ```
507527
pub fn foo() {}
528+
529+
#[test]
530+
fn foo_test() {}
508531
")
509532
.file("tests/test.rs", "
510533
extern crate syntax;
@@ -517,15 +540,29 @@ test!(lib_with_standard_name {
517540
execs().with_status(0)
518541
.with_stdout(format!("\
519542
{compiling} syntax v0.0.1 ({dir})
543+
{running} target[..]test[..]syntax-[..]
544+
545+
running 1 test
546+
test foo_test ... ok
547+
548+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n\
549+
520550
{running} target[..]test[..]test-[..]
521551
522552
running 1 test
523553
test test ... ok
524554
555+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n\
556+
557+
{doctest} syntax
558+
559+
running 1 test
560+
test foo_0 ... ok
561+
525562
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n\
526563
",
527564
compiling = COMPILING, running = RUNNING,
528-
dir = p.url()).as_slice()));
565+
doctest = DOCTEST, dir = p.url()).as_slice()));
529566
})
530567

531568
test!(lib_with_standard_name2 {

0 commit comments

Comments
 (0)