From 884c7c9326a2f6042b4343d41c4fa10fa7740ee2 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Fri, 12 Apr 2013 16:15:40 -0700 Subject: [PATCH 1/2] rustpkg: Factor out tests; use a condition instead of returning an option Pulled out tests into their own modules inside the files they test, as per the draft style guidelines. Started a new module, path_util, for utility functions to do with paths and directories. Changed default_dest_dir to use a condition and return Path instead of Option. --- src/librustpkg/conditions.rs | 17 ++++++++ src/librustpkg/path_util.rs | 83 ++++++++++++++++++++++++++++++++++++ src/librustpkg/rustpkg.rc | 51 +++------------------- src/librustpkg/tests.rs | 11 +++++ src/librustpkg/util.rs | 39 +++++++++-------- 5 files changed, 138 insertions(+), 63 deletions(-) create mode 100644 src/librustpkg/conditions.rs create mode 100644 src/librustpkg/path_util.rs create mode 100644 src/librustpkg/tests.rs diff --git a/src/librustpkg/conditions.rs b/src/librustpkg/conditions.rs new file mode 100644 index 0000000000000..353995a816e37 --- /dev/null +++ b/src/librustpkg/conditions.rs @@ -0,0 +1,17 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Useful conditions + +pub use core::path::Path; + +condition! { + bad_path: (super::Path, ~str) -> super::Path; +} diff --git a/src/librustpkg/path_util.rs b/src/librustpkg/path_util.rs new file mode 100644 index 0000000000000..c77106a9a39cb --- /dev/null +++ b/src/librustpkg/path_util.rs @@ -0,0 +1,83 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// rustpkg utilities having to do with paths and directories + +use core::path::*; +use core::os; +use util::PkgId; + +/// Returns the output directory to use. +/// Right now is always the default, should +/// support changing it. +pub fn dest_dir(pkgid: PkgId) -> Path { + default_dest_dir(&pkgid.path) +} + +/// Returns the default output directory for compilation. +/// Creates that directory if it doesn't exist. +pub fn default_dest_dir(pkg_dir: &Path) -> Path { + use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR}; + use conditions::bad_path::cond; + + // For now: assumes that pkg_dir exists and is relative + // to the CWD. Change this later when we do path searching. + let rslt = pkg_dir.push("build"); + let is_dir = os::path_is_dir(&rslt); + if os::path_exists(&rslt) { + if is_dir { + rslt + } + else { + cond.raise((rslt, ~"Path names a file that isn't a directory")) + } + } + else { + // Create it + if os::make_dir(&rslt, (S_IRUSR | S_IWUSR | S_IXUSR) as i32) { + rslt + } + else { + cond.raise((rslt, ~"Could not create directory")) + } + } +} + +#[cfg(test)] +mod test { + use core::{os, rand}; + use core::path::Path; + use core::rand::RngUtil; + use path_util::*; + + // Helper function to create a directory name that doesn't exist + pub fn mk_nonexistent(tmpdir: &Path, suffix: &str) -> Path { + let r = rand::Rng(); + for 1000.times { + let p = tmpdir.push(r.gen_str(16) + suffix); + if !os::path_exists(&p) { + return p; + } + } + fail!(~"Couldn't compute a non-existent path name; this is worrisome") + } + + #[test] + fn default_dir_ok() { + let the_path = os::tmpdir(); + let substitute_path = Path("xyzzy"); + assert!(default_dest_dir(&the_path) == the_path.push(~"build")); + let nonexistent_path = mk_nonexistent(&the_path, "quux"); + let bogus = do ::conditions::bad_path::cond.trap(|_| { + substitute_path + }).in { default_dest_dir(&nonexistent_path) }; + assert!(bogus == substitute_path); + } +} \ No newline at end of file diff --git a/src/librustpkg/rustpkg.rc b/src/librustpkg/rustpkg.rc index 399fcec680401..d61fe30992d85 100644 --- a/src/librustpkg/rustpkg.rc +++ b/src/librustpkg/rustpkg.rc @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// rustpkg - a purely function package manager and build system +// rustpkg - a package manager and build system for Rust #[link(name = "rustpkg", vers = "0.6", @@ -37,8 +37,11 @@ use std::net::url; use std::{getopts}; use syntax::{ast, diagnostic}; use util::{ExitCode, Pkg, PkgId}; +use path_util::dest_dir; +mod conditions; mod usage; +mod path_util; mod util; /// A PkgScript represents user-supplied custom logic for @@ -155,46 +158,6 @@ struct Ctx { dep_cache: @mut HashMap<~str, bool>, } - -/// Returns the output directory to use. -/// Right now is always the default, should -/// support changing it. -fn dest_dir(pkgid: PkgId) -> Path { - default_dest_dir(&pkgid.path).expect( - ~"couldn't make default dir?!") -} - -/// Returns the default output directory for compilation. -/// Creates that directory if it doesn't exist. -fn default_dest_dir(pkg_dir: &Path) -> Option { - use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR}; - - // For now: assumes that pkg_dir exists and is relative - // to the CWD. Change this later when we do path searching. - let rslt = pkg_dir.push("build"); - let is_dir = os::path_is_dir(&rslt); - if os::path_exists(&rslt) { - if is_dir { - Some(rslt) - } - else { - util::error(fmt!("%s is not a directory", rslt.to_str())); - None - } - } - else { - // Create it - if os::make_dir(&rslt, (S_IRUSR | S_IWUSR | S_IXUSR) as i32) { - Some(rslt) - } - else { - util::error(fmt!("Could not create directory %s", - rslt.to_str())); - None // ??? should probably use conditions - } - } -} - impl Ctx { fn run(&self, cmd: ~str, args: ~[~str]) { @@ -759,10 +722,6 @@ pub struct PkgSrc { benchs: ~[Crate], } -condition! { - bad_path: (super::Path, ~str) -> super::Path; -} - condition! { build_err: (~str) -> (); } @@ -785,7 +744,7 @@ impl PkgSrc { fn check_dir(&self) -> Path { - use bad_path::cond; + use conditions::bad_path::cond; debug!("Pushing onto root: %s | %s", self.id.path.to_str(), self.root.to_str()); diff --git a/src/librustpkg/tests.rs b/src/librustpkg/tests.rs new file mode 100644 index 0000000000000..f594860607255 --- /dev/null +++ b/src/librustpkg/tests.rs @@ -0,0 +1,11 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// rustpkg unit tests diff --git a/src/librustpkg/util.rs b/src/librustpkg/util.rs index 819823e87447c..602c71cf8be96 100644 --- a/src/librustpkg/util.rs +++ b/src/librustpkg/util.rs @@ -563,21 +563,26 @@ pub fn link_exe(src: &Path, dest: &Path) -> bool { } } -#[test] -fn test_is_cmd() { - assert!(is_cmd(~"build")); - assert!(is_cmd(~"clean")); - assert!(is_cmd(~"do")); - assert!(is_cmd(~"info")); - assert!(is_cmd(~"install")); - assert!(is_cmd(~"prefer")); - assert!(is_cmd(~"test")); - assert!(is_cmd(~"uninstall")); - assert!(is_cmd(~"unprefer")); -} - -#[test] -fn test_parse_name() { - assert!(parse_name(~"org.mozilla.servo").get() == ~"servo"); - assert!(parse_name(~"org. mozilla.servo 2131").is_err()); +#[cfg(test)] +mod test { + use super::{is_cmd, parse_name}; + + #[test] + fn test_is_cmd() { + assert!(is_cmd(~"build")); + assert!(is_cmd(~"clean")); + assert!(is_cmd(~"do")); + assert!(is_cmd(~"info")); + assert!(is_cmd(~"install")); + assert!(is_cmd(~"prefer")); + assert!(is_cmd(~"test")); + assert!(is_cmd(~"uninstall")); + assert!(is_cmd(~"unprefer")); + } + + #[test] + fn test_parse_name() { + assert!(parse_name(~"org.mozilla.servo").get() == ~"servo"); + assert!(parse_name(~"org. mozilla.servo 2131").is_err()); + } } From 7881bde0271ed4dd68e2a286ed456c2578e70d6a Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Mon, 15 Apr 2013 11:52:10 -0700 Subject: [PATCH 2/2] Tidy --- src/librustpkg/path_util.rs | 4 ++-- src/librustpkg/util.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustpkg/path_util.rs b/src/librustpkg/path_util.rs index c77106a9a39cb..c821281184d3b 100644 --- a/src/librustpkg/path_util.rs +++ b/src/librustpkg/path_util.rs @@ -75,9 +75,9 @@ mod test { let substitute_path = Path("xyzzy"); assert!(default_dest_dir(&the_path) == the_path.push(~"build")); let nonexistent_path = mk_nonexistent(&the_path, "quux"); - let bogus = do ::conditions::bad_path::cond.trap(|_| { + let bogus = do ::conditions::bad_path::cond.trap(|_| { substitute_path }).in { default_dest_dir(&nonexistent_path) }; assert!(bogus == substitute_path); } -} \ No newline at end of file +} diff --git a/src/librustpkg/util.rs b/src/librustpkg/util.rs index 602c71cf8be96..4c38a177e7295 100644 --- a/src/librustpkg/util.rs +++ b/src/librustpkg/util.rs @@ -579,7 +579,7 @@ mod test { assert!(is_cmd(~"uninstall")); assert!(is_cmd(~"unprefer")); } - + #[test] fn test_parse_name() { assert!(parse_name(~"org.mozilla.servo").get() == ~"servo");