2013-04-12 18:15:40 -05:00
|
|
|
// 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 unit tests
|
2013-04-24 19:37:59 -05:00
|
|
|
|
|
|
|
use context::Ctx;
|
|
|
|
use core::hashmap::HashMap;
|
|
|
|
use core::path::Path;
|
|
|
|
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,
|
2013-05-10 21:00:51 -05:00
|
|
|
make_dir_rwx, u_rwx, RemotePath, LocalPath, normalize,
|
|
|
|
built_bench_in_workspace, built_test_in_workspace};
|
2013-04-24 19:37:59 -05:00
|
|
|
|
2013-05-03 18:47:53 -05:00
|
|
|
fn fake_ctxt(sysroot_opt: Option<@Path>) -> Ctx {
|
2013-04-24 19:37:59 -05:00
|
|
|
Ctx {
|
2013-05-03 18:47:53 -05:00
|
|
|
sysroot_opt: sysroot_opt,
|
2013-04-24 19:37:59 -05:00
|
|
|
json: false,
|
|
|
|
dep_cache: @mut HashMap::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fake_pkg() -> PkgId {
|
2013-05-10 21:00:51 -05:00
|
|
|
let sn = ~"bogus";
|
|
|
|
let remote = RemotePath(Path(sn));
|
2013-04-24 19:37:59 -05:00
|
|
|
PkgId {
|
2013-05-14 19:46:52 -05:00
|
|
|
local_path: normalize(copy remote),
|
2013-05-10 21:00:51 -05:00
|
|
|
remote_path: remote,
|
|
|
|
short_name: sn,
|
2013-04-24 19:37:59 -05:00
|
|
|
version: default_version()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 18:47:53 -05:00
|
|
|
fn remote_pkg() -> PkgId {
|
2013-05-10 21:00:51 -05:00
|
|
|
let remote = RemotePath(Path(~"github.com/catamorphism/test-pkg"));
|
2013-05-03 18:47:53 -05:00
|
|
|
PkgId {
|
2013-05-14 19:46:52 -05:00
|
|
|
local_path: normalize(copy remote),
|
2013-05-10 21:00:51 -05:00
|
|
|
remote_path: remote,
|
|
|
|
short_name: ~"test_pkg",
|
2013-05-03 18:47:53 -05:00
|
|
|
version: default_version()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-02 15:09:28 -05:00
|
|
|
fn writeFile(file_path: &Path, contents: ~str) {
|
|
|
|
let out: @io::Writer =
|
|
|
|
result::get(&io::file_writer(file_path,
|
|
|
|
~[io::Create, io::Truncate]));
|
|
|
|
out.write_line(contents);
|
|
|
|
}
|
|
|
|
|
2013-05-10 21:00:51 -05:00
|
|
|
fn mk_temp_workspace(short_name: &LocalPath) -> Path {
|
2013-05-02 15:09:28 -05:00
|
|
|
let workspace = mkdtemp(&os::tmpdir(), "test").expect("couldn't create temp dir");
|
2013-05-14 19:46:52 -05:00
|
|
|
// include version number in directory name
|
|
|
|
let package_dir = workspace.push(~"src").push(fmt!("%s-0.1", short_name.to_str()));
|
|
|
|
assert!(os::mkdir_recursive(&package_dir, u_rwx));
|
2013-05-02 15:09:28 -05:00
|
|
|
// Create main, lib, test, and bench files
|
|
|
|
writeFile(&package_dir.push(~"main.rs"),
|
|
|
|
~"fn main() { let _x = (); }");
|
|
|
|
writeFile(&package_dir.push(~"lib.rs"),
|
|
|
|
~"pub fn f() { let _x = (); }");
|
|
|
|
writeFile(&package_dir.push(~"test.rs"),
|
|
|
|
~"#[test] pub fn f() { (); }");
|
|
|
|
writeFile(&package_dir.push(~"bench.rs"),
|
|
|
|
~"#[bench] pub fn f() { (); }");
|
|
|
|
workspace
|
2013-04-24 19:37:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2013-05-02 15:09:28 -05:00
|
|
|
Some(m) =>
|
2013-04-24 19:37:59 -05:00
|
|
|
((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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 18:47:53 -05:00
|
|
|
fn test_sysroot() -> Path {
|
|
|
|
// Totally gross hack but it's just for test cases.
|
2013-05-14 09:25:13 -05:00
|
|
|
// Infer the sysroot from the exe name and pray that it's right.
|
|
|
|
// (Did I mention it was a gross hack?)
|
2013-05-03 18:47:53 -05:00
|
|
|
let self_path = os::self_exe_path().expect("Couldn't get self_exe path");
|
2013-05-14 09:25:13 -05:00
|
|
|
self_path.pop()
|
2013-05-03 18:47:53 -05:00
|
|
|
}
|
|
|
|
|
2013-05-15 18:15:26 -05:00
|
|
|
// Ignored on i686 -- see #6517
|
2013-04-24 19:37:59 -05:00
|
|
|
#[test]
|
2013-05-15 18:15:26 -05:00
|
|
|
#[ignore(cfg(target_arch = "x86"))]
|
2013-04-24 19:37:59 -05:00
|
|
|
fn test_make_dir_rwx() {
|
|
|
|
let temp = &os::tmpdir();
|
|
|
|
let dir = temp.push(~"quux");
|
2013-05-02 15:09:28 -05:00
|
|
|
assert!(!os::path_exists(&dir) ||
|
|
|
|
os::remove_dir_recursive(&dir));
|
|
|
|
debug!("Trying to make %s", dir.to_str());
|
2013-04-24 19:37:59 -05:00
|
|
|
assert!(make_dir_rwx(&dir));
|
|
|
|
assert!(os::path_is_dir(&dir));
|
|
|
|
assert!(is_rwx(&dir));
|
2013-05-02 15:09:28 -05:00
|
|
|
assert!(os::remove_dir_recursive(&dir));
|
2013-04-24 19:37:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-05-15 18:15:26 -05:00
|
|
|
#[ignore(cfg(target_arch = "x86"))]
|
2013-04-24 19:37:59 -05:00
|
|
|
fn test_install_valid() {
|
2013-05-03 18:47:53 -05:00
|
|
|
let sysroot = test_sysroot();
|
|
|
|
debug!("sysroot = %s", sysroot.to_str());
|
|
|
|
let ctxt = fake_ctxt(Some(@sysroot));
|
2013-04-24 19:37:59 -05:00
|
|
|
let temp_pkg_id = fake_pkg();
|
2013-05-10 21:00:51 -05:00
|
|
|
let temp_workspace = mk_temp_workspace(&temp_pkg_id.local_path);
|
2013-04-24 19:37:59 -05:00
|
|
|
// should have test, bench, lib, and main
|
2013-05-12 15:50:57 -05:00
|
|
|
ctxt.install(&temp_workspace, &temp_pkg_id);
|
2013-04-24 19:37:59 -05:00
|
|
|
// Check that all files exist
|
2013-05-12 15:50:57 -05:00
|
|
|
let exec = target_executable_in_workspace(&temp_pkg_id, &temp_workspace);
|
2013-05-02 15:09:28 -05:00
|
|
|
debug!("exec = %s", exec.to_str());
|
2013-04-24 19:37:59 -05:00
|
|
|
assert!(os::path_exists(&exec));
|
|
|
|
assert!(is_rwx(&exec));
|
2013-05-12 15:50:57 -05:00
|
|
|
let lib = target_library_in_workspace(&temp_pkg_id, &temp_workspace);
|
2013-05-02 15:09:28 -05:00
|
|
|
debug!("lib = %s", lib.to_str());
|
2013-04-24 19:37:59 -05:00
|
|
|
assert!(os::path_exists(&lib));
|
|
|
|
assert!(is_rwx(&lib));
|
|
|
|
// And that the test and bench executables aren't installed
|
2013-05-12 15:50:57 -05:00
|
|
|
assert!(!os::path_exists(&target_test_in_workspace(&temp_pkg_id, &temp_workspace)));
|
|
|
|
let bench = target_bench_in_workspace(&temp_pkg_id, &temp_workspace);
|
2013-05-02 15:09:28 -05:00
|
|
|
debug!("bench = %s", bench.to_str());
|
|
|
|
assert!(!os::path_exists(&bench));
|
2013-04-24 19:37:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-05-15 18:15:26 -05:00
|
|
|
#[ignore(cfg(target_arch = "x86"))]
|
2013-04-24 19:37:59 -05:00
|
|
|
fn test_install_invalid() {
|
|
|
|
use conditions::nonexistent_package::cond;
|
2013-05-02 15:09:28 -05:00
|
|
|
use cond1 = conditions::missing_pkg_files::cond;
|
2013-04-24 19:37:59 -05:00
|
|
|
|
2013-05-03 18:47:53 -05:00
|
|
|
let ctxt = fake_ctxt(None);
|
2013-04-24 19:37:59 -05:00
|
|
|
let pkgid = fake_pkg();
|
2013-05-02 15:09:28 -05:00
|
|
|
let temp_workspace = mkdtemp(&os::tmpdir(), "test").expect("couldn't create temp dir");
|
|
|
|
let mut error_occurred = false;
|
|
|
|
let mut error1_occurred = false;
|
|
|
|
do cond1.trap(|_| {
|
|
|
|
error1_occurred = true;
|
2013-04-24 19:37:59 -05:00
|
|
|
}).in {
|
2013-05-02 15:09:28 -05:00
|
|
|
do cond.trap(|_| {
|
|
|
|
error_occurred = true;
|
|
|
|
}).in {
|
2013-05-12 15:50:57 -05:00
|
|
|
ctxt.install(&temp_workspace, &pkgid);
|
2013-05-02 15:09:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert!(error_occurred && error1_occurred);
|
2013-04-26 11:24:15 -05:00
|
|
|
}
|
2013-05-03 18:47:53 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-05-15 18:15:26 -05:00
|
|
|
#[ignore(cfg(target_arch = "x86"))]
|
2013-05-03 18:47:53 -05:00
|
|
|
fn test_install_url() {
|
2013-05-10 21:00:51 -05:00
|
|
|
let workspace = mkdtemp(&os::tmpdir(), "test").expect("couldn't create temp dir");
|
2013-05-03 18:47:53 -05:00
|
|
|
let sysroot = test_sysroot();
|
|
|
|
debug!("sysroot = %s", sysroot.to_str());
|
|
|
|
let ctxt = fake_ctxt(Some(@sysroot));
|
|
|
|
let temp_pkg_id = remote_pkg();
|
|
|
|
// should have test, bench, lib, and main
|
2013-05-14 19:46:52 -05:00
|
|
|
ctxt.install(&workspace, &temp_pkg_id);
|
2013-05-03 18:47:53 -05:00
|
|
|
// Check that all files exist
|
2013-05-14 19:46:52 -05:00
|
|
|
let exec = target_executable_in_workspace(&temp_pkg_id, &workspace);
|
2013-05-03 18:47:53 -05:00
|
|
|
debug!("exec = %s", exec.to_str());
|
|
|
|
assert!(os::path_exists(&exec));
|
|
|
|
assert!(is_rwx(&exec));
|
2013-05-14 19:46:52 -05:00
|
|
|
let lib = target_library_in_workspace(&temp_pkg_id, &workspace);
|
2013-05-03 18:47:53 -05:00
|
|
|
debug!("lib = %s", lib.to_str());
|
|
|
|
assert!(os::path_exists(&lib));
|
|
|
|
assert!(is_rwx(&lib));
|
2013-05-14 19:46:52 -05:00
|
|
|
let built_test = built_test_in_workspace(&temp_pkg_id, &workspace).expect(~"test_install_url");
|
2013-05-10 21:00:51 -05:00
|
|
|
assert!(os::path_exists(&built_test));
|
2013-05-14 19:46:52 -05:00
|
|
|
let built_bench = built_bench_in_workspace(&temp_pkg_id,
|
|
|
|
&workspace).expect(~"test_install_url");
|
2013-05-10 21:00:51 -05:00
|
|
|
assert!(os::path_exists(&built_bench));
|
2013-05-03 18:47:53 -05:00
|
|
|
// And that the test and bench executables aren't installed
|
2013-05-14 19:46:52 -05:00
|
|
|
let test = target_test_in_workspace(&temp_pkg_id, &workspace);
|
2013-05-10 21:00:51 -05:00
|
|
|
assert!(!os::path_exists(&test));
|
|
|
|
debug!("test = %s", test.to_str());
|
2013-05-14 19:46:52 -05:00
|
|
|
let bench = target_bench_in_workspace(&temp_pkg_id, &workspace);
|
2013-05-03 18:47:53 -05:00
|
|
|
debug!("bench = %s", bench.to_str());
|
|
|
|
assert!(!os::path_exists(&bench));
|
2013-05-12 15:50:57 -05:00
|
|
|
}
|
2013-05-14 19:46:52 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_package_ids_must_be_relative_path_like() {
|
|
|
|
use conditions::bad_pkg_id::cond;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Okay:
|
|
|
|
- One identifier, with no slashes
|
|
|
|
- Several slash-delimited things, with no / at the root
|
|
|
|
|
|
|
|
Not okay:
|
|
|
|
- Empty string
|
|
|
|
- Absolute path (as per os::is_absolute)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
let default_version_str = "0.1";
|
|
|
|
let addversion = |s| {
|
|
|
|
fmt!("%s-%s", s, default_version_str)
|
|
|
|
};
|
|
|
|
|
|
|
|
let whatever = PkgId::new("foo");
|
|
|
|
|
|
|
|
assert!(addversion("foo") == whatever.to_str());
|
|
|
|
assert!(addversion("github.com/mozilla/rust") ==
|
|
|
|
PkgId::new("github.com/mozilla/rust").to_str());
|
|
|
|
|
|
|
|
do cond.trap(|(p, e)| {
|
|
|
|
assert!("" == p.to_str());
|
|
|
|
assert!("0-length pkgid" == e);
|
|
|
|
copy whatever
|
|
|
|
}).in {
|
|
|
|
let x = PkgId::new("");
|
|
|
|
assert!(addversion("foo") == x.to_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
do cond.trap(|(p, e)| {
|
|
|
|
assert!(p.to_str() == os::make_absolute(&Path("foo/bar/quux")).to_str());
|
|
|
|
assert!("absolute pkgid" == e);
|
|
|
|
copy whatever
|
|
|
|
}).in {
|
|
|
|
let z = PkgId::new(os::make_absolute(&Path("foo/bar/quux")).to_str());
|
|
|
|
assert!(addversion("foo") == z.to_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|