]> git.lizzy.rs Git - rust.git/commitdiff
std: Add fs::homedir
authorBrian Anderson <banderson@mozilla.com>
Fri, 6 Jan 2012 19:55:43 +0000 (11:55 -0800)
committerBrian Anderson <banderson@mozilla.com>
Sat, 7 Jan 2012 00:33:17 +0000 (16:33 -0800)
Returns the home directory of the user as appropriate for the platform.

Issue #1359

src/libstd/fs.rs
src/test/stdtest/fs.rs

index 330305651436549b6bc19883742875525154f283..cf7173f09576e7561670d1e7b89c428bfd526bb3 100644 (file)
@@ -399,6 +399,54 @@ fn reterminate(orig: path, new: path) -> path {
     }
 }
 
+/*
+Function: homedir
+
+Returns the path to the user's home directory, if known.
+
+On Unix, returns the value of the "HOME" environment variable if it is set and
+not equal to the empty string.
+
+On Windows, returns the value of the "HOME" environment variable if it is set
+and not equal to the empty string. Otherwise, returns the value of the
+"USERPROFILE" environment variable if it is set and not equal to the empty
+string.
+
+Otherwise, homedir returns option::none.
+*/
+fn homedir() -> option<path> {
+    ret alt generic_os::getenv("HOME") {
+        some(p) {
+           if !str::is_empty(p) {
+                some(p)
+            } else {
+               secondary()
+           }
+       }
+       none. {
+           secondary()
+       }
+    };
+
+    #[cfg(target_os = "linux")]
+    #[cfg(target_os = "macos")]
+    #[cfg(target_os = "freebsd")]
+    fn secondary() -> option<path> {
+        none
+    }
+
+    #[cfg(target_os = "win32")]
+    fn secondary() -> option<path> {
+        option::maybe(none, generic_os::getenv("USERPROFILE")) {|p|
+            if !str::is_empty(p) {
+                some(p)
+           } else {
+               none
+           }
+        }
+    }
+}
+
 // Local Variables:
 // mode: rust;
 // fill-column: 78;
index a0ab02a898f26e57c804c7076b98135e1d2676eb..f0aacf7fe739f7c9f4f553022f65dca6a94aea5e 100644 (file)
@@ -221,3 +221,55 @@ fn splitext_nobasename() {
     assert base == "oh.my/";
     assert ext == "";
 }
+
+#[test]
+#[cfg(target_os = "linux")]
+#[cfg(target_os = "macos")]
+#[cfg(target_os = "freebsd")]
+fn homedir() {
+    import getenv = std::generic_os::getenv;
+    import setenv = std::generic_os::setenv;
+
+    let oldhome = getenv("HOME");
+
+    setenv("HOME", "/home/MountainView");
+    assert fs::homedir() == some("/home/MountainView");
+
+    setenv("HOME", "");
+    assert fs::homedir() == none;
+
+    option::may(oldhome, {|s| setenv("HOME", s)});
+}
+
+#[test]
+#[cfg(target_os = "win32")]
+fn homedir() {
+    import getenv = std::generic_os::getenv;
+    import setenv = std::generic_os::setenv;
+
+    let oldhome = getenv("HOME");
+    let olduserprofile = getenv("USERPROFILE");
+
+    setenv("HOME", "");
+    setenv("USERPROFILE", "");
+
+    assert fs::homedir() == none;
+
+    setenv("HOME", "/home/MountainView");
+    assert fs::homedir() == some("/home/MountainView");
+
+    setenv("HOME", "");
+
+    setenv("USERPROFILE", "/home/MountainView");
+    assert fs::homedir() == some("/home/MountainView");
+
+    setenv("USERPROFILE", "/home/MountainView");
+    assert fs::homedir() == some("/home/MountainView");
+
+    setenv("HOME", "/home/MountainView");
+    setenv("USERPROFILE", "/home/PaloAlto");
+    assert fs::homedir() == some("/home/MountainView");
+
+    option::may(oldhome, {|s| setenv("HOME", s)});
+    option::may(olduserprofile, {|s| setenv("USERPROFILE", s)});
+}