Skip to content

Commit db28490

Browse files
[ty] Override home directory in ty tests (#23724)
Closes astral-sh/ty#2942.
1 parent 5f0fd91 commit db28490

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

crates/ty/tests/cli/main.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,8 @@ fn user_configuration() -> anyhow::Result<()> {
383383
),
384384
])?;
385385

386-
let config_directory = case.root().join("home/.config");
387-
let config_env_var = if cfg!(windows) {
388-
"APPDATA"
389-
} else {
390-
"XDG_CONFIG_HOME"
391-
};
386+
let config_directory = case.user_config_directory();
387+
let config_env_var = user_config_directory_env_var();
392388

393389
assert_cmd_snapshot!(
394390
case.command().current_dir(case.root().join("project")).env(config_env_var, config_directory.as_os_str()),
@@ -888,13 +884,16 @@ impl CliTest {
888884
// Canonicalize the tempdir path because macos uses symlinks for tempdirs
889885
// and that doesn't play well with our snapshot filtering.
890886
// Simplify with dunce because otherwise we get UNC paths on Windows.
891-
let project_dir = dunce::simplified(
887+
let temp_dir_path = dunce::simplified(
892888
&temp_dir
893889
.path()
894890
.canonicalize()
895-
.context("Failed to canonicalize project path")?,
891+
.context("Failed to canonicalize temporary directory path")?,
896892
)
897893
.to_path_buf();
894+
let project_dir = temp_dir_path.join("project");
895+
std::fs::create_dir_all(&project_dir)
896+
.with_context(|| format!("Failed to create directory `{}`", project_dir.display()))?;
898897

899898
let mut settings = insta::Settings::clone_current();
900899
settings.add_filter(&tempdir_filter(&project_dir), "<temp_dir>/");
@@ -1016,9 +1015,21 @@ impl CliTest {
10161015

10171016
// Unset all environment variables because they can affect test behavior.
10181017
command.env_clear();
1018+
// Point user config discovery at a test-local directory to avoid picking up host config.
1019+
command.env(
1020+
user_config_directory_env_var(),
1021+
self.user_config_directory(),
1022+
);
10191023

10201024
command
10211025
}
1026+
1027+
fn user_config_directory(&self) -> PathBuf {
1028+
self.project_dir
1029+
.parent()
1030+
.expect("project directory always has a parent")
1031+
.join("home/.config")
1032+
}
10221033
}
10231034

10241035
fn tempdir_filter(path: &Path) -> String {
@@ -1032,3 +1043,11 @@ fn site_packages_filter(python_version: &str) -> String {
10321043
format!("lib/python{}/site-packages", regex::escape(python_version))
10331044
}
10341045
}
1046+
1047+
fn user_config_directory_env_var() -> &'static str {
1048+
if cfg!(windows) {
1049+
"APPDATA"
1050+
} else {
1051+
"XDG_CONFIG_HOME"
1052+
}
1053+
}

0 commit comments

Comments
 (0)