Skip to content

Commit

Permalink
Merge pull request #56 from alexcrichton/fix-windows-tests
Browse files Browse the repository at this point in the history
Fix all tests on windows
  • Loading branch information
wycats committed Jun 25, 2014
2 parents db0d744 + b05fbe5 commit c2ab4f7
Show file tree
Hide file tree
Showing 15 changed files with 277 additions and 219 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ BINS = cargo \
SRC = $(shell find src -name '*.rs' -not -path 'src/bin*')

DEPS = -L libs/hammer.rs/target -L libs/toml-rs/build
TOML = libs/toml-rs/lib/$(shell rustc --crate-file-name libs/toml-rs/src/toml.rs)
TOML = libs/toml-rs/build/$(shell rustc --crate-file-name libs/toml-rs/src/toml.rs)
HAMMER = libs/hammer.rs/target/$(shell rustc --crate-type=lib --crate-file-name libs/hammer.rs/src/hammer.rs)
HAMCREST = libs/hamcrest-rust/target/libhamcrest.timestamp
LIBCARGO = target/libcargo.timestamp
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Package {
// Sort the sources just to make sure we have a consistent fingerprint.
sources.sort_by(|a, b| {
cmp::lexical_ordering(a.kind.cmp(&b.kind),
a.url.to_str().cmp(&b.url.to_str()))
a.location.to_str().cmp(&b.location.to_str()))
});
let sources = sources.iter().map(|source_id| {
source_id.load(config)
Expand Down
24 changes: 13 additions & 11 deletions src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serialize::{
};

use util::{CargoResult, CargoError};
use core::source::Location;

trait ToVersion {
fn to_version(self) -> Result<semver::Version, String>;
Expand Down Expand Up @@ -57,7 +58,7 @@ impl<'a> ToUrl for &'a Url {
pub struct PackageId {
name: String,
version: semver::Version,
namespace: Url
namespace: Location,
}

#[deriving(Clone, Show, PartialEq)]
Expand All @@ -77,14 +78,13 @@ impl CargoError for PackageIdError {
}

impl PackageId {
pub fn new<T: ToVersion, U: ToUrl>(name: &str, version: T,
namespace: U) -> CargoResult<PackageId> {
pub fn new<T: ToVersion>(name: &str, version: T,
ns: &Location) -> CargoResult<PackageId> {
let v = try!(version.to_version().map_err(InvalidVersion));
let ns = try!(namespace.to_url().map_err(InvalidNamespace));
Ok(PackageId {
name: name.to_str(),
version: v,
namespace: ns
namespace: ns.clone()
})
}

Expand All @@ -96,7 +96,7 @@ impl PackageId {
&self.version
}

pub fn get_namespace<'a>(&'a self) -> &'a Url {
pub fn get_namespace<'a>(&'a self) -> &'a Location {
&self.namespace
}
}
Expand Down Expand Up @@ -125,7 +125,7 @@ impl<D: Decoder<Box<CargoError>>> Decodable<D,Box<CargoError>> for PackageId {
PackageId::new(
vector.get(0).as_slice(),
vector.get(1).as_slice(),
vector.get(2).as_slice())
&try!(Location::parse(vector.get(2).as_slice())))
}
}

Expand All @@ -139,12 +139,14 @@ impl<E, S: Encoder<E>> Encodable<S,E> for PackageId {
#[cfg(test)]
mod tests {
use super::{PackageId, central_repo};
use core::source::Location;

#[test]
fn invalid_version_handled_nicely() {
assert!(PackageId::new("foo", "1.0", central_repo).is_err());
assert!(PackageId::new("foo", "1", central_repo).is_err());
assert!(PackageId::new("foo", "bar", central_repo).is_err());
assert!(PackageId::new("foo", "", central_repo).is_err());
let repo = Location::parse(central_repo).unwrap();
assert!(PackageId::new("foo", "1.0", &repo).is_err());
assert!(PackageId::new("foo", "1", &repo).is_err());
assert!(PackageId::new("foo", "bar", &repo).is_err());
assert!(PackageId::new("foo", "", &repo).is_err());
}
}
43 changes: 15 additions & 28 deletions src/cargo/core/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,56 +57,43 @@ pub fn resolve<R: Registry>(deps: &[Dependency],
#[cfg(test)]
mod test {
use url;
use hamcrest::{assert_that, equal_to, contains};

use hamcrest::{
assert_that,
equal_to,
contains
};

use core::source::{
SourceId,
RegistryKind
};

use core::{
Dependency,
PackageId,
Summary
};

use super::{
resolve
};
use core::source::{SourceId, RegistryKind, Location, Remote};
use core::{Dependency, PackageId, Summary};
use super::resolve;

macro_rules! pkg(
($name:expr => $($deps:expr),+) => (
{
let url = url::from_str("http://example.com").unwrap();
let source_id = SourceId::new(RegistryKind, url);
let source_id = SourceId::new(RegistryKind, Remote(url));
let d: Vec<Dependency> = vec!($($deps),+).iter().map(|s| {
Dependency::parse(*s, Some("1.0.0"), &source_id).unwrap()
}).collect();
Summary::new(&PackageId::new($name, "1.0.0",
"http://www.example.com/").unwrap(),
Summary::new(&PackageId::new($name, "1.0.0", &registry_loc()).unwrap(),
d.as_slice())
}
);

($name:expr) => (
Summary::new(&PackageId::new($name, "1.0.0",
"http://www.example.com/").unwrap(), [])
Summary::new(&PackageId::new($name, "1.0.0", &registry_loc()).unwrap(),
[])
)
)

fn registry_loc() -> Location {
Location::parse("http://www.example.com/").unwrap()
}

fn pkg(name: &str) -> Summary {
Summary::new(&PackageId::new(name, "1.0.0", "http://www.example.com/").unwrap(),
Summary::new(&PackageId::new(name, "1.0.0", &registry_loc()).unwrap(),
&[])
}

fn dep(name: &str) -> Dependency {
let url = url::from_str("http://example.com").unwrap();
let source_id = SourceId::new(RegistryKind, url);
let source_id = SourceId::new(RegistryKind, Remote(url));
Dependency::parse(name, Some("1.0.0"), &source_id).unwrap()
}

Expand All @@ -116,7 +103,7 @@ mod test {

fn names(names: &[&'static str]) -> Vec<PackageId> {
names.iter()
.map(|name| PackageId::new(*name, "1.0.0", "http://www.example.com/").unwrap())
.map(|name| PackageId::new(*name, "1.0.0", &registry_loc()).unwrap())
.collect()
}

Expand Down
76 changes: 48 additions & 28 deletions src/cargo/core/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::fmt::{Show, Formatter};
use url;
use url::Url;

use core::{Summary,Package,PackageId};
use sources::{PathSource,GitSource};
use util::{Config,CargoResult};
use core::{Summary, Package, PackageId};
use sources::{PathSource, GitSource};
use util::{Config, CargoResult};
use util::errors::human;

/// A Source finds and downloads remote packages based on names and
/// versions.
Expand Down Expand Up @@ -51,20 +52,47 @@ pub enum SourceKind {
RegistryKind
}

#[deriving(Clone, PartialEq, Eq)]
pub enum Location {
Local(Path),
Remote(Url),
}

#[deriving(Clone,PartialEq)]
pub struct SourceId {
pub kind: SourceKind,
pub url: Url
pub location: Location,
}

impl Show for Location {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
Local(ref p) => write!(f, "file:{}", p.display()),
Remote(ref u) => write!(f, "{}", u),
}
}
}

impl Location {
pub fn parse(s: &str) -> CargoResult<Location> {
if s.starts_with("file:") {
Ok(Local(Path::new(s.slice_from(5))))
} else {
url::from_str(s).map(Remote).map_err(|e| {
human(format!("invalid url `{}`: `{}", s, e))
})
}
}
}

impl Show for SourceId {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
SourceId { kind: PathKind, ref url } => {
try!(write!(f, "{}", url))
SourceId { kind: PathKind, ref location } => {
try!(write!(f, "{}", location))
},
SourceId { kind: GitKind(ref reference), ref url } => {
try!(write!(f, "{}", url));
SourceId { kind: GitKind(ref reference), ref location } => {
try!(write!(f, "{}", location));
if reference.as_slice() != "master" {
try!(write!(f, " (ref={})", reference));
}
Expand All @@ -80,33 +108,26 @@ impl Show for SourceId {
}

impl SourceId {
pub fn new(kind: SourceKind, url: Url) -> SourceId {
SourceId { kind: kind, url: url }
pub fn new(kind: SourceKind, location: Location) -> SourceId {
SourceId { kind: kind, location: location }
}

// Pass absolute path
pub fn for_path(path: &Path) -> SourceId {
// TODO: use proper path -> URL
let url = if cfg!(windows) {
let path = path.display().to_str();
format!("file://{}", path.as_slice().replace("\\", "/"))
} else {
format!("file://{}", path.display())
};
SourceId::new(PathKind, url::from_str(url.as_slice()).unwrap())
SourceId::new(PathKind, Local(path.clone()))
}

pub fn for_git(url: &Url, reference: &str) -> SourceId {
SourceId::new(GitKind(reference.to_str()), url.clone())
SourceId::new(GitKind(reference.to_str()), Remote(url.clone()))
}

pub fn for_central() -> SourceId {
SourceId::new(RegistryKind,
url::from_str("https://example.com").unwrap())
Remote(url::from_str("https://example.com").unwrap()))
}

pub fn get_url<'a>(&'a self) -> &'a Url {
&self.url
pub fn get_location<'a>(&'a self) -> &'a Location {
&self.location
}

pub fn is_path(&self) -> bool {
Expand All @@ -124,12 +145,11 @@ impl SourceId {
match self.kind {
GitKind(..) => box GitSource::new(self, config) as Box<Source>,
PathKind => {
let mut path = self.url.path.clone();
if cfg!(windows) {
path = path.replace("/", "\\");
}
let path = Path::new(path);
box PathSource::new(&path, self) as Box<Source>
let path = match self.location {
Local(ref p) => p,
Remote(..) => fail!("path sources cannot be remote"),
};
box PathSource::new(path, self) as Box<Source>
},
RegistryKind => unimplemented!()
}
Expand Down
Loading

0 comments on commit c2ab4f7

Please sign in to comment.