Skip to content

Commit

Permalink
[cargo] add --test flag to install
Browse files Browse the repository at this point in the history
  • Loading branch information
Elly Jones committed Jan 19, 2012
1 parent 6a6aec0 commit ad0065f
Showing 1 changed file with 48 additions and 15 deletions.
63 changes: 48 additions & 15 deletions src/cargo/cargo.rs
Expand Up @@ -54,7 +54,8 @@ type cargo = {
libdir: str,
workdir: str,
sourcedir: str,
sources: map::hashmap<str, source>
sources: map::hashmap<str, source>,
mutable test: bool
};

type pkg = {
Expand All @@ -67,7 +68,7 @@ type pkg = {
};

fn info(msg: str) {
io::stdout().write_line(msg);
io::stdout().write_line("info: " + msg);
}

fn warn(msg: str) {
Expand Down Expand Up @@ -323,7 +324,8 @@ fn configure() -> cargo {
libdir: fs::connect(p, "lib"),
workdir: fs::connect(p, "work"),
sourcedir: fs::connect(p, "sources"),
sources: sources
sources: sources,
mutable test: false
};

need_dir(c.root);
Expand Down Expand Up @@ -353,15 +355,16 @@ fn for_each_package(c: cargo, b: block(source, package)) {
})
}

fn install_one_crate(c: cargo, _path: str, cf: str, _p: pkg) {
// FIXME: deduplicate code with install_one_crate
fn test_one_crate(c: cargo, _path: str, cf: str, _p: pkg) {
let name = fs::basename(cf);
let ri = str::index(name, '.' as u8);
if ri != -1 {
name = str::slice(name, 0u, ri as uint);
}
#debug("Installing: %s", name);
let old = fs::list_dir(".");
let p = run::program_output("rustc", [name + ".rc"]);
let p = run::program_output("rustc", ["--test", name + ".rc"]);
if p.status != 0 {
error(#fmt["rustc failed: %d\n%s\n%s", p.status, p.err, p.out]);
ret;
Expand All @@ -371,6 +374,26 @@ fn install_one_crate(c: cargo, _path: str, cf: str, _p: pkg) {
vec::filter::<str>(new, { |n| !vec::member::<str>(n, old) });
let exec_suffix = os::exec_suffix();
for ct: str in created {
if (exec_suffix != "" && str::ends_with(ct, exec_suffix)) ||
(exec_suffix == "" && !str::starts_with(ct, "./lib")) {
// FIXME: need libstd fs::copy or something
run::run_program(ct, []);
}
}
}

fn install_one_crate(c: cargo, _path: str, cf: str, _p: pkg) {
let buildpath = fs::connect(_path, "/build");
need_dir(buildpath);
#debug("Installing: %s -> %s", cf, buildpath);
let p = run::program_output("rustc", ["--out-dir", buildpath, cf]);
if p.status != 0 {
error(#fmt["rustc failed: %d\n%s\n%s", p.status, p.err, p.out]);
ret;
}
let new = fs::list_dir(buildpath);
let exec_suffix = os::exec_suffix();
for ct: str in new {
if (exec_suffix != "" && str::ends_with(ct, exec_suffix)) ||
(exec_suffix == "" && !str::starts_with(ct, "./lib")) {
#debug(" bin: %s", ct);
Expand Down Expand Up @@ -402,6 +425,9 @@ fn install_source(c: cargo, path: str) {
alt p {
none. { cont; }
some(_p) {
if c.test {
test_one_crate(c, path, cf, _p);
}
install_one_crate(c, path, cf, _p);
}
}
Expand Down Expand Up @@ -530,13 +556,20 @@ fn cmd_install(c: cargo, argv: [str]) {
ret;
}

let target = argv[2];
// TODO: getopts
if vec::len(argv) > 3u && argv[2] == "--test" {
c.test = true;
target = argv[3];
}

let wd = alt tempfile::mkdtemp(c.workdir + fs::path_sep(), "") {
some(_wd) { _wd }
none. { fail "needed temp dir"; }
};

if str::starts_with(argv[2], "uuid:") {
let uuid = rest(argv[2], 5u);
if str::starts_with(target, "uuid:") {
let uuid = rest(target, 5u);
let idx = str::index(uuid, '/' as u8);
if idx != -1 {
let source = str::slice(uuid, 0u, idx as uint);
Expand All @@ -546,7 +579,7 @@ fn cmd_install(c: cargo, argv: [str]) {
install_uuid(c, wd, uuid);
}
} else {
let name = argv[2];
let name = target;
let idx = str::index(name, '/' as u8);
if idx != -1 {
let source = str::slice(name, 0u, idx as uint);
Expand Down Expand Up @@ -686,13 +719,13 @@ fn cmd_search(c: cargo, argv: [str]) {

fn cmd_usage() {
print("Usage: cargo <verb> [args...]");
print(" init Fetch default sources");
print(" install [source/]package-name Install by name");
print(" install uuid:[source/]package-uuid Install by uuid");
print(" list [source] List packages");
print(" search <name | '*'> [tags...] Search packages");
print(" sync Sync all sources");
print(" usage This");
print(" init Fetch default sources");
print(" install [--test] [source/]package-name Install by name");
print(" install [--test] uuid:[source/]package-uuid Install by uuid");
print(" list [source] List packages");
print(" search <name | '*'> [tags...] Search packages");
print(" sync Sync all sources");
print(" usage This");
}

fn main(argv: [str]) {
Expand Down

0 comments on commit ad0065f

Please sign in to comment.