Skip to content

Commit 884c7c9

Browse files
committed
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<Path>.
1 parent 74fee15 commit 884c7c9

File tree

5 files changed

+138
-63
lines changed

5 files changed

+138
-63
lines changed

src/librustpkg/conditions.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
// Useful conditions
12+
13+
pub use core::path::Path;
14+
15+
condition! {
16+
bad_path: (super::Path, ~str) -> super::Path;
17+
}

src/librustpkg/path_util.rs

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 paths and directories
12+
13+
use core::path::*;
14+
use core::os;
15+
use util::PkgId;
16+
17+
/// Returns the output directory to use.
18+
/// Right now is always the default, should
19+
/// support changing it.
20+
pub fn dest_dir(pkgid: PkgId) -> Path {
21+
default_dest_dir(&pkgid.path)
22+
}
23+
24+
/// Returns the default output directory for compilation.
25+
/// Creates that directory if it doesn't exist.
26+
pub fn default_dest_dir(pkg_dir: &Path) -> Path {
27+
use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
28+
use conditions::bad_path::cond;
29+
30+
// For now: assumes that pkg_dir exists and is relative
31+
// to the CWD. Change this later when we do path searching.
32+
let rslt = pkg_dir.push("build");
33+
let is_dir = os::path_is_dir(&rslt);
34+
if os::path_exists(&rslt) {
35+
if is_dir {
36+
rslt
37+
}
38+
else {
39+
cond.raise((rslt, ~"Path names a file that isn't a directory"))
40+
}
41+
}
42+
else {
43+
// Create it
44+
if os::make_dir(&rslt, (S_IRUSR | S_IWUSR | S_IXUSR) as i32) {
45+
rslt
46+
}
47+
else {
48+
cond.raise((rslt, ~"Could not create directory"))
49+
}
50+
}
51+
}
52+
53+
#[cfg(test)]
54+
mod test {
55+
use core::{os, rand};
56+
use core::path::Path;
57+
use core::rand::RngUtil;
58+
use path_util::*;
59+
60+
// Helper function to create a directory name that doesn't exist
61+
pub fn mk_nonexistent(tmpdir: &Path, suffix: &str) -> Path {
62+
let r = rand::Rng();
63+
for 1000.times {
64+
let p = tmpdir.push(r.gen_str(16) + suffix);
65+
if !os::path_exists(&p) {
66+
return p;
67+
}
68+
}
69+
fail!(~"Couldn't compute a non-existent path name; this is worrisome")
70+
}
71+
72+
#[test]
73+
fn default_dir_ok() {
74+
let the_path = os::tmpdir();
75+
let substitute_path = Path("xyzzy");
76+
assert!(default_dest_dir(&the_path) == the_path.push(~"build"));
77+
let nonexistent_path = mk_nonexistent(&the_path, "quux");
78+
let bogus = do ::conditions::bad_path::cond.trap(|_| {
79+
substitute_path
80+
}).in { default_dest_dir(&nonexistent_path) };
81+
assert!(bogus == substitute_path);
82+
}
83+
}

src/librustpkg/rustpkg.rc

+5-46
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// rustpkg - a purely function package manager and build system
11+
// rustpkg - a package manager and build system for Rust
1212

1313
#[link(name = "rustpkg",
1414
vers = "0.6",
@@ -37,8 +37,11 @@ use std::net::url;
3737
use std::{getopts};
3838
use syntax::{ast, diagnostic};
3939
use util::{ExitCode, Pkg, PkgId};
40+
use path_util::dest_dir;
4041

42+
mod conditions;
4143
mod usage;
44+
mod path_util;
4245
mod util;
4346

4447
/// A PkgScript represents user-supplied custom logic for
@@ -155,46 +158,6 @@ struct Ctx {
155158
dep_cache: @mut HashMap<~str, bool>,
156159
}
157160

158-
159-
/// Returns the output directory to use.
160-
/// Right now is always the default, should
161-
/// support changing it.
162-
fn dest_dir(pkgid: PkgId) -> Path {
163-
default_dest_dir(&pkgid.path).expect(
164-
~"couldn't make default dir?!")
165-
}
166-
167-
/// Returns the default output directory for compilation.
168-
/// Creates that directory if it doesn't exist.
169-
fn default_dest_dir(pkg_dir: &Path) -> Option<Path> {
170-
use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
171-
172-
// For now: assumes that pkg_dir exists and is relative
173-
// to the CWD. Change this later when we do path searching.
174-
let rslt = pkg_dir.push("build");
175-
let is_dir = os::path_is_dir(&rslt);
176-
if os::path_exists(&rslt) {
177-
if is_dir {
178-
Some(rslt)
179-
}
180-
else {
181-
util::error(fmt!("%s is not a directory", rslt.to_str()));
182-
None
183-
}
184-
}
185-
else {
186-
// Create it
187-
if os::make_dir(&rslt, (S_IRUSR | S_IWUSR | S_IXUSR) as i32) {
188-
Some(rslt)
189-
}
190-
else {
191-
util::error(fmt!("Could not create directory %s",
192-
rslt.to_str()));
193-
None // ??? should probably use conditions
194-
}
195-
}
196-
}
197-
198161
impl Ctx {
199162

200163
fn run(&self, cmd: ~str, args: ~[~str]) {
@@ -759,10 +722,6 @@ pub struct PkgSrc {
759722
benchs: ~[Crate],
760723
}
761724

762-
condition! {
763-
bad_path: (super::Path, ~str) -> super::Path;
764-
}
765-
766725
condition! {
767726
build_err: (~str) -> ();
768727
}
@@ -785,7 +744,7 @@ impl PkgSrc {
785744

786745

787746
fn check_dir(&self) -> Path {
788-
use bad_path::cond;
747+
use conditions::bad_path::cond;
789748

790749
debug!("Pushing onto root: %s | %s", self.id.path.to_str(),
791750
self.root.to_str());

src/librustpkg/tests.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 unit tests

src/librustpkg/util.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -563,21 +563,26 @@ pub fn link_exe(src: &Path, dest: &Path) -> bool {
563563
}
564564
}
565565
566-
#[test]
567-
fn test_is_cmd() {
568-
assert!(is_cmd(~"build"));
569-
assert!(is_cmd(~"clean"));
570-
assert!(is_cmd(~"do"));
571-
assert!(is_cmd(~"info"));
572-
assert!(is_cmd(~"install"));
573-
assert!(is_cmd(~"prefer"));
574-
assert!(is_cmd(~"test"));
575-
assert!(is_cmd(~"uninstall"));
576-
assert!(is_cmd(~"unprefer"));
577-
}
578-
579-
#[test]
580-
fn test_parse_name() {
581-
assert!(parse_name(~"org.mozilla.servo").get() == ~"servo");
582-
assert!(parse_name(~"org. mozilla.servo 2131").is_err());
566+
#[cfg(test)]
567+
mod test {
568+
use super::{is_cmd, parse_name};
569+
570+
#[test]
571+
fn test_is_cmd() {
572+
assert!(is_cmd(~"build"));
573+
assert!(is_cmd(~"clean"));
574+
assert!(is_cmd(~"do"));
575+
assert!(is_cmd(~"info"));
576+
assert!(is_cmd(~"install"));
577+
assert!(is_cmd(~"prefer"));
578+
assert!(is_cmd(~"test"));
579+
assert!(is_cmd(~"uninstall"));
580+
assert!(is_cmd(~"unprefer"));
581+
}
582+
583+
#[test]
584+
fn test_parse_name() {
585+
assert!(parse_name(~"org.mozilla.servo").get() == ~"servo");
586+
assert!(parse_name(~"org. mozilla.servo 2131").is_err());
587+
}
583588
}

0 commit comments

Comments
 (0)