A passing test

This commit is contained in:
Carl Lerche 2014-03-19 17:57:12 -07:00
parent 2009fd76ab
commit 07af6fdd4a
11 changed files with 114 additions and 27 deletions

View File

@ -52,7 +52,7 @@ tests/tests: $(BIN_TARGETS) $(HAMCREST) $(TEST_SRC)
$(RUSTC) --test --crate-type=lib $(TEST_DEPS) -Ltarget --out-dir tests tests/tests.rs
test-integration: tests/tests
tests/tests
CARGO_BIN_PATH=$(PWD)/target/ tests/tests
test: test-integration

View File

@ -1,4 +1,5 @@
#[crate_id="cargo-compile"];
#[allow(deprecated_owned_vector)];
extern crate serialize;
extern crate hammer;

View File

@ -1,4 +1,5 @@
#[crate_id="cargo-read-manifest"];
#[allow(deprecated_owned_vector)];
extern crate cargo;
extern crate hammer;
@ -111,7 +112,7 @@ fn normalize(lib: &Option<~[SerializedLibTarget]>, bin: &Option<~[SerializedExec
let b = b_ref.clone();
let mut path = b.path.clone();
if path.is_none() {
path = Some(format!("src/bin/{}.rs", b.name.clone()));
path = Some(format!("src/{}.rs", b.name.clone()));
}
ExecTarget{ path: path.unwrap(), name: b.name }
});

View File

@ -1,4 +1,5 @@
#[crate_id="cargo-rustc"];
#[allow(deprecated_owned_vector)];
extern crate toml;
extern crate serialize;
@ -10,7 +11,7 @@ use std::io::process::{Process,ProcessConfig,InheritFd};
use serialize::json;
use serialize::Decodable;
use std::path::Path;
use cargo::{Manifest,CargoResult,ToCargoError};
use cargo::{Manifest,CargoResult,CargoError,ToCargoError};
/**
cargo-rustc -- ...args
@ -21,7 +22,7 @@ use cargo::{Manifest,CargoResult,ToCargoError};
fn main() {
match execute() {
Err(e) => {
println!("{}", e.message);
write!(&mut std::io::stderr(), "{}", e.message);
// TODO: Exit with error code
},
_ => return
@ -36,16 +37,23 @@ fn execute() -> CargoResult<()> {
let mut decoder = json::Decoder::new(json);
let manifest: Manifest = Decodable::decode(&mut decoder);
let Manifest{ root, lib, .. } = manifest;
let Manifest{ root, lib, bin, .. } = manifest;
let (crate_type, out_dir) = if lib.len() > 0 {
( ~"lib", lib[0].path )
} else if bin.len() > 0 {
( ~"bin", bin[0].path )
} else {
return Err(CargoError::new(~"bad manifest, no lib or bin specified", 1));
};
let root = Path::new(root);
let out_dir = lib[0].path;
let target = join(&root, ~"target");
let args = [
join(&root, out_dir),
~"--out-dir", target,
~"--crate-type", ~"lib"
~"--crate-type", crate_type
];
match io::fs::mkdir_recursive(&root.join("target"), io::UserRWX) {
@ -75,3 +83,11 @@ fn execute() -> CargoResult<()> {
fn join(path: &Path, part: ~str) -> ~str {
format!("{}", path.join(part).display())
}
fn vec_idx<T>(v: ~[T], idx: uint) -> Option<T> {
if idx < v.len() {
Some(v[idx])
} else {
None
}
}

View File

@ -1,4 +1,5 @@
#[crate_id="cargo-verify-project"];
#[allow(deprecated_owned_vector)];
extern crate toml;
extern crate getopts;

View File

@ -1,6 +1,8 @@
#[crate_id="cargo"];
#[crate_type="rlib"];
#[allow(deprecated_owned_vector)];
extern crate serialize;
use serialize::{Decoder};
use std::fmt;

View File

@ -1,2 +1,2 @@
pub use self::process_builder::process;
mod process_builder;
pub mod process_builder;

View File

@ -1,26 +1,63 @@
use std;
use std::os;
use std::io::process::{Process,ProcessConfig,InheritFd};
use std::path::Path;
use std::io::IoResult;
use std::io::process::{Process,ProcessConfig,ProcessOutput};
use ToCargoError;
use CargoResult;
pub struct ProcessBuilder {
program: ~str,
args: ~[~str],
path: ~[~str]
path: ~[~str],
cwd: Path
}
// TODO: Upstream a Windows/Posix branch to Rust proper
static PATH_SEP : &'static str = ":";
impl ProcessBuilder {
pub fn args(mut self, arguments: &[~str]) -> ProcessBuilder {
self.args = arguments.to_owned();
self
}
pub fn extra_path(mut self, path: &str) -> ProcessBuilder {
self.path.push(path.to_owned());
self
}
pub fn cwd(mut self, path: Path) -> ProcessBuilder {
self.cwd = path;
self
}
pub fn exec_with_output(self) -> CargoResult<ProcessOutput> {
let mut config = ProcessConfig::new();
println!("cwd: {}", self.cwd.display());
config.program = self.program.as_slice();
config.args = self.args.as_slice();
config.cwd = Some(&self.cwd);
let os_path = try!(os::getenv("PATH").to_cargo_error(~"Could not find the PATH environment variable", 1));
let path = os_path + PATH_SEP + self.path.connect(PATH_SEP);
let path = [(~"PATH", path)];
config.env = Some(path.as_slice());
println!("{:?}", config);
Process::configure(config).map(|mut ok| ok.wait_with_output()).to_cargo_error(~"Could not spawn process", 1)
}
}
pub fn process(cmd: &str) -> ProcessBuilder {
ProcessBuilder { program: cmd.to_owned(), args: ~[], path: get_curr_path() }
}
pub fn get_curr_path() -> ~[~str] {
os::getenv("PATH").map(|path| {
path.split(std::path::SEP).map(|seg| seg.to_owned()).collect()
}).unwrap_or(~[])
ProcessBuilder {
program: cmd.to_owned(),
args: ~[],
path: ~[],
cwd: os::getcwd()
}
}

View File

@ -49,19 +49,24 @@ impl ProjectBuilder {
}
}
pub fn root(&self) -> Path {
self.root.clone()
}
pub fn file(mut self, path: &str, body: &str) -> ProjectBuilder {
self.files.push(FileBuilder::new(self.root.join(path), body));
self
}
pub fn build(self) {
// TODO: return something different than a ProjectBuilder
pub fn build(self) -> ProjectBuilder {
match self.build_with_result() {
Err(e) => fail!(e),
_ => return
_ => return self
}
}
pub fn build_with_result(self) -> Result<(), ~str> {
pub fn build_with_result(&self) -> Result<(), ~str> {
// First, clean the directory if it already exists
try!(self.rm_root());

View File

@ -1,3 +1,4 @@
use std;
use support::project;
use cargo;
@ -6,7 +7,7 @@ fn setup() {
}
test!(cargo_compile_with_explicit_manifest_path {
project("foo")
let p = project("foo")
.file("Cargo.toml", r#"
[project]
@ -24,13 +25,29 @@ test!(cargo_compile_with_explicit_manifest_path {
}"#)
.build();
cargo::util::process("cargo-compile")
.args([]);
// //.extra_path("target/")
// //.cwd("/foo/bar")
// //.exec_with_output()
let output = cargo::util::process("cargo-compile")
.args([~"--manifest-path", ~"Cargo.toml"])
.extra_path(target_path())
.cwd(p.root())
.exec_with_output();
match output {
Ok(out) => {
println!("out:\n{}\n", std::str::from_utf8(out.output));
println!("err:\n{}\n", std::str::from_utf8(out.error));
},
Err(e) => println!("err: {}", e)
}
assert!(p.root().join("target/foo").exists(), "the executable exists");
let o = cargo::util::process("foo")
.extra_path(format!("{}", p.root().join("target").display()))
.exec_with_output()
.unwrap();
assert_eq!(std::str::from_utf8(o.output).unwrap(), "i am foo\n");
fail!("not implemented");
// 1) Setup project
// 2) Run cargo-compile --manifest-path /tmp/bar/zomg
// 3) assertThat(target/foo) exists assertThat("target/foo", isCompiledBin())
@ -38,3 +55,9 @@ test!(cargo_compile_with_explicit_manifest_path {
})
// test!(compiling_project_with_invalid_manifest)
fn target_path() -> ~str {
std::os::getenv("CARGO_BIN_PATH").unwrap_or_else(|| {
fail!("CARGO_BIN_PATH wasn't set. Cannot continue running test")
})
}

View File

@ -1,4 +1,5 @@
#[feature(macro_rules)];
#[allow(deprecated_owned_vector)];
extern crate cargo;