diff --git a/components/util/opts.rs b/components/util/opts.rs index 08124b1ebb6..28a5908e477 100644 --- a/components/util/opts.rs +++ b/components/util/opts.rs @@ -14,7 +14,7 @@ use prefs; use std::cmp; use std::default::Default; use std::env; -use std::fs::{File, PathExt}; +use std::fs::File; use std::io::{self, Read, Write}; use std::path::Path; use std::process; @@ -485,18 +485,8 @@ pub fn from_cmdline_args(args: &[String]) { print_usage(app_name, &opts); args_fail("servo asks that you provide a URL") } else { - let ref url = opt_match.free[0]; - match Url::parse(url) { - Ok(url) => url, - Err(url::ParseError::RelativeUrlWithoutBase) => { - if Path::new(url).exists() { - Url::from_file_path(&*cwd.join(url)).unwrap() - } else { - args_fail(&format!("File not found: {}", url)) - } - } - Err(_) => panic!("URL parsing failed"), - } + parse_url_or_filename(&cwd, &opt_match.free[0]) + .unwrap_or_else(|()| args_fail("URL parsing failed")) }; let tile_size: usize = match opt_match.opt_str("s") { @@ -663,3 +653,13 @@ pub fn set_defaults(opts: Opts) { pub fn get() -> &'static Opts { &OPTIONS } + +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()) + } + Err(_) => Err(()), + } +} diff --git a/tests/ref/basic.list b/tests/ref/basic.list index b70bc02f9a0..5bb708a9d04 100644 --- a/tests/ref/basic.list +++ b/tests/ref/basic.list @@ -125,7 +125,6 @@ prefs:"layout.flex.enabled" == flex_row_direction.html flex_row_direction_ref.ht == font_style.html font_style_ref.html == height_compute_reset.html height_compute.html # `?` and `#` in the name is a test for https://github.com/servo/servo/issues/3340 -== hello_a?foo#bar.html hello_b.html == hide_after_create.html hide_after_create_ref.html == iframe/bg_color.html iframe/bg_color_ref.html diff --git a/tests/ref/hello_a?foo#bar.html b/tests/ref/hello_a?foo#bar.html deleted file mode 100644 index 6ef40f0f03c..00000000000 --- a/tests/ref/hello_a?foo#bar.html +++ /dev/null @@ -1,8 +0,0 @@ - - - hello - - - Hello! - - diff --git a/tests/ref/hello_b.html b/tests/ref/hello_b.html deleted file mode 100644 index e878c1f73f0..00000000000 --- a/tests/ref/hello_b.html +++ /dev/null @@ -1,8 +0,0 @@ - - - hello - - - Hello! - - diff --git a/tests/unit/util/lib.rs b/tests/unit/util/lib.rs index a9d4cb7b482..ed7e7fbbf4b 100644 --- a/tests/unit/util/lib.rs +++ b/tests/unit/util/lib.rs @@ -14,3 +14,4 @@ extern crate euclid; #[cfg(test)] mod vec; #[cfg(test)] mod mem; #[cfg(test)] mod str; +#[cfg(test)] mod opts; diff --git a/tests/unit/util/opts.rs b/tests/unit/util/opts.rs new file mode 100644 index 00000000000..a4b4d8c3b35 --- /dev/null +++ b/tests/unit/util/opts.rs @@ -0,0 +1,39 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use std::path::Path; +use util::opts::parse_url_or_filename; + +#[test] +fn test_argument_parsing() { + 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(); + assert_eq!(url.scheme, "http"); + + 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"]); + + 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"]); + + // '?' and '#' have a special meaning in URLs... + let url = parse_url_or_filename(fake_cwd, "file:///foo/bar?baz#buzz.html").unwrap(); + assert_eq!(&*url.to_file_path().unwrap(), Path::new("/foo/bar")); + assert_eq!(url.scheme, "file"); + assert_eq!(url.path().unwrap(), ["foo", "bar"]); + assert_eq!(url.query.unwrap(), "baz"); + assert_eq!(url.fragment.unwrap(), "buzz.html"); + + // but not in file names. + let url = parse_url_or_filename(fake_cwd, "./bar?baz#buzz.html").unwrap(); + assert_eq!(&*url.to_file_path().unwrap(), Path::new("/fake/cwd/bar?baz#buzz.html")); + assert_eq!(url.scheme, "file"); + assert_eq!(url.path().unwrap(), ["fake", "cwd", "bar%3Fbaz%23buzz.html"]); + assert!(url.query.is_none()); + assert!(url.fragment.is_none()); +}