Skip to content

Commit 4e2c8f4

Browse files
committed
rustpkg: Preliminary work on install command
Mostly just tests (that are ignored); install command is still stubbed out.
1 parent f945e57 commit 4e2c8f4

File tree

6 files changed

+175
-30
lines changed

6 files changed

+175
-30
lines changed

src/librustpkg/conditions.rs

+5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
// Useful conditions
1212

1313
pub use core::path::Path;
14+
pub use util::PkgId;
1415

1516
condition! {
1617
bad_path: (super::Path, ~str) -> super::Path;
1718
}
19+
20+
condition! {
21+
nonexistent_package: (super::PkgId, ~str) -> super::Path;
22+
}

src/librustpkg/context.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Context data structure used by rustpkg
12+
13+
use core::hashmap::HashMap;
14+
15+
pub struct Ctx {
16+
// I'm not sure what this is for
17+
json: bool,
18+
// Cache of hashes of things already installed
19+
// though I'm not sure why the value is a bool
20+
dep_cache: @mut HashMap<~str, bool>,
21+
}

src/librustpkg/rustpkg.rc

+21-29
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@ use rustc::metadata::filesearch;
3636
use std::{getopts};
3737
use syntax::{ast, diagnostic};
3838
use util::*;
39-
use path_util::{normalize, workspace_contains_package_id};
40-
use path_util::{build_pkg_id_in_workspace, pkgid_src_in_workspace, rust_path};
39+
use path_util::normalize;
40+
use path_util::{build_pkg_id_in_workspace, pkgid_src_in_workspace};
41+
use workspace::pkg_parent_workspaces;
4142
use rustc::driver::session::{lib_crate, bin_crate, crate_type};
43+
use context::Ctx;
4244

4345
mod conditions;
46+
mod context;
4447
mod usage;
4548
mod path_util;
49+
mod tests;
4650
mod util;
51+
mod workspace;
4752

4853
/// A PkgScript represents user-supplied custom logic for
4954
/// special build hooks. This only exists for packages with
@@ -154,14 +159,6 @@ impl PkgScript {
154159

155160
}
156161

157-
struct Ctx {
158-
// I'm not sure what this is for
159-
json: bool,
160-
// Cache of hashes of things already installed
161-
// though I'm not sure why the value is a bool
162-
dep_cache: @mut HashMap<~str, bool>,
163-
}
164-
165162
impl Ctx {
166163

167164
fn run(&self, cmd: ~str, args: ~[~str]) {
@@ -194,17 +191,7 @@ impl Ctx {
194191
// The package id is presumed to be the first command-line
195192
// argument
196193
let pkgid = PkgId::new(args[0]);
197-
// Using the RUST_PATH, find workspaces that contain
198-
// this package ID
199-
let workspaces = rust_path().filtered(|ws|
200-
workspace_contains_package_id(pkgid, ws));
201-
if workspaces.is_empty() {
202-
fail!(fmt!("Package %s not found in any of \
203-
the following workspaces: %s",
204-
pkgid.path.to_str(),
205-
rust_path().to_str()));
206-
}
207-
for workspaces.each |workspace| {
194+
for pkg_parent_workspaces(pkgid) |workspace| {
208195
let src_dir = pkgid_src_in_workspace(pkgid, workspace);
209196
let build_dir = build_pkg_id_in_workspace(pkgid, workspace);
210197
debug!("Destination dir = %s", build_dir.to_str());
@@ -271,10 +258,16 @@ impl Ctx {
271258
self.info();
272259
}
273260
~"install" => {
274-
self.install(if args.len() >= 1 { Some(args[0]) }
275-
else { None },
276-
if args.len() >= 2 { Some(args[1]) }
277-
else { None }, false);
261+
if args.len() < 1 {
262+
return usage::install();
263+
}
264+
265+
// The package id is presumed to be the first command-line
266+
// argument
267+
let pkgid = PkgId::new(args[0]);
268+
for pkg_parent_workspaces(pkgid) |workspace| {
269+
self.install(workspace, pkgid);
270+
}
278271
}
279272
~"prefer" => {
280273
if args.len() < 1 {
@@ -310,9 +303,9 @@ impl Ctx {
310303
}
311304
}
312305

313-
fn do_cmd(&self, cmd: ~str, pkgname: ~str) {
306+
fn do_cmd(&self, _cmd: ~str, _pkgname: ~str) {
314307
// stub
315-
fail!("`do` not yet implemented");
308+
fail!(~"`do` not yet implemented");
316309
}
317310

318311
fn clean(&self, workspace: &Path, id: PkgId) {
@@ -336,8 +329,7 @@ impl Ctx {
336329
fail!(~"info not yet implemented");
337330
}
338331

339-
fn install(&self, _url: Option<~str>,
340-
_target: Option<~str>, _cache: bool) {
332+
fn install(&self, _workspace: &Path, _id: PkgId) {
341333
// stub
342334
fail!(~"install not yet implemented");
343335
}

src/librustpkg/tests.rs

+93
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,96 @@
99
// except according to those terms.
1010

1111
// rustpkg unit tests
12+
13+
use context::Ctx;
14+
use core::hashmap::HashMap;
15+
use core::path::Path;
16+
use core::os;
17+
use core::io;
18+
use core::option::*;
19+
use std::tempfile::mkdtemp;
20+
use util::{PkgId, default_version};
21+
use path_util::{target_executable_in_workspace, target_library_in_workspace,
22+
target_test_in_workspace, target_bench_in_workspace,
23+
make_dir_rwx};
24+
25+
fn fake_ctxt() -> Ctx {
26+
Ctx {
27+
json: false,
28+
dep_cache: @mut HashMap::new()
29+
}
30+
}
31+
32+
fn fake_pkg() -> PkgId {
33+
PkgId {
34+
path: Path(~"bogus"),
35+
version: default_version()
36+
}
37+
}
38+
39+
fn mk_temp_workspace() -> Path {
40+
mkdtemp(&os::tmpdir(), "test").expect("couldn't create temp dir")
41+
}
42+
43+
fn is_rwx(p: &Path) -> bool {
44+
use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
45+
46+
match p.get_mode() {
47+
None => return false,
48+
Some(m) => {
49+
((m & S_IRUSR as uint) == S_IRUSR as uint
50+
&& (m & S_IWUSR as uint) == S_IWUSR as uint
51+
&& (m & S_IXUSR as uint) == S_IXUSR as uint)
52+
}
53+
}
54+
}
55+
56+
#[test]
57+
fn test_make_dir_rwx() {
58+
let temp = &os::tmpdir();
59+
let dir = temp.push(~"quux");
60+
let _ = os::remove_dir(&dir);
61+
assert!(make_dir_rwx(&dir));
62+
assert!(os::path_is_dir(&dir));
63+
assert!(is_rwx(&dir));
64+
assert!(os::remove_dir(&dir));
65+
}
66+
67+
#[test]
68+
#[ignore(reason = "install not yet implemented")]
69+
fn test_install_valid() {
70+
let ctxt = fake_ctxt();
71+
let temp_pkg_id = fake_pkg();
72+
let temp_workspace() = mk_temp_workspace();
73+
// should have test, bench, lib, and main
74+
ctxt.install(&temp_workspace, temp_pkg_id);
75+
// Check that all files exist
76+
let exec = target_executable_in_workspace(temp_pkg_id, &temp_workspace);
77+
assert!(os::path_exists(&exec));
78+
assert!(is_rwx(&exec));
79+
let lib = target_library_in_workspace(temp_pkg_id, &temp_workspace);
80+
assert!(os::path_exists(&lib));
81+
assert!(is_rwx(&lib));
82+
// And that the test and bench executables aren't installed
83+
assert!(!os::path_exists(&target_test_in_workspace(temp_pkg_id, &temp_workspace)));
84+
assert!(!os::path_exists(&target_bench_in_workspace(temp_pkg_id, &temp_workspace)));
85+
}
86+
87+
#[test]
88+
#[ignore(reason = "install not yet implemented")]
89+
fn test_install_invalid() {
90+
use conditions::nonexistent_package::cond;
91+
92+
let ctxt = fake_ctxt();
93+
let pkgid = fake_pkg();
94+
let temp_workspace = mk_temp_workspace();
95+
let expected_path = Path(~"quux");
96+
let substituted: Path = do cond.trap(|_| {
97+
expected_path
98+
}).in {
99+
ctxt.install(&temp_workspace, pkgid);
100+
// ok
101+
fail!(~"test_install_invalid failed, should have raised a condition");
102+
};
103+
assert!(substituted == expected_path);
104+
}

src/librustpkg/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl ToStr for Version {
7878
}
7979

8080
/// Placeholder
81-
fn default_version() -> Version { ExactRevision(0.1) }
81+
pub fn default_version() -> Version { ExactRevision(0.1) }
8282

8383
// Path-fragment identifier of a package such as
8484
// 'github.com/graydon/test'; path must be a relative

src/librustpkg/workspace.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// rustpkg utilities having to do with workspaces
12+
13+
use path_util::{rust_path, workspace_contains_package_id};
14+
use util::PkgId;
15+
use core::path::Path;
16+
17+
pub fn pkg_parent_workspaces(pkgid: PkgId, action: &fn(&Path) -> bool) {
18+
// Using the RUST_PATH, find workspaces that contain
19+
// this package ID
20+
let workspaces = rust_path().filtered(|ws|
21+
workspace_contains_package_id(pkgid, ws));
22+
if workspaces.is_empty() {
23+
// tjc: make this a condition
24+
fail!(fmt!("Package %s not found in any of \
25+
the following workspaces: %s",
26+
pkgid.path.to_str(),
27+
rust_path().to_str()));
28+
}
29+
for workspaces.each |ws| {
30+
if action(ws) {
31+
break;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)