Skip to content

Commit e3c4104

Browse files
committed
rustpkg: Write more automated tests
Automate more tests described in the commands.txt file, and add infrastructure for running them. Right now, tests shell out to call rustpkg. This is not ideal.
1 parent 1adbb45 commit e3c4104

File tree

10 files changed

+558
-120
lines changed

10 files changed

+558
-120
lines changed

src/librustpkg/messages.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
use extra::term;
12+
use core::io;
13+
use core::result::*;
14+
15+
pub fn note(msg: &str) {
16+
pretty_message(msg, "note: ", term::color_green, io::stdout())
17+
}
18+
19+
pub fn warn(msg: &str) {
20+
pretty_message(msg, "warning: ", term::color_yellow, io::stdout())
21+
}
22+
23+
pub fn error(msg: &str) {
24+
pretty_message(msg, "error: ", term::color_red, io::stdout())
25+
}
26+
27+
fn pretty_message<'a>(msg: &'a str, prefix: &'a str, color: u8, out: @io::Writer) {
28+
let term = term::Terminal::new(out);
29+
match term {
30+
Ok(ref t) => {
31+
t.fg(color);
32+
out.write_str(prefix);
33+
t.reset();
34+
},
35+
_ => {
36+
out.write_str(prefix);
37+
}
38+
}
39+
out.write_line(msg);
40+
}

src/librustpkg/package_id.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ impl PkgId {
6969
}
7070
};
7171

72+
debug!("local_path = %s, remote_path = %s", local_path.to_str(), remote_path.to_str());
7273
PkgId {
7374
local_path: local_path,
7475
remote_path: remote_path,
@@ -90,11 +91,7 @@ impl PkgId {
9091

9192
impl ToStr for PkgId {
9293
fn to_str(&self) -> ~str {
93-
let maybe_dash = match self.version {
94-
NoVersion => "",
95-
_ => "-"
96-
};
9794
// should probably use the filestem and not the whole path
98-
fmt!("%s%s%s", self.local_path.to_str(), maybe_dash, self.version.to_str())
95+
fmt!("%s-%s", self.local_path.to_str(), self.version.to_str())
9996
}
10097
}

src/librustpkg/package_source.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use core::option::*;
1515
use core::{os, run, str, vec};
1616
use context::*;
1717
use crate::Crate;
18+
use messages::*;
1819
use path_util::pkgid_src_in_workspace;
19-
use util::{compile_crate, note};
20+
use util::compile_crate;
2021
use version::{ExactRevision, SemanticVersion, NoVersion};
2122

2223
// An enumeration of the unpacked source of a package workspace.
@@ -95,7 +96,7 @@ impl PkgSrc {
9596
};
9697

9798

98-
note(fmt!("git clone %s %s %?", url, local.to_str(), branch_args));
99+
note(fmt!("Fetching package: git clone %s %s %?", url, local.to_str(), branch_args));
99100

100101
if run::process_output("git",
101102
~[~"clone", copy url, local.to_str()] + branch_args).status != 0 {

src/librustpkg/path_util.rs

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
// rustpkg utilities having to do with paths and directories
1212

1313
use core::prelude::*;
14-
pub use package_path::{RemotePath, LocalPath};
14+
pub use package_path::{RemotePath, LocalPath, normalize};
1515
pub use package_id::PkgId;
1616
pub use target::{OutputType, Main, Lib, Test, Bench, Target, Build, Install};
17+
pub use version::{Version, NoVersion, split_version_general};
1718
use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
1819
use core::os::mkdir_recursive;
1920
use core::os;
21+
use core::iterator::IteratorUtil;
22+
use messages::*;
23+
use package_id::*;
2024

2125
/// Returns the value of RUST_PATH, as a list
2226
/// of Paths. In general this should be read from the
@@ -38,8 +42,39 @@ pub fn make_dir_rwx(p: &Path) -> bool { os::make_dir(p, u_rwx) }
3842
/// True if there's a directory in <workspace> with
3943
/// pkgid's short name
4044
pub fn workspace_contains_package_id(pkgid: &PkgId, workspace: &Path) -> bool {
41-
let pkgpath = workspace.push("src").push(pkgid.remote_path.to_str());
42-
os::path_is_dir(&pkgpath)
45+
let src_dir = workspace.push("src");
46+
for os::list_dir(&src_dir).each |&p| {
47+
let p = Path(p);
48+
debug!("=> p = %s", p.to_str());
49+
if !os::path_is_dir(&src_dir.push_rel(&p)) {
50+
loop;
51+
}
52+
debug!("p = %s, remote_path = %s", p.to_str(), pkgid.remote_path.to_str());
53+
54+
if p == *pkgid.remote_path {
55+
return true;
56+
}
57+
else {
58+
let pf = p.filename();
59+
for pf.iter().advance |&pf| {
60+
let f_ = copy pf;
61+
let g = f_.to_str();
62+
match split_version_general(g, '-') {
63+
Some((ref might_match, ref vers)) => {
64+
debug!("might_match = %s, vers = %s", *might_match,
65+
vers.to_str());
66+
if *might_match == pkgid.short_name
67+
&& (*vers == pkgid.version || pkgid.version == NoVersion)
68+
{
69+
return true;
70+
}
71+
}
72+
None => ()
73+
}
74+
}
75+
}
76+
}
77+
false
4378
}
4479

4580
/// Returns a list of possible directories
@@ -114,31 +149,34 @@ fn output_in_workspace(pkgid: &PkgId, workspace: &Path, what: OutputType) -> Opt
114149
/// Figure out what the library name for <pkgid> in <workspace>'s build
115150
/// directory is, and if the file exists, return it.
116151
pub fn built_library_in_workspace(pkgid: &PkgId, workspace: &Path) -> Option<Path> {
117-
// passing in local_path here sounds fishy
118-
library_in_workspace(pkgid.local_path.to_str(), pkgid.short_name, Build,
119-
workspace, "build")
152+
library_in_workspace(&pkgid.local_path, pkgid.short_name,
153+
Build, workspace, "build")
120154
}
121155

122156
/// Does the actual searching stuff
123157
pub fn installed_library_in_workspace(short_name: &str, workspace: &Path) -> Option<Path> {
124-
library_in_workspace(short_name, short_name, Install, workspace, "lib")
158+
library_in_workspace(&normalize(RemotePath(Path(short_name))),
159+
short_name, Install, workspace, "lib")
125160
}
126161

127162

128163
/// This doesn't take a PkgId, so we can use it for `extern mod` inference, where we
129164
/// don't know the entire package ID.
130-
/// `full_name` is used to figure out the directory to search.
165+
/// `workspace` is used to figure out the directory to search.
131166
/// `short_name` is taken as the link name of the library.
132-
fn library_in_workspace(full_name: &str, short_name: &str, where: Target,
167+
pub fn library_in_workspace(path: &LocalPath, short_name: &str, where: Target,
133168
workspace: &Path, prefix: &str) -> Option<Path> {
134169
debug!("library_in_workspace: checking whether a library named %s exists",
135170
short_name);
136171

137172
// We don't know what the hash is, so we have to search through the directory
138173
// contents
139174

175+
debug!("short_name = %s where = %? workspace = %s \
176+
prefix = %s", short_name, where, workspace.to_str(), prefix);
177+
140178
let dir_to_search = match where {
141-
Build => workspace.push(prefix).push(full_name),
179+
Build => workspace.push(prefix).push_rel(&**path),
142180
Install => workspace.push(prefix)
143181
};
144182
debug!("Listing directory %s", dir_to_search.to_str());
@@ -193,7 +231,11 @@ fn library_in_workspace(full_name: &str, short_name: &str, where: Target,
193231
// Return the filename that matches, which we now know exists
194232
// (if result_filename != None)
195233
match result_filename {
196-
None => None,
234+
None => {
235+
warn(fmt!("library_in_workspace didn't find a library in %s for %s",
236+
dir_to_search.to_str(), short_name));
237+
None
238+
}
197239
Some(result_filename) => {
198240
let absolute_path = dir_to_search.push_rel(&result_filename);
199241
debug!("result_filename = %s", absolute_path.to_str());
@@ -210,17 +252,17 @@ pub fn target_executable_in_workspace(pkgid: &PkgId, workspace: &Path) -> Path {
210252
}
211253

212254

213-
/// Returns the installed path for <built_library> in <workspace>
255+
/// Returns the executable that would be installed for <pkgid>
256+
/// in <workspace>
214257
/// As a side effect, creates the lib-dir if it doesn't exist
215-
pub fn target_library_in_workspace(workspace: &Path,
216-
built_library: &Path) -> Path {
258+
pub fn target_library_in_workspace(pkgid: &PkgId, workspace: &Path) -> Path {
217259
use conditions::bad_path::cond;
218-
let result = workspace.push("lib");
219-
if !os::path_exists(&result) && !mkdir_recursive(&result, u_rwx) {
220-
cond.raise((copy result, ~"I couldn't create the library directory"));
260+
if !os::path_is_dir(workspace) {
261+
cond.raise((copy *workspace,
262+
fmt!("Workspace supplied to target_library_in_workspace \
263+
is not a directory! %s", workspace.to_str())));
221264
}
222-
result.push(built_library.filename().expect(fmt!("I don't know how to treat %s as a library",
223-
built_library.to_str())))
265+
target_file_in_workspace(pkgid, workspace, Lib, Install)
224266
}
225267

226268
/// Returns the test executable that would be installed for <pkgid>
@@ -249,7 +291,9 @@ fn target_file_in_workspace(pkgid: &PkgId, workspace: &Path,
249291
};
250292
let result = workspace.push(subdir);
251293
if !os::path_exists(&result) && !mkdir_recursive(&result, u_rwx) {
252-
cond.raise((copy result, fmt!("I couldn't create the %s dir", subdir)));
294+
cond.raise((copy result, fmt!("target_file_in_workspace couldn't \
295+
create the %s dir (pkgid=%s, workspace=%s, what=%?, where=%?",
296+
subdir, pkgid.to_str(), workspace.to_str(), what, where)));
253297
}
254298
mk_output_path(what, where, pkgid, &result)
255299
}
@@ -275,7 +319,8 @@ pub fn build_pkg_id_in_workspace(pkgid: &PkgId, workspace: &Path) -> Path {
275319
/// given whether we're building a library and whether we're building tests
276320
pub fn mk_output_path(what: OutputType, where: Target,
277321
pkg_id: &PkgId, workspace: &Path) -> Path {
278-
let short_name_with_version = pkg_id.short_name_with_version();
322+
let short_name_with_version = fmt!("%s-%s", pkg_id.short_name,
323+
pkg_id.version.to_str());
279324
// Not local_path.dir_path()! For package foo/bar/blat/, we want
280325
// the executable blat-0.5 to live under blat/
281326
let dir = match where {
@@ -291,7 +336,7 @@ pub fn mk_output_path(what: OutputType, where: Target,
291336
// this code is duplicated from elsewhere; fix this
292337
Lib => dir.push(os::dll_filename(short_name_with_version)),
293338
// executable names *aren't* versioned
294-
_ => dir.push(fmt!("%s%s%s", copy pkg_id.short_name,
339+
_ => dir.push(fmt!("%s%s%s", pkg_id.short_name,
295340
match what {
296341
Test => "test",
297342
Bench => "bench",

src/librustpkg/rustpkg.rc

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,20 @@ use rustc::metadata::filesearch;
3636
use extra::{getopts};
3737
use syntax::{ast, diagnostic};
3838
use util::*;
39+
use messages::*;
3940
use path_util::{build_pkg_id_in_workspace, first_pkgid_src_in_workspace};
40-
use path_util::u_rwx;
41+
use path_util::{u_rwx, rust_path};
4142
use path_util::{built_executable_in_workspace, built_library_in_workspace};
4243
use path_util::{target_executable_in_workspace, target_library_in_workspace};
43-
use workspace::pkg_parent_workspaces;
44+
use workspace::{each_pkg_parent_workspace, pkg_parent_workspaces};
4445
use context::Ctx;
4546
use package_id::PkgId;
4647
use package_source::PkgSrc;
4748

4849
mod conditions;
4950
mod context;
5051
mod crate;
52+
mod messages;
5153
mod package_id;
5254
mod package_path;
5355
mod package_source;
@@ -189,7 +191,7 @@ impl Ctx {
189191
// The package id is presumed to be the first command-line
190192
// argument
191193
let pkgid = PkgId::new(copy args[0]);
192-
for pkg_parent_workspaces(&pkgid) |workspace| {
194+
for each_pkg_parent_workspace(&pkgid) |workspace| {
193195
self.build(workspace, &pkgid);
194196
}
195197
}
@@ -221,8 +223,19 @@ impl Ctx {
221223
// The package id is presumed to be the first command-line
222224
// argument
223225
let pkgid = PkgId::new(args[0]);
224-
for pkg_parent_workspaces(&pkgid) |workspace| {
225-
self.install(workspace, &pkgid);
226+
let workspaces = pkg_parent_workspaces(&pkgid);
227+
if workspaces.is_empty() {
228+
let rp = rust_path();
229+
assert!(!rp.is_empty());
230+
let src = PkgSrc::new(&rp[0], &build_pkg_id_in_workspace(&pkgid, &rp[0]),
231+
&pkgid);
232+
src.fetch_git();
233+
self.install(&rp[0], &pkgid);
234+
}
235+
else {
236+
for each_pkg_parent_workspace(&pkgid) |workspace| {
237+
self.install(workspace, &pkgid);
238+
}
226239
}
227240
}
228241
"prefer" => {
@@ -259,6 +272,8 @@ impl Ctx {
259272
}
260273

261274
fn build(&self, workspace: &Path, pkgid: &PkgId) {
275+
debug!("build: workspace = %s pkgid = %s", workspace.to_str(),
276+
pkgid.to_str());
262277
let src_dir = first_pkgid_src_in_workspace(pkgid, workspace);
263278
let build_dir = build_pkg_id_in_workspace(pkgid, workspace);
264279
debug!("Destination dir = %s", build_dir.to_str());
@@ -310,14 +325,14 @@ impl Ctx {
310325
// Do something reasonable for now
311326

312327
let dir = build_pkg_id_in_workspace(id, workspace);
313-
util::note(fmt!("Cleaning package %s (removing directory %s)",
328+
note(fmt!("Cleaning package %s (removing directory %s)",
314329
id.to_str(), dir.to_str()));
315330
if os::path_exists(&dir) {
316331
os::remove_dir_recursive(&dir);
317-
util::note(fmt!("Removed directory %s", dir.to_str()));
332+
note(fmt!("Removed directory %s", dir.to_str()));
318333
}
319334

320-
util::note(fmt!("Cleaned package %s", id.to_str()));
335+
note(fmt!("Cleaned package %s", id.to_str()));
321336
}
322337

323338
fn info(&self) {
@@ -338,7 +353,7 @@ impl Ctx {
338353
let maybe_executable = built_executable_in_workspace(id, workspace);
339354
let maybe_library = built_library_in_workspace(id, workspace);
340355
let target_exec = target_executable_in_workspace(id, workspace);
341-
let target_lib = maybe_library.map(|p| target_library_in_workspace(workspace, p));
356+
let target_lib = maybe_library.map(|_p| target_library_in_workspace(id, workspace));
342357

343358
debug!("target_exec = %s target_lib = %? \
344359
maybe_executable = %? maybe_library = %?",
@@ -392,7 +407,7 @@ pub fn main() {
392407
let matches = &match getopts::getopts(args, opts) {
393408
result::Ok(m) => m,
394409
result::Err(f) => {
395-
util::error(fmt!("%s", getopts::fail_str(f)));
410+
error(fmt!("%s", getopts::fail_str(f)));
396411

397412
return;
398413
}
@@ -428,8 +443,12 @@ pub fn main() {
428443
};
429444
}
430445

446+
let sroot = match filesearch::get_rustpkg_sysroot() {
447+
Ok(r) => Some(@r.pop().pop()), Err(_) => None
448+
};
449+
debug!("Using sysroot: %?", sroot);
431450
Ctx {
432-
sysroot_opt: None, // Currently, only tests override this
451+
sysroot_opt: sroot, // Currently, only tests override this
433452
json: json,
434453
dep_cache: @mut HashMap::new()
435454
}.run(cmd, args);

src/librustpkg/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ pub fn find_library_in_search_path(sroot_opt: Option<@Path>, short_name: &str) -
2222
}
2323
None => None
2424
}
25-
}
25+
}

0 commit comments

Comments
 (0)