Skip to content

Commit

Permalink
Auto merge of #14270 - epage:test-home, r=weihanglo
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jul 19, 2024
2 parents dad331c + 49deefe commit 4196ea0
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 147 deletions.
11 changes: 3 additions & 8 deletions crates/cargo-test-support/src/install.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::paths;
use std::env::consts::EXE_SUFFIX;
use std::path::{Path, PathBuf};
use std::path::Path;

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

pub fn cargo_home() -> PathBuf {
paths::home().join(".cargo")
}

pub fn exe(name: &str) -> String {
format!("{}{}", name, EXE_SUFFIX)
}
2 changes: 1 addition & 1 deletion crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ pub trait TestEnvCommandExt: Sized {
self = self
.current_dir(&paths::root())
.env("HOME", paths::home())
.env("CARGO_HOME", paths::home().join(".cargo"))
.env("CARGO_HOME", paths::cargo_home())
.env("__CARGO_TEST_ROOT", paths::global_root())
// Force Cargo to think it's on the stable channel for all tests, this
// should hopefully not surprise us as we add cargo features over time and
Expand Down
4 changes: 4 additions & 0 deletions crates/cargo-test-support/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ pub fn home() -> PathBuf {
path
}

pub fn cargo_home() -> PathBuf {
home().join(".cargo")
}

pub trait CargoPathExt {
fn to_url(&self) -> url::Url;

Expand Down
6 changes: 3 additions & 3 deletions crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl RegistryBuilder {
/// Initializes the registry.
#[must_use]
pub fn build(self) -> TestRegistry {
let config_path = paths::home().join(".cargo/config.toml");
let config_path = paths::cargo_home().join("config.toml");
t!(fs::create_dir_all(config_path.parent().unwrap()));
let prefix = if let Some(alternative) = &self.alternative {
format!("{alternative}-")
Expand Down Expand Up @@ -391,7 +391,7 @@ impl RegistryBuilder {
}

if self.configure_token {
let credentials = paths::home().join(".cargo/credentials.toml");
let credentials = paths::cargo_home().join("credentials.toml");
match &registry.token {
Token::Plaintext(token) => {
if let Some(alternative) = &self.alternative {
Expand Down Expand Up @@ -1195,7 +1195,7 @@ impl Package {
/// Creates a new package builder.
/// Call `publish()` to finalize and build the package.
pub fn new(name: &str, vers: &str) -> Package {
let config = paths::home().join(".cargo/config.toml");
let config = paths::cargo_home().join("config.toml");
if !config.exists() {
init();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/binary_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use cargo_test_support::install::assert_has_installed_exe;
use cargo_test_support::install::assert_has_not_installed_exe;
use cargo_test_support::install::cargo_home;
use cargo_test_support::paths;
use cargo_test_support::prelude::*;
use cargo_test_support::project;
use cargo_test_support::str;
Expand Down Expand Up @@ -221,7 +221,7 @@ Hello, crabs!
.masquerade_as_nightly_cargo(&["different-binary-name"])
.run();

assert_has_installed_exe(cargo_home(), "007bar");
assert_has_installed_exe(paths::cargo_home(), "007bar");

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

assert_has_not_installed_exe(cargo_home(), "007bar");
assert_has_not_installed_exe(paths::cargo_home(), "007bar");
}

#[cargo_test]
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io;
use std::thread;

use cargo_test_support::compare::assert_e2e;
use cargo_test_support::install::cargo_home;
use cargo_test_support::paths::cargo_home;
use cargo_test_support::prelude::*;
use cargo_test_support::registry::Package;
use cargo_test_support::str;
Expand Down
13 changes: 7 additions & 6 deletions tests/testsuite/concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use std::{env, str};

use cargo_test_support::cargo_process;
use cargo_test_support::git;
use cargo_test_support::install::{assert_has_installed_exe, cargo_home};
use cargo_test_support::install::assert_has_installed_exe;
use cargo_test_support::paths;
use cargo_test_support::prelude::*;
use cargo_test_support::registry::Package;
use cargo_test_support::str;
Expand Down Expand Up @@ -46,8 +47,8 @@ fn multiple_installs() {
execs().run_output(&a);
execs().run_output(&b);

assert_has_installed_exe(cargo_home(), "foo");
assert_has_installed_exe(cargo_home(), "bar");
assert_has_installed_exe(paths::cargo_home(), "foo");
assert_has_installed_exe(paths::cargo_home(), "bar");
}

#[cargo_test]
Expand Down Expand Up @@ -75,8 +76,8 @@ fn concurrent_installs() {
execs().run_output(&a);
execs().run_output(&b);

assert_has_installed_exe(cargo_home(), "foo");
assert_has_installed_exe(cargo_home(), "bar");
assert_has_installed_exe(paths::cargo_home(), "foo");
assert_has_installed_exe(paths::cargo_home(), "bar");
}

#[cargo_test]
Expand Down Expand Up @@ -104,7 +105,7 @@ fn one_install_should_be_bad() {
execs().run_output(&a);
execs().run_output(&b);

assert_has_installed_exe(cargo_home(), "foo");
assert_has_installed_exe(paths::cargo_home(), "foo");
}

#[cargo_test]
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/features2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::fs::File;

use cargo_test_support::cross_compile::{self, alternate};
use cargo_test_support::install::cargo_home;
use cargo_test_support::paths;
use cargo_test_support::prelude::*;
use cargo_test_support::publish::validate_crate_contents;
use cargo_test_support::registry::{Dependency, Package};
Expand Down Expand Up @@ -2208,8 +2208,8 @@ fn minimal_download() {
.build();

let clear = || {
cargo_home().join("registry/cache").rm_rf();
cargo_home().join("registry/src").rm_rf();
paths::cargo_home().join("registry/cache").rm_rf();
paths::cargo_home().join("registry/src").rm_rf();
p.build_dir().rm_rf();
};

Expand Down
Loading

0 comments on commit 4196ea0

Please sign in to comment.