diff --git a/components/util/opts.rs b/components/util/opts.rs index 78ac6f9cee6..e8d5283b9b4 100644 --- a/components/util/opts.rs +++ b/components/util/opts.rs @@ -868,7 +868,7 @@ pub fn parse_url_or_filename(cwd: &Path, input: &str) -> Result { match Url::parse(input) { Ok(url) => Ok(url), Err(url::ParseError::RelativeUrlWithoutBase) => { - Ok(Url::from_file_path(&*cwd.join(input)).unwrap()) + Url::from_file_path(&*cwd.join(input)) } Err(_) => Err(()), } diff --git a/tests/unit/util/opts.rs b/tests/unit/util/opts.rs index a4b4d8c3b35..5807f3327ef 100644 --- a/tests/unit/util/opts.rs +++ b/tests/unit/util/opts.rs @@ -5,9 +5,15 @@ use std::path::Path; use util::opts::parse_url_or_filename; +#[cfg(not(target_os = "windows"))] +const FAKE_CWD: &'static str = "/fake/cwd"; + +#[cfg(target_os = "windows")] +const FAKE_CWD: &'static str = "C:/fake/cwd"; + #[test] fn test_argument_parsing() { - let fake_cwd = Path::new("/fake/cwd"); + let fake_cwd = Path::new(FAKE_CWD); assert!(parse_url_or_filename(fake_cwd, "http://example.net:invalid").is_err()); let url = parse_url_or_filename(fake_cwd, "http://example.net").unwrap(); @@ -16,10 +22,33 @@ fn test_argument_parsing() { let url = parse_url_or_filename(fake_cwd, "file:///foo/bar.html").unwrap(); assert_eq!(url.scheme, "file"); assert_eq!(url.path().unwrap(), ["foo", "bar.html"]); +} + +#[test] +#[cfg(not(target_os = "windows"))] +fn test_file_path_parsing() { + let fake_cwd = Path::new(FAKE_CWD); let url = parse_url_or_filename(fake_cwd, "bar.html").unwrap(); assert_eq!(url.scheme, "file"); assert_eq!(url.path().unwrap(), ["fake", "cwd", "bar.html"]); +} + +#[test] +#[cfg(target_os = "windows")] +fn test_file_path_parsing() { + let fake_cwd = Path::new(FAKE_CWD); + + let url = parse_url_or_filename(fake_cwd, "bar.html").unwrap(); + assert_eq!(url.scheme, "file"); + assert_eq!(url.path().unwrap(), ["C:", "fake", "cwd", "bar.html"]); +} + +#[test] +#[cfg(not(target_os = "windows"))] +// Windows file paths can't contain ? +fn test_argument_parsing_special() { + let fake_cwd = Path::new(FAKE_CWD); // '?' and '#' have a special meaning in URLs... let url = parse_url_or_filename(fake_cwd, "file:///foo/bar?baz#buzz.html").unwrap();