Skip to content

Commit a6c04ea

Browse files
committed
Merge pull request #1562 from elly/cargo
[cargo] add --test flag to install
2 parents 6a6aec0 + ad0065f commit a6c04ea

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

src/cargo/cargo.rs

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ type cargo = {
5454
libdir: str,
5555
workdir: str,
5656
sourcedir: str,
57-
sources: map::hashmap<str, source>
57+
sources: map::hashmap<str, source>,
58+
mutable test: bool
5859
};
5960

6061
type pkg = {
@@ -67,7 +68,7 @@ type pkg = {
6768
};
6869

6970
fn info(msg: str) {
70-
io::stdout().write_line(msg);
71+
io::stdout().write_line("info: " + msg);
7172
}
7273

7374
fn warn(msg: str) {
@@ -323,7 +324,8 @@ fn configure() -> cargo {
323324
libdir: fs::connect(p, "lib"),
324325
workdir: fs::connect(p, "work"),
325326
sourcedir: fs::connect(p, "sources"),
326-
sources: sources
327+
sources: sources,
328+
mutable test: false
327329
};
328330

329331
need_dir(c.root);
@@ -353,15 +355,16 @@ fn for_each_package(c: cargo, b: block(source, package)) {
353355
})
354356
}
355357

356-
fn install_one_crate(c: cargo, _path: str, cf: str, _p: pkg) {
358+
// FIXME: deduplicate code with install_one_crate
359+
fn test_one_crate(c: cargo, _path: str, cf: str, _p: pkg) {
357360
let name = fs::basename(cf);
358361
let ri = str::index(name, '.' as u8);
359362
if ri != -1 {
360363
name = str::slice(name, 0u, ri as uint);
361364
}
362365
#debug("Installing: %s", name);
363366
let old = fs::list_dir(".");
364-
let p = run::program_output("rustc", [name + ".rc"]);
367+
let p = run::program_output("rustc", ["--test", name + ".rc"]);
365368
if p.status != 0 {
366369
error(#fmt["rustc failed: %d\n%s\n%s", p.status, p.err, p.out]);
367370
ret;
@@ -371,6 +374,26 @@ fn install_one_crate(c: cargo, _path: str, cf: str, _p: pkg) {
371374
vec::filter::<str>(new, { |n| !vec::member::<str>(n, old) });
372375
let exec_suffix = os::exec_suffix();
373376
for ct: str in created {
377+
if (exec_suffix != "" && str::ends_with(ct, exec_suffix)) ||
378+
(exec_suffix == "" && !str::starts_with(ct, "./lib")) {
379+
// FIXME: need libstd fs::copy or something
380+
run::run_program(ct, []);
381+
}
382+
}
383+
}
384+
385+
fn install_one_crate(c: cargo, _path: str, cf: str, _p: pkg) {
386+
let buildpath = fs::connect(_path, "/build");
387+
need_dir(buildpath);
388+
#debug("Installing: %s -> %s", cf, buildpath);
389+
let p = run::program_output("rustc", ["--out-dir", buildpath, cf]);
390+
if p.status != 0 {
391+
error(#fmt["rustc failed: %d\n%s\n%s", p.status, p.err, p.out]);
392+
ret;
393+
}
394+
let new = fs::list_dir(buildpath);
395+
let exec_suffix = os::exec_suffix();
396+
for ct: str in new {
374397
if (exec_suffix != "" && str::ends_with(ct, exec_suffix)) ||
375398
(exec_suffix == "" && !str::starts_with(ct, "./lib")) {
376399
#debug(" bin: %s", ct);
@@ -402,6 +425,9 @@ fn install_source(c: cargo, path: str) {
402425
alt p {
403426
none. { cont; }
404427
some(_p) {
428+
if c.test {
429+
test_one_crate(c, path, cf, _p);
430+
}
405431
install_one_crate(c, path, cf, _p);
406432
}
407433
}
@@ -530,13 +556,20 @@ fn cmd_install(c: cargo, argv: [str]) {
530556
ret;
531557
}
532558

559+
let target = argv[2];
560+
// TODO: getopts
561+
if vec::len(argv) > 3u && argv[2] == "--test" {
562+
c.test = true;
563+
target = argv[3];
564+
}
565+
533566
let wd = alt tempfile::mkdtemp(c.workdir + fs::path_sep(), "") {
534567
some(_wd) { _wd }
535568
none. { fail "needed temp dir"; }
536569
};
537570

538-
if str::starts_with(argv[2], "uuid:") {
539-
let uuid = rest(argv[2], 5u);
571+
if str::starts_with(target, "uuid:") {
572+
let uuid = rest(target, 5u);
540573
let idx = str::index(uuid, '/' as u8);
541574
if idx != -1 {
542575
let source = str::slice(uuid, 0u, idx as uint);
@@ -546,7 +579,7 @@ fn cmd_install(c: cargo, argv: [str]) {
546579
install_uuid(c, wd, uuid);
547580
}
548581
} else {
549-
let name = argv[2];
582+
let name = target;
550583
let idx = str::index(name, '/' as u8);
551584
if idx != -1 {
552585
let source = str::slice(name, 0u, idx as uint);
@@ -686,13 +719,13 @@ fn cmd_search(c: cargo, argv: [str]) {
686719

687720
fn cmd_usage() {
688721
print("Usage: cargo <verb> [args...]");
689-
print(" init Fetch default sources");
690-
print(" install [source/]package-name Install by name");
691-
print(" install uuid:[source/]package-uuid Install by uuid");
692-
print(" list [source] List packages");
693-
print(" search <name | '*'> [tags...] Search packages");
694-
print(" sync Sync all sources");
695-
print(" usage This");
722+
print(" init Fetch default sources");
723+
print(" install [--test] [source/]package-name Install by name");
724+
print(" install [--test] uuid:[source/]package-uuid Install by uuid");
725+
print(" list [source] List packages");
726+
print(" search <name | '*'> [tags...] Search packages");
727+
print(" sync Sync all sources");
728+
print(" usage This");
696729
}
697730

698731
fn main(argv: [str]) {

0 commit comments

Comments
 (0)