Skip to content

Commit 4196ea0

Browse files
committed
Auto merge of #14270 - epage:test-home, r=weihanglo
fix(test): Move 'cargo_home' from 'install' to 'paths' ### What does this PR try to resolve? This is used outside of `cargo install` contexts and this makes it more discoverable for those use cases. That I found places that weren't using `cargo_home` but could helps illustrate the point. ### How should we test and review this PR? ### Additional information
2 parents dad331c + 49deefe commit 4196ea0

16 files changed

+146
-147
lines changed
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use crate::paths;
21
use std::env::consts::EXE_SUFFIX;
3-
use std::path::{Path, PathBuf};
2+
use std::path::Path;
43

54
/// Used by `cargo install` tests to assert an executable binary
65
/// has been installed. Example usage:
76
/// ```no_run
87
/// use cargo_test_support::install::assert_has_installed_exe;
9-
/// use cargo_test_support::install::cargo_home;
8+
/// use cargo_test_support::paths;
109
///
11-
/// assert_has_installed_exe(cargo_home(), "foo");
10+
/// assert_has_installed_exe(paths::cargo_home(), "foo");
1211
/// ```
1312
#[track_caller]
1413
pub fn assert_has_installed_exe<P: AsRef<Path>>(path: P, name: &'static str) {
@@ -24,10 +23,6 @@ fn check_has_installed_exe<P: AsRef<Path>>(path: P, name: &'static str) -> bool
2423
path.as_ref().join("bin").join(exe(name)).is_file()
2524
}
2625

27-
pub fn cargo_home() -> PathBuf {
28-
paths::home().join(".cargo")
29-
}
30-
3126
pub fn exe(name: &str) -> String {
3227
format!("{}{}", name, EXE_SUFFIX)
3328
}

crates/cargo-test-support/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ pub trait TestEnvCommandExt: Sized {
13081308
self = self
13091309
.current_dir(&paths::root())
13101310
.env("HOME", paths::home())
1311-
.env("CARGO_HOME", paths::home().join(".cargo"))
1311+
.env("CARGO_HOME", paths::cargo_home())
13121312
.env("__CARGO_TEST_ROOT", paths::global_root())
13131313
// Force Cargo to think it's on the stable channel for all tests, this
13141314
// should hopefully not surprise us as we add cargo features over time and

crates/cargo-test-support/src/paths.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ pub fn home() -> PathBuf {
110110
path
111111
}
112112

113+
pub fn cargo_home() -> PathBuf {
114+
home().join(".cargo")
115+
}
116+
113117
pub trait CargoPathExt {
114118
fn to_url(&self) -> url::Url;
115119

crates/cargo-test-support/src/registry.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl RegistryBuilder {
279279
/// Initializes the registry.
280280
#[must_use]
281281
pub fn build(self) -> TestRegistry {
282-
let config_path = paths::home().join(".cargo/config.toml");
282+
let config_path = paths::cargo_home().join("config.toml");
283283
t!(fs::create_dir_all(config_path.parent().unwrap()));
284284
let prefix = if let Some(alternative) = &self.alternative {
285285
format!("{alternative}-")
@@ -391,7 +391,7 @@ impl RegistryBuilder {
391391
}
392392

393393
if self.configure_token {
394-
let credentials = paths::home().join(".cargo/credentials.toml");
394+
let credentials = paths::cargo_home().join("credentials.toml");
395395
match &registry.token {
396396
Token::Plaintext(token) => {
397397
if let Some(alternative) = &self.alternative {
@@ -1195,7 +1195,7 @@ impl Package {
11951195
/// Creates a new package builder.
11961196
/// Call `publish()` to finalize and build the package.
11971197
pub fn new(name: &str, vers: &str) -> Package {
1198-
let config = paths::home().join(".cargo/config.toml");
1198+
let config = paths::cargo_home().join("config.toml");
11991199
if !config.exists() {
12001200
init();
12011201
}

tests/testsuite/binary_name.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use cargo_test_support::install::assert_has_installed_exe;
44
use cargo_test_support::install::assert_has_not_installed_exe;
5-
use cargo_test_support::install::cargo_home;
5+
use cargo_test_support::paths;
66
use cargo_test_support::prelude::*;
77
use cargo_test_support::project;
88
use cargo_test_support::str;
@@ -221,7 +221,7 @@ Hello, crabs!
221221
.masquerade_as_nightly_cargo(&["different-binary-name"])
222222
.run();
223223

224-
assert_has_installed_exe(cargo_home(), "007bar");
224+
assert_has_installed_exe(paths::cargo_home(), "007bar");
225225

226226
p.cargo("uninstall")
227227
.with_stderr_data(str![[r#"
@@ -231,7 +231,7 @@ Hello, crabs!
231231
.masquerade_as_nightly_cargo(&["different-binary-name"])
232232
.run();
233233

234-
assert_has_not_installed_exe(cargo_home(), "007bar");
234+
assert_has_not_installed_exe(paths::cargo_home(), "007bar");
235235
}
236236

237237
#[cargo_test]

tests/testsuite/build_script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::io;
66
use std::thread;
77

88
use cargo_test_support::compare::assert_e2e;
9-
use cargo_test_support::install::cargo_home;
9+
use cargo_test_support::paths::cargo_home;
1010
use cargo_test_support::prelude::*;
1111
use cargo_test_support::registry::Package;
1212
use cargo_test_support::str;

tests/testsuite/concurrent.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use std::{env, str};
99

1010
use cargo_test_support::cargo_process;
1111
use cargo_test_support::git;
12-
use cargo_test_support::install::{assert_has_installed_exe, cargo_home};
12+
use cargo_test_support::install::assert_has_installed_exe;
13+
use cargo_test_support::paths;
1314
use cargo_test_support::prelude::*;
1415
use cargo_test_support::registry::Package;
1516
use cargo_test_support::str;
@@ -46,8 +47,8 @@ fn multiple_installs() {
4647
execs().run_output(&a);
4748
execs().run_output(&b);
4849

49-
assert_has_installed_exe(cargo_home(), "foo");
50-
assert_has_installed_exe(cargo_home(), "bar");
50+
assert_has_installed_exe(paths::cargo_home(), "foo");
51+
assert_has_installed_exe(paths::cargo_home(), "bar");
5152
}
5253

5354
#[cargo_test]
@@ -75,8 +76,8 @@ fn concurrent_installs() {
7576
execs().run_output(&a);
7677
execs().run_output(&b);
7778

78-
assert_has_installed_exe(cargo_home(), "foo");
79-
assert_has_installed_exe(cargo_home(), "bar");
79+
assert_has_installed_exe(paths::cargo_home(), "foo");
80+
assert_has_installed_exe(paths::cargo_home(), "bar");
8081
}
8182

8283
#[cargo_test]
@@ -104,7 +105,7 @@ fn one_install_should_be_bad() {
104105
execs().run_output(&a);
105106
execs().run_output(&b);
106107

107-
assert_has_installed_exe(cargo_home(), "foo");
108+
assert_has_installed_exe(paths::cargo_home(), "foo");
108109
}
109110

110111
#[cargo_test]

tests/testsuite/features2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::fs::File;
44

55
use cargo_test_support::cross_compile::{self, alternate};
6-
use cargo_test_support::install::cargo_home;
6+
use cargo_test_support::paths;
77
use cargo_test_support::prelude::*;
88
use cargo_test_support::publish::validate_crate_contents;
99
use cargo_test_support::registry::{Dependency, Package};
@@ -2208,8 +2208,8 @@ fn minimal_download() {
22082208
.build();
22092209

22102210
let clear = || {
2211-
cargo_home().join("registry/cache").rm_rf();
2212-
cargo_home().join("registry/src").rm_rf();
2211+
paths::cargo_home().join("registry/cache").rm_rf();
2212+
paths::cargo_home().join("registry/src").rm_rf();
22132213
p.build_dir().rm_rf();
22142214
};
22152215

0 commit comments

Comments
 (0)