Explorar o código

Build most Windows-specific code on non-Windows too so it doesn’t bitrot.

See #102.
Simon Sapin %!s(int64=11) %!d(string=hai) anos
pai
achega
93bc3d1f44
Modificáronse 2 ficheiros con 27 adicións e 14 borrados
  1. 12 0
      src/lib.rs
  2. 15 14
      src/tests.rs

+ 12 - 0
src/lib.rs

@@ -928,6 +928,12 @@ fn path_to_file_url_path(path: &Path) -> Result<Vec<String>, ()> {
 
 #[cfg(windows)]
 fn path_to_file_url_path(path: &Path) -> Result<Vec<String>, ()> {
+    path_to_file_url_path_windows(path)
+}
+
+// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
+#[cfg_attr(not(windows), allow(dead_code))]
+fn path_to_file_url_path_windows(path: &Path) -> Result<Vec<String>, ()> {
     use std::path::{Prefix, Component};
     if !path.is_absolute() {
         return Err(())
@@ -983,6 +989,12 @@ fn file_url_path_to_pathbuf(path: &[String]) -> Result<PathBuf, ()> {
 
 #[cfg(windows)]
 fn file_url_path_to_pathbuf(path: &[String]) -> Result<PathBuf, ()> {
+    file_url_path_to_pathbuf_windows(path)
+}
+
+// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
+#[cfg_attr(not(windows), allow(dead_code))]
+fn file_url_path_to_pathbuf_windows(path: &[String]) -> Result<PathBuf, ()> {
     use percent_encoding::percent_decode;
 
     if path.is_empty() {

+ 15 - 14
src/tests.rs

@@ -234,24 +234,25 @@ fn new_path_bad_utf8() {
 }
 
 #[test]
-#[cfg(windows)]
 fn new_path_windows_fun() {
-    use std::path::{Path, PathBuf};
-    let mut url = Url::from_file_path(Path::new(r"C:\foo\bar")).unwrap();
-    assert_eq!(url.host(), Some(&Host::Domain("".to_string())));
-    assert_eq!(url.path(), Some(&["C:".to_string(), "foo".to_string(), "bar".to_string()][..]));
-    assert_eq!(url.to_file_path(),
-               Ok(PathBuf::from(r"C:\foo\bar")));
+    if cfg!(windows) {
+        use std::path::{Path, PathBuf};
+        let mut url = Url::from_file_path(Path::new(r"C:\foo\bar")).unwrap();
+        assert_eq!(url.host(), Some(&Host::Domain("".to_string())));
+        assert_eq!(url.path(), Some(&["C:".to_string(), "foo".to_string(), "bar".to_string()][..]));
+        assert_eq!(url.to_file_path(),
+                   Ok(PathBuf::from(r"C:\foo\bar")));
 
-    url.path_mut().unwrap()[2] = "ba\0r".to_string();
-    assert!(url.to_file_path().is_ok());
+        url.path_mut().unwrap()[2] = "ba\0r".to_string();
+        assert!(url.to_file_path().is_ok());
 
-    url.path_mut().unwrap()[2] = "ba%00r".to_string();
-    assert!(url.to_file_path().is_ok());
+        url.path_mut().unwrap()[2] = "ba%00r".to_string();
+        assert!(url.to_file_path().is_ok());
 
-    // Invalid UTF-8
-    url.path_mut().unwrap()[2] = "ba%80r".to_string();
-    assert!(url.to_file_path().is_err());
+        // Invalid UTF-8
+        url.path_mut().unwrap()[2] = "ba%80r".to_string();
+        assert!(url.to_file_path().is_err());
+    }
 }