Skip to content

Commit 20c00e8

Browse files
committed
rustpkg: Re-enable some tests
Re-enable test_install_invalid_external and test_install_invalid Also improves an error message Closes #9994
1 parent 90754ae commit 20c00e8

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

src/libextra/url.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ fn get_authority(rawurl: &str) ->
573573

574574

575575
// returns the path and unparsed part of url, or an error
576-
fn get_path(rawurl: &str, authority: bool) ->
576+
pub fn get_path(rawurl: &str, authority: bool) ->
577577
Result<(~str, ~str), ~str> {
578578
let len = rawurl.len();
579579
let mut end = len;

src/librustpkg/package_id.rs

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

11+
use extra::url;
1112
use version::{try_getting_version, try_getting_local_version,
1213
Version, NoVersion, split_version};
1314
use std::hash::Streaming;
1415
use std::hash;
16+
use messages::error;
1517

1618
/// Path-fragment identifier of a package such as
1719
/// 'github.com/graydon/test'; path must be a relative
@@ -41,10 +43,60 @@ impl Eq for PkgId {
4143
}
4244
}
4345

46+
/// Parses `s` as a URL and, if the parse was successful, returns the
47+
/// portion of the URL without the scheme
48+
/// for example: git://github.com/catamorphism/foo => github.com/catamorphism/foo
49+
fn drop_url_scheme<'a>(s: &'a str) -> Option<~str> {
50+
match url::get_scheme(s) {
51+
Ok((_, rest)) => {
52+
// get_scheme leaves in the leading //
53+
let len = rest.len();
54+
let path_to_use = if len > 2 {
55+
let no_slashes = rest.slice(2, len);
56+
// Don't display the .git extension, since it's not part of the actual
57+
// package ID
58+
if no_slashes.ends_with(".git") {
59+
no_slashes.slice(0, len - 6).to_owned()
60+
}
61+
else {
62+
no_slashes.to_owned()
63+
}
64+
} else {
65+
rest
66+
};
67+
Some(path_to_use)
68+
}
69+
Err(_) => None
70+
}
71+
}
72+
73+
// Fails if this is not a legal package ID,
74+
// after printing a hint
75+
fn ensure_legal_package_id(s: &str) {
76+
// url::get_path checks that the string contains characters
77+
// that are legal in a URL ('#' is legal, so we're good there)
78+
let maybe_intended_path = drop_url_scheme(s);
79+
let legal = maybe_intended_path.is_none()
80+
&& url::get_path(s, false).is_ok();
81+
if !legal {
82+
for maybe_package_id in maybe_intended_path.iter() {
83+
error(format!("rustpkg operates on package IDs; did you mean to write \
84+
`{}` instead of `{}`?",
85+
*maybe_package_id,
86+
s));
87+
}
88+
fail!("Can't parse {} as a package ID", s);
89+
}
90+
}
91+
4492
impl PkgId {
4593
pub fn new(s: &str) -> PkgId {
4694
use conditions::bad_pkg_id::cond;
4795

96+
// Make sure the path is a legal package ID -- it might not even
97+
// be a legal path, so we do this first
98+
ensure_legal_package_id(s);
99+
48100
let mut given_version = None;
49101

50102
// Did the user request a specific version?

src/librustpkg/tests.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use std::{os, run, str, task};
1515
use std::io;
1616
use std::io::fs;
1717
use std::io::File;
18-
use std::io::process;
19-
use std::io::process::ProcessExit;
2018
use extra::arc::Arc;
2119
use extra::arc::RWArc;
2220
use extra::tempfile::TempDir;
@@ -294,7 +292,7 @@ fn command_line_test_with_env(args: &[~str], cwd: &Path, env: Option<~[(~str, ~s
294292
output.status);
295293
if !output.status.success() {
296294
debug!("Command {} {:?} failed with exit code {:?}; its output was --- {} ---",
297-
cmd, args, output.status,
295+
cmd, args, output.status,
298296
str::from_utf8(output.output) + str::from_utf8(output.error));
299297
Fail(output)
300298
}
@@ -615,7 +613,6 @@ fn test_install_valid() {
615613
}
616614

617615
#[test]
618-
#[ignore]
619616
fn test_install_invalid() {
620617
let sysroot = test_sysroot();
621618
let pkgid = fake_pkg();
@@ -631,8 +628,11 @@ fn test_install_invalid() {
631628
pkgid.clone());
632629
ctxt.install(pkg_src, &WhatToBuild::new(MaybeCustom, Everything));
633630
};
634-
assert!(result.unwrap_err()
635-
.to_str().contains("supplied path for package dir does not exist"));
631+
let x = result.unwrap_err();
632+
assert!(x.is::<~str>());
633+
let error_string = *x.move::<~str>().unwrap();
634+
debug!("result error = {}", error_string);
635+
assert!(error_string.contains("supplied path for package dir does not exist"));
636636
}
637637

638638
#[test]
@@ -663,7 +663,6 @@ fn test_install_valid_external() {
663663
}
664664

665665
#[test]
666-
#[ignore(reason = "9994")]
667666
fn test_install_invalid_external() {
668667
let cwd = os::getcwd();
669668
command_line_test_expect_fail([~"install", ~"foo"],
@@ -2442,6 +2441,21 @@ fn correct_error_dependency() {
24422441
}
24432442
}
24442443
2444+
#[test]
2445+
fn test_bad_package_id_url() {
2446+
use str::{is_utf8, from_utf8};
2447+
2448+
match command_line_test_partial([~"install", ~"git://github.com/mozilla/servo.git"],
2449+
&os::getcwd()) {
2450+
Fail(ProcessOutput{ error: _, output: output, _}) => {
2451+
assert!(is_utf8(output));
2452+
assert!(from_utf8(output).contains("rustpkg operates on package IDs; did you mean to \
2453+
write `github.com/mozilla/servo` instead"));
2454+
}
2455+
Success(*) => fail!("test_bad_package_id_url: succeeded but should have failed")
2456+
}
2457+
}
2458+
24452459
/// Returns true if p exists and is executable
24462460
fn is_executable(p: &Path) -> bool {
24472461
p.exists() && p.stat().perm & io::UserExecute == io::UserExecute

0 commit comments

Comments
 (0)