Skip to content

Commit

Permalink
auto merge of #9736 : catamorphism/rust/rustpkg-issue-8520, r=cmr
Browse files Browse the repository at this point in the history
r? @cmr As per #8520, find crates in the current working directory even
if it's not a workspace.

Closes #8520
  • Loading branch information
bors committed Oct 21, 2013
2 parents ebb9b46 + 5412794 commit cf7b9eb
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 18 deletions.
4 changes: 0 additions & 4 deletions src/librustpkg/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ condition! {
pub no_rust_path: (~str) -> Path;
}

condition! {
pub not_a_workspace: (~str) -> Path;
}

condition! {
pub failed_to_create_temp_dir: (~str) -> Path;
}
Expand Down
22 changes: 19 additions & 3 deletions src/librustpkg/package_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::Crate;
use messages::*;
use source_control::{safe_git_clone, git_clone_url, DirToUse, CheckedOutSources};
use source_control::make_read_only;
use path_util::{find_dir_using_rust_path_hack, make_dir_rwx_recursive};
use path_util::{target_build_dir, versionize};
use path_util::{find_dir_using_rust_path_hack, make_dir_rwx_recursive, default_workspace};
use path_util::{target_build_dir, versionize, dir_has_crate_file};
use util::{compile_crate, DepMap};
use workcache_support;
use workcache_support::crate_tag;
Expand Down Expand Up @@ -197,7 +197,23 @@ impl PkgSrc {
match ok_d {
Some(d) => d,
None => {
if use_rust_path_hack {
// See if the sources are in $CWD
let cwd = os::getcwd();
if dir_has_crate_file(&cwd) {
return PkgSrc {
// In this case, source_workspace isn't really a workspace.
// This data structure needs yet more refactoring.
source_workspace: cwd.clone(),
destination_workspace: default_workspace(),
build_in_destination: true,
start_dir: cwd,
id: id,
libs: ~[],
mains: ~[],
benchs: ~[],
tests: ~[]
}
} else if use_rust_path_hack {
match find_dir_using_rust_path_hack(&id) {
Some(d) => d,
None => {
Expand Down
8 changes: 6 additions & 2 deletions src/librustpkg/path_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ pub fn uninstall_package_from(workspace: &Path, pkgid: &PkgId) {

}

pub fn dir_has_crate_file(dir: &Path) -> bool {
dir_has_file(dir, "lib.rs") || dir_has_file(dir, "main.rs")
|| dir_has_file(dir, "test.rs") || dir_has_file(dir, "bench.rs")
}

fn dir_has_file(dir: &Path, file: &str) -> bool {
assert!(dir.is_absolute());
os::path_exists(&dir.join(file))
Expand All @@ -427,8 +432,7 @@ pub fn find_dir_using_rust_path_hack(p: &PkgId) -> Option<Path> {
// has a name that's a single component
if dir.ends_with_path(&p.path) || dir.ends_with_path(&versionize(&p.path, &p.version)) {
debug2!("In find_dir_using_rust_path_hack: checking dir {}", dir.display());
if dir_has_file(dir, "lib.rs") || dir_has_file(dir, "main.rs")
|| dir_has_file(dir, "test.rs") || dir_has_file(dir, "bench.rs") {
if dir_has_crate_file(dir) {
debug2!("Did find id {} in dir {}", p.to_str(), dir.display());
return Some(dir.clone());
}
Expand Down
14 changes: 7 additions & 7 deletions src/librustpkg/rustpkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use messages::{error, warn, note};
use path_util::{build_pkg_id_in_workspace, built_test_in_workspace};
use path_util::{U_RWX, in_rust_path};
use path_util::{built_executable_in_workspace, built_library_in_workspace, default_workspace};
use path_util::{target_executable_in_workspace, target_library_in_workspace};
use path_util::{target_executable_in_workspace, target_library_in_workspace, dir_has_crate_file};
use source_control::{CheckedOutSources, is_git_dir, make_read_only};
use workspace::{each_pkg_parent_workspace, pkg_parent_workspaces, cwd_to_workspace};
use workspace::determine_destination;
Expand All @@ -48,7 +48,6 @@ use context::{Context, BuildContext,
use package_id::PkgId;
use package_source::PkgSrc;
use target::{WhatToBuild, Everything, is_lib, is_main, is_test, is_bench, Tests};
// use workcache_support::{discover_outputs, digest_only_date};
use workcache_support::digest_only_date;
use exit_codes::{COPY_FAILED_CODE, BAD_FLAG_CODE};

Expand Down Expand Up @@ -210,10 +209,11 @@ pub trait CtxMethods {

impl CtxMethods for BuildContext {
fn build_args(&self, args: ~[~str], what: &WhatToBuild) -> Option<(PkgId, Path)> {
let cwd = os::getcwd();

if args.len() < 1 {
match cwd_to_workspace() {
None if self.context.use_rust_path_hack => {
let cwd = os::getcwd();
None if dir_has_crate_file(&cwd) => {
// FIXME (#9639): This needs to handle non-utf8 paths
let pkgid = PkgId::new(cwd.filename_str().unwrap());
let mut pkg_src = PkgSrc::new(cwd, default_workspace(), true, pkgid);
Expand Down Expand Up @@ -260,6 +260,7 @@ impl CtxMethods for BuildContext {
}
}
fn run(&self, cmd: &str, args: ~[~str]) {
let cwd = os::getcwd();
match cmd {
"build" => {
self.build_args(args, &Everything);
Expand All @@ -278,7 +279,6 @@ impl CtxMethods for BuildContext {
// The package id is presumed to be the first command-line
// argument
let pkgid = PkgId::new(args[0].clone());
let cwd = os::getcwd();
self.clean(&cwd, &pkgid); // tjc: should use workspace, not cwd
}
}
Expand All @@ -295,9 +295,9 @@ impl CtxMethods for BuildContext {
"install" => {
if args.len() < 1 {
match cwd_to_workspace() {
None if self.context.use_rust_path_hack => {
let cwd = os::getcwd();
None if dir_has_crate_file(&cwd) => {
// FIXME (#9639): This needs to handle non-utf8 paths

let inferred_pkgid =
PkgId::new(cwd.filename_str().unwrap());
self.install(PkgSrc::new(cwd, default_workspace(),
Expand Down
59 changes: 57 additions & 2 deletions src/librustpkg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,11 @@ fn test_make_dir_rwx() {
assert!(os::remove_dir_recursive(&dir));
}

// n.b. I ignored the next two tests for now because something funny happens on linux
// and I don't want to debug the issue right now (calling into the rustpkg lib directly
// is a little sketchy anyway)
#[test]
#[ignore]
fn test_install_valid() {
use path_util::installed_library_in_workspace;

Expand Down Expand Up @@ -621,6 +625,7 @@ fn test_install_valid() {
}

#[test]
#[ignore]
fn test_install_invalid() {
let sysroot = test_sysroot();
let pkgid = fake_pkg();
Expand All @@ -641,7 +646,44 @@ fn test_install_invalid() {
assert!(result == Err(()));
}

// Tests above should (maybe) be converted to shell out to rustpkg, too
#[test]
fn test_install_valid_external() {
let temp_pkg_id = PkgId::new("foo");
let (tempdir, _) = mk_temp_workspace(&temp_pkg_id.path,
&temp_pkg_id.version);
let temp_workspace = tempdir.path();
command_line_test([~"install", ~"foo"], temp_workspace);

// Check that all files exist
let exec = target_executable_in_workspace(&temp_pkg_id, temp_workspace);
debug2!("exec = {}", exec.display());
assert!(os::path_exists(&exec));
assert!(is_rwx(&exec));

let lib = installed_library_in_workspace(&temp_pkg_id.path, temp_workspace);
debug2!("lib = {:?}", lib);
assert!(lib.as_ref().map_default(false, |l| os::path_exists(l)));
assert!(lib.as_ref().map_default(false, |l| is_rwx(l)));

// And that the test and bench executables aren't installed
assert!(!os::path_exists(&target_test_in_workspace(&temp_pkg_id, temp_workspace)));
let bench = target_bench_in_workspace(&temp_pkg_id, temp_workspace);
debug2!("bench = {}", bench.display());
assert!(!os::path_exists(&bench));

}

#[test]
#[ignore(reason = "9994")]
fn test_install_invalid_external() {
let cwd = os::getcwd();
command_line_test_expect_fail([~"install", ~"foo"],
&cwd,
None,
// FIXME #3408: Should be NONEXISTENT_PACKAGE_CODE
COPY_FAILED_CODE);
}

#[test]
fn test_install_git() {
let temp_pkg_id = git_repo_pkg();
Expand Down Expand Up @@ -1367,6 +1409,8 @@ fn rust_path_hack_test(hack_flag: bool) {
assert!(!built_executable_exists(workspace, "foo"));
}

// Notice that this is the only test case where the --rust-path-hack
// flag is actually needed
#[test]
fn test_rust_path_can_contain_package_dirs_with_flag() {
/*
Expand Down Expand Up @@ -2029,7 +2073,6 @@ fn test_rustpkg_test_output() {
}

#[test]
#[ignore(reason = "Issue 9441")]
fn test_rebuild_when_needed() {
let foo_id = PkgId::new("foo");
let foo_workspace = create_local_package(&foo_id);
Expand Down Expand Up @@ -2196,6 +2239,18 @@ fn test_compile_error() {
}
}

#[test]
fn find_sources_in_cwd() {
let temp_dir = TempDir::new("sources").expect("find_sources_in_cwd failed");
let temp_dir = temp_dir.path();
let source_dir = temp_dir.join("foo");
os::mkdir_recursive(&source_dir, U_RWX);
writeFile(&source_dir.join("main.rs"),
"fn main() { let _x = (); }");
command_line_test([~"install", ~"foo"], &source_dir);
assert_executable_exists(&source_dir.join(".rust"), "foo");
}

/// Returns true if p exists and is executable
fn is_executable(p: &Path) -> bool {
use std::libc::consts::os::posix88::{S_IXUSR};
Expand Down

0 comments on commit cf7b9eb

Please sign in to comment.