Skip to content

Commit

Permalink
rustpkg: Preliminary work on install command
Browse files Browse the repository at this point in the history
Mostly just tests (that are ignored); install command is still
stubbed out.
  • Loading branch information
catamorphism committed Apr 25, 2013
1 parent f945e57 commit 4e2c8f4
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 30 deletions.
5 changes: 5 additions & 0 deletions src/librustpkg/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
// Useful conditions

pub use core::path::Path;
pub use util::PkgId;

condition! {
bad_path: (super::Path, ~str) -> super::Path;
}

condition! {
nonexistent_package: (super::PkgId, ~str) -> super::Path;
}
21 changes: 21 additions & 0 deletions src/librustpkg/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Context data structure used by rustpkg

use core::hashmap::HashMap;

pub struct Ctx {
// I'm not sure what this is for
json: bool,
// Cache of hashes of things already installed
// though I'm not sure why the value is a bool
dep_cache: @mut HashMap<~str, bool>,
}
50 changes: 21 additions & 29 deletions src/librustpkg/rustpkg.rc
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ use rustc::metadata::filesearch;
use std::{getopts};
use syntax::{ast, diagnostic};
use util::*;
use path_util::{normalize, workspace_contains_package_id};
use path_util::{build_pkg_id_in_workspace, pkgid_src_in_workspace, rust_path};
use path_util::normalize;
use path_util::{build_pkg_id_in_workspace, pkgid_src_in_workspace};
use workspace::pkg_parent_workspaces;
use rustc::driver::session::{lib_crate, bin_crate, crate_type};
use context::Ctx;

mod conditions;
mod context;
mod usage;
mod path_util;
mod tests;
mod util;
mod workspace;

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

}

struct Ctx {
// I'm not sure what this is for
json: bool,
// Cache of hashes of things already installed
// though I'm not sure why the value is a bool
dep_cache: @mut HashMap<~str, bool>,
}

impl Ctx {

fn run(&self, cmd: ~str, args: ~[~str]) {
Expand Down Expand Up @@ -194,17 +191,7 @@ impl Ctx {
// The package id is presumed to be the first command-line
// argument
let pkgid = PkgId::new(args[0]);
// Using the RUST_PATH, find workspaces that contain
// this package ID
let workspaces = rust_path().filtered(|ws|
workspace_contains_package_id(pkgid, ws));
if workspaces.is_empty() {
fail!(fmt!("Package %s not found in any of \
the following workspaces: %s",
pkgid.path.to_str(),
rust_path().to_str()));
}
for workspaces.each |workspace| {
for pkg_parent_workspaces(pkgid) |workspace| {
let src_dir = pkgid_src_in_workspace(pkgid, workspace);
let build_dir = build_pkg_id_in_workspace(pkgid, workspace);
debug!("Destination dir = %s", build_dir.to_str());
Expand Down Expand Up @@ -271,10 +258,16 @@ impl Ctx {
self.info();
}
~"install" => {
self.install(if args.len() >= 1 { Some(args[0]) }
else { None },
if args.len() >= 2 { Some(args[1]) }
else { None }, false);
if args.len() < 1 {
return usage::install();
}

// The package id is presumed to be the first command-line
// argument
let pkgid = PkgId::new(args[0]);
for pkg_parent_workspaces(pkgid) |workspace| {
self.install(workspace, pkgid);
}
}
~"prefer" => {
if args.len() < 1 {
Expand Down Expand Up @@ -310,9 +303,9 @@ impl Ctx {
}
}

fn do_cmd(&self, cmd: ~str, pkgname: ~str) {
fn do_cmd(&self, _cmd: ~str, _pkgname: ~str) {
// stub
fail!("`do` not yet implemented");
fail!(~"`do` not yet implemented");
}

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

fn install(&self, _url: Option<~str>,
_target: Option<~str>, _cache: bool) {
fn install(&self, _workspace: &Path, _id: PkgId) {
// stub
fail!(~"install not yet implemented");
}
Expand Down
93 changes: 93 additions & 0 deletions src/librustpkg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,96 @@
// except according to those terms.

// rustpkg unit tests

use context::Ctx;
use core::hashmap::HashMap;
use core::path::Path;
use core::os;
use core::io;
use core::option::*;
use std::tempfile::mkdtemp;
use util::{PkgId, default_version};
use path_util::{target_executable_in_workspace, target_library_in_workspace,
target_test_in_workspace, target_bench_in_workspace,
make_dir_rwx};

fn fake_ctxt() -> Ctx {
Ctx {
json: false,
dep_cache: @mut HashMap::new()
}
}

fn fake_pkg() -> PkgId {
PkgId {
path: Path(~"bogus"),
version: default_version()
}
}

fn mk_temp_workspace() -> Path {
mkdtemp(&os::tmpdir(), "test").expect("couldn't create temp dir")
}

fn is_rwx(p: &Path) -> bool {
use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};

match p.get_mode() {
None => return false,
Some(m) => {
((m & S_IRUSR as uint) == S_IRUSR as uint
&& (m & S_IWUSR as uint) == S_IWUSR as uint
&& (m & S_IXUSR as uint) == S_IXUSR as uint)
}
}
}

#[test]
fn test_make_dir_rwx() {
let temp = &os::tmpdir();
let dir = temp.push(~"quux");
let _ = os::remove_dir(&dir);
assert!(make_dir_rwx(&dir));
assert!(os::path_is_dir(&dir));
assert!(is_rwx(&dir));
assert!(os::remove_dir(&dir));
}

#[test]
#[ignore(reason = "install not yet implemented")]
fn test_install_valid() {
let ctxt = fake_ctxt();
let temp_pkg_id = fake_pkg();
let temp_workspace() = mk_temp_workspace();
// should have test, bench, lib, and main
ctxt.install(&temp_workspace, temp_pkg_id);
// Check that all files exist
let exec = target_executable_in_workspace(temp_pkg_id, &temp_workspace);
assert!(os::path_exists(&exec));
assert!(is_rwx(&exec));
let lib = target_library_in_workspace(temp_pkg_id, &temp_workspace);
assert!(os::path_exists(&lib));
assert!(is_rwx(&lib));
// And that the test and bench executables aren't installed
assert!(!os::path_exists(&target_test_in_workspace(temp_pkg_id, &temp_workspace)));
assert!(!os::path_exists(&target_bench_in_workspace(temp_pkg_id, &temp_workspace)));
}

#[test]
#[ignore(reason = "install not yet implemented")]
fn test_install_invalid() {
use conditions::nonexistent_package::cond;

let ctxt = fake_ctxt();
let pkgid = fake_pkg();
let temp_workspace = mk_temp_workspace();
let expected_path = Path(~"quux");
let substituted: Path = do cond.trap(|_| {
expected_path
}).in {
ctxt.install(&temp_workspace, pkgid);
// ok
fail!(~"test_install_invalid failed, should have raised a condition");
};
assert!(substituted == expected_path);
}
2 changes: 1 addition & 1 deletion src/librustpkg/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl ToStr for Version {
}

/// Placeholder
fn default_version() -> Version { ExactRevision(0.1) }
pub fn default_version() -> Version { ExactRevision(0.1) }

// Path-fragment identifier of a package such as
// 'github.com/graydon/test'; path must be a relative
Expand Down
34 changes: 34 additions & 0 deletions src/librustpkg/workspace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// rustpkg utilities having to do with workspaces

use path_util::{rust_path, workspace_contains_package_id};
use util::PkgId;
use core::path::Path;

pub fn pkg_parent_workspaces(pkgid: PkgId, action: &fn(&Path) -> bool) {
// Using the RUST_PATH, find workspaces that contain
// this package ID
let workspaces = rust_path().filtered(|ws|
workspace_contains_package_id(pkgid, ws));
if workspaces.is_empty() {
// tjc: make this a condition
fail!(fmt!("Package %s not found in any of \
the following workspaces: %s",
pkgid.path.to_str(),
rust_path().to_str()));
}
for workspaces.each |ws| {
if action(ws) {

This comment has been minimized.

Copy link
@alexcrichton

alexcrichton May 3, 2013

Member

I was taking a look at changing the for loop protocols, and I ran across this. Is this supposed to be !action, or is this supposed to yield just one workspace (both use cases currently look like they only use the first workspace returned and then this breaks out)

This comment has been minimized.

Copy link
@catamorphism

catamorphism May 3, 2013

Author Contributor

Yes, my mistake. It's not caught because I don't have any tests for multiple workspaces yet, but once I add one, it'll be caught, so no need to worry... though feel free to fix it if you want to :-)

This comment has been minimized.

Copy link
@alexcrichton

alexcrichton May 3, 2013

Member

No problem! 155ab7db571800f08910e3bc6e9d935acc3f9c6b

Just wanted to make sure

break;
}
}
}

5 comments on commit 4e2c8f4

@bors
Copy link
Contributor

@bors bors commented on 4e2c8f4 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from graydon
at catamorphism@4e2c8f4

@bors
Copy link
Contributor

@bors bors commented on 4e2c8f4 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging catamorphism/rust/rustpkg = 4e2c8f4 into auto

@bors
Copy link
Contributor

@bors bors commented on 4e2c8f4 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catamorphism/rust/rustpkg = 4e2c8f4 merged ok, testing candidate = 0604468

@bors
Copy link
Contributor

@bors bors commented on 4e2c8f4 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 4e2c8f4 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = 0604468

Please sign in to comment.