cargo/tests/test_cargo_run.rs
Alex Crichton ef28cd2d97 Robustly run binaries and tests after compilation
These were previously just run by executing the raw binary, but this didn't
ensure that any of the necessary paths were in the dylib search path for the
host platform.

This commit enhances the return type of `compile_targets` to have information
about the result of compilation, along with the ability to spawn correctly
configured processes.

This primarily fixes `cargo test` and `cargo run` whenever dynamic dependencies
are involved, but it also fixes the two commands whenever there's a native
dynamic dependency involved as well.
2014-08-06 18:46:23 -07:00

116 lines
2.9 KiB
Rust

use std::path;
use support::{project, execs, path2url};
use support::{COMPILING, RUNNING};
use hamcrest::{assert_that, existing_file};
fn setup() {
}
test!(simple {
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/main.rs", r#"
fn main() { println!("hello"); }
"#);
assert_that(p.cargo_process("cargo-run"),
execs().with_status(0).with_stdout(format!("\
{compiling} foo v0.0.1 ({dir})
{running} `target{sep}foo`
hello
",
compiling = COMPILING,
running = RUNNING,
dir = path2url(p.root()),
sep = path::SEP).as_slice()));
assert_that(&p.bin("foo"), existing_file());
})
test!(simple_with_args {
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/main.rs", r#"
fn main() {
assert_eq!(std::os::args().get(1).as_slice(), "hello");
assert_eq!(std::os::args().get(2).as_slice(), "world");
}
"#);
assert_that(p.cargo_process("cargo-run").arg("hello").arg("world"),
execs().with_status(0));
})
test!(exit_code {
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/main.rs", r#"
fn main() { std::os::set_exit_status(2); }
"#);
assert_that(p.cargo_process("cargo-run"),
execs().with_status(2));
})
test!(no_main_file {
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/lib.rs", "");
assert_that(p.cargo_process("cargo-run"),
execs().with_status(101)
.with_stderr("`src/main.rs` must be present for \
`cargo run`\n"));
})
test!(run_dylib_dep {
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
[dependencies.bar]
path = "bar"
"#)
.file("src/main.rs", r#"
extern crate bar;
fn main() { bar::bar(); }
"#)
.file("bar/Cargo.toml", r#"
[package]
name = "bar"
version = "0.0.1"
authors = []
[[lib]]
name = "bar"
crate-type = ["dylib"]
"#)
.file("bar/src/lib.rs", "pub fn bar() {}");
assert_that(p.cargo_process("cargo-run").arg("hello").arg("world"),
execs().with_status(0));
})