]> git.proxmox.com Git - cargo.git/commitdiff
Allow the location of `~/.cargo` to be overridden via `CARGO_HOME` env var.
authorBrian Anderson <banderson@mozilla.com>
Mon, 22 Dec 2014 21:58:24 +0000 (13:58 -0800)
committerBrian Anderson <banderson@mozilla.com>
Tue, 23 Dec 2014 03:54:41 +0000 (19:54 -0800)
I intend to use this to allow many copies of the Rust toolchain to coexist.

src/cargo/util/config.rs
tests/test_cargo.rs

index a796bb38099312af0402a62bb9b6bff642dc794b..508f6edf188e7662f68a801306f993e6b1801fe7 100644 (file)
@@ -36,7 +36,7 @@ impl<'a> Config<'a> {
         let (rustc_version, rustc_host) = try!(ops::rustc_version());
 
         Ok(Config {
-            home_path: try!(os::homedir().require(|| {
+            home_path: try!(homedir().require(|| {
                 human("Cargo couldn't find your home directory. \
                       This probably means that $HOME was not set.")
             })),
@@ -248,6 +248,12 @@ impl ConfigValue {
     }
 }
 
+fn homedir() -> Option<Path> {
+    let cargo_home = os::getenv("CARGO_HOME").map(|p| Path::new(p));
+    let user_home = os::homedir();
+    return cargo_home.or(user_home);
+}
+
 pub fn get_config(pwd: Path, key: &str) -> CargoResult<ConfigValue> {
     find_in_tree(&pwd, |file| extract_config(file, key)).map_err(|_|
         human(format!("`{}` not found in your configuration", key)))
@@ -313,7 +319,7 @@ fn walk_tree(pwd: &Path,
     // Once we're done, also be sure to walk the home directory even if it's not
     // in our history to be sure we pick up that standard location for
     // information.
-    let home = try!(os::homedir().require(|| {
+    let home = try!(homedir().require(|| {
         human("Cargo couldn't find your home directory. \
               This probably means that $HOME was not set.")
     }));
index 773754d699c831450917da474ccadc61b41fa5f5..8ae8aa2c0235249e42a93023a4c79857cdbaf656 100644 (file)
@@ -1,5 +1,6 @@
 use std::io::fs::{mod, PathExtensions};
 use std::io;
+use std::io::{USER_RWX, File};
 use std::os;
 use std::str;
 use cargo::util::process;
@@ -73,3 +74,28 @@ test!(find_closest_dont_correct_nonsense {
                        .with_stderr("No such subcommand
 "));
 });
+
+test!(override_cargo_home {
+    let root = paths::root();
+    let my_home = root.join("my_home");
+    fs::mkdir(&my_home, USER_RWX).assert();
+    fs::mkdir(&my_home.join(".cargo"), USER_RWX).assert();
+    File::create(&my_home.join(".cargo/config")).write_str(r#"
+        [cargo-new]
+        name = "foo"
+        email = "bar"
+        git = false
+    "#).assert();
+
+    assert_that(process(cargo_dir().join("cargo")).unwrap()
+                .arg("new").arg("foo")
+                .cwd(paths::root())
+                .env("USER", Some("foo"))
+                .env("HOME", Some(paths::home()))
+                .env("CARGO_HOME", Some(my_home.clone())),
+                execs().with_status(0));
+
+    let toml = paths::root().join("foo/Cargo.toml");
+    let toml = File::open(&toml).read_to_string().assert();
+    assert!(toml.as_slice().contains(r#"authors = ["foo <bar>"]"#));
+});