Skip to content

Commit 5d005a7

Browse files
authored
Canonicalize paths when running tests (#23655)
In the Windows test environment, the paths generated by `temp_tree()` are symlink paths, which causes certain tests to fail. I later noticed that when opening a project, we seem to always use `canonicalize` to normalize the paths, as shown here: #21039. This PR adopts a similar approach for the test environment to address the issue. Release Notes: - N/A
1 parent 0f8e2e3 commit 5d005a7

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

crates/extension_host/src/extension_store_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::{
2525
sync::Arc,
2626
};
2727
use theme::ThemeRegistry;
28-
use util::test::temp_tree;
28+
use util::test::TempTree;
2929

3030
#[cfg(test)]
3131
#[ctor::ctor]
@@ -470,11 +470,11 @@ async fn test_extension_store_with_test_extension(cx: &mut TestAppContext) {
470470
let test_extension_dir = root_dir.join("extensions").join(test_extension_id);
471471

472472
let fs = Arc::new(RealFs::default());
473-
let extensions_dir = temp_tree(json!({
473+
let extensions_dir = TempTree::new(json!({
474474
"installed": {},
475475
"work": {}
476476
}));
477-
let project_dir = temp_tree(json!({
477+
let project_dir = TempTree::new(json!({
478478
"test.gleam": ""
479479
}));
480480

crates/project/src/project_tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use unindent::Unindent as _;
2727
use util::{
2828
assert_set_eq,
2929
paths::{replace_path_separator, PathMatcher},
30-
test::temp_tree,
30+
test::TempTree,
3131
TryFutureExt as _,
3232
};
3333

@@ -67,7 +67,7 @@ async fn test_symlinks(cx: &mut gpui::TestAppContext) {
6767
init_test(cx);
6868
cx.executor().allow_parking();
6969

70-
let dir = temp_tree(json!({
70+
let dir = TempTree::new(json!({
7171
"root": {
7272
"apple": "",
7373
"banana": {
@@ -106,7 +106,7 @@ async fn test_symlinks(cx: &mut gpui::TestAppContext) {
106106
async fn test_editorconfig_support(cx: &mut gpui::TestAppContext) {
107107
init_test(cx);
108108

109-
let dir = temp_tree(json!({
109+
let dir = TempTree::new(json!({
110110
".editorconfig": r#"
111111
root = true
112112
[*.rs]
@@ -3187,7 +3187,7 @@ async fn test_rescan_and_remote_updates(cx: &mut gpui::TestAppContext) {
31873187
init_test(cx);
31883188
cx.executor().allow_parking();
31893189

3190-
let dir = temp_tree(json!({
3190+
let dir = TempTree::new(json!({
31913191
"a": {
31923192
"file1": "",
31933193
"file2": "",

crates/util/src/test.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,26 @@ use tempfile::TempDir;
1111
pub use assertions::*;
1212
pub use marked_text::*;
1313

14-
pub fn temp_tree(tree: serde_json::Value) -> TempDir {
15-
let dir = TempDir::new().unwrap();
16-
write_tree(dir.path(), tree);
17-
dir
14+
pub struct TempTree {
15+
_temp_dir: TempDir,
16+
path: PathBuf,
17+
}
18+
19+
impl TempTree {
20+
pub fn new(tree: serde_json::Value) -> Self {
21+
let dir = TempDir::new().unwrap();
22+
let path = std::fs::canonicalize(dir.path()).unwrap();
23+
write_tree(path.as_path(), tree);
24+
25+
Self {
26+
_temp_dir: dir,
27+
path,
28+
}
29+
}
30+
31+
pub fn path(&self) -> &Path {
32+
self.path.as_path()
33+
}
1834
}
1935

2036
fn write_tree(path: &Path, tree: serde_json::Value) {

crates/worktree/src/worktree_tests.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::{
2525
path::{Path, PathBuf},
2626
sync::Arc,
2727
};
28-
use util::{test::temp_tree, ResultExt};
28+
use util::{test::TempTree, ResultExt};
2929

3030
#[gpui::test]
3131
async fn test_traversal(cx: &mut TestAppContext) {
@@ -352,7 +352,7 @@ async fn test_renaming_case_only(cx: &mut TestAppContext) {
352352
const NEW_NAME: &str = "AAA.rs";
353353

354354
let fs = Arc::new(RealFs::default());
355-
let temp_root = temp_tree(json!({
355+
let temp_root = TempTree::new(json!({
356356
OLD_NAME: "",
357357
}));
358358

@@ -846,7 +846,7 @@ async fn test_update_gitignore(cx: &mut TestAppContext) {
846846
async fn test_write_file(cx: &mut TestAppContext) {
847847
init_test(cx);
848848
cx.executor().allow_parking();
849-
let dir = temp_tree(json!({
849+
let dir = TempTree::new(json!({
850850
".git": {},
851851
".gitignore": "ignored-dir\n",
852852
"tracked-dir": {},
@@ -903,7 +903,7 @@ async fn test_write_file(cx: &mut TestAppContext) {
903903
async fn test_file_scan_inclusions(cx: &mut TestAppContext) {
904904
init_test(cx);
905905
cx.executor().allow_parking();
906-
let dir = temp_tree(json!({
906+
let dir = TempTree::new(json!({
907907
".gitignore": "**/target\n/node_modules\ntop_level.txt\n",
908908
"target": {
909909
"index": "blah2"
@@ -973,7 +973,7 @@ async fn test_file_scan_inclusions(cx: &mut TestAppContext) {
973973
async fn test_file_scan_exclusions_overrules_inclusions(cx: &mut TestAppContext) {
974974
init_test(cx);
975975
cx.executor().allow_parking();
976-
let dir = temp_tree(json!({
976+
let dir = TempTree::new(json!({
977977
".gitignore": "**/target\n/node_modules\n",
978978
"target": {
979979
"index": "blah2"
@@ -1031,7 +1031,7 @@ async fn test_file_scan_exclusions_overrules_inclusions(cx: &mut TestAppContext)
10311031
async fn test_file_scan_inclusions_reindexes_on_setting_change(cx: &mut TestAppContext) {
10321032
init_test(cx);
10331033
cx.executor().allow_parking();
1034-
let dir = temp_tree(json!({
1034+
let dir = TempTree::new(json!({
10351035
".gitignore": "**/target\n/node_modules/\n",
10361036
"target": {
10371037
"index": "blah2"
@@ -1108,7 +1108,7 @@ async fn test_file_scan_inclusions_reindexes_on_setting_change(cx: &mut TestAppC
11081108
async fn test_file_scan_exclusions(cx: &mut TestAppContext) {
11091109
init_test(cx);
11101110
cx.executor().allow_parking();
1111-
let dir = temp_tree(json!({
1111+
let dir = TempTree::new(json!({
11121112
".gitignore": "**/target\n/node_modules\n",
11131113
"target": {
11141114
"index": "blah2"
@@ -1206,7 +1206,7 @@ async fn test_file_scan_exclusions(cx: &mut TestAppContext) {
12061206
async fn test_fs_events_in_exclusions(cx: &mut TestAppContext) {
12071207
init_test(cx);
12081208
cx.executor().allow_parking();
1209-
let dir = temp_tree(json!({
1209+
let dir = TempTree::new(json!({
12101210
".git": {
12111211
"HEAD": "ref: refs/heads/main\n",
12121212
"foo": "bar",
@@ -1349,7 +1349,7 @@ async fn test_fs_events_in_exclusions(cx: &mut TestAppContext) {
13491349
async fn test_fs_events_in_dot_git_worktree(cx: &mut TestAppContext) {
13501350
init_test(cx);
13511351
cx.executor().allow_parking();
1352-
let dir = temp_tree(json!({
1352+
let dir = TempTree::new(json!({
13531353
".git": {
13541354
"HEAD": "ref: refs/heads/main\n",
13551355
"foo": "foo contents",
@@ -1562,7 +1562,7 @@ async fn test_create_dir_all_on_create_entry(cx: &mut TestAppContext) {
15621562
});
15631563

15641564
let fs_real = Arc::new(RealFs::default());
1565-
let temp_root = temp_tree(json!({
1565+
let temp_root = TempTree::new(json!({
15661566
"a": {}
15671567
}));
15681568

@@ -2160,7 +2160,7 @@ const CONFLICT: FileStatus = FileStatus::Unmerged(UnmergedStatus {
21602160
async fn test_rename_work_directory(cx: &mut TestAppContext) {
21612161
init_test(cx);
21622162
cx.executor().allow_parking();
2163-
let root = temp_tree(json!({
2163+
let root = TempTree::new(json!({
21642164
"projects": {
21652165
"project1": {
21662166
"a": "",
@@ -2231,7 +2231,7 @@ async fn test_rename_work_directory(cx: &mut TestAppContext) {
22312231
async fn test_git_repository_for_path(cx: &mut TestAppContext) {
22322232
init_test(cx);
22332233
cx.executor().allow_parking();
2234-
let root = temp_tree(json!({
2234+
let root = TempTree::new(json!({
22352235
"c.txt": "",
22362236
"dir1": {
22372237
".git": {},
@@ -2341,7 +2341,7 @@ async fn test_file_status(cx: &mut TestAppContext) {
23412341
cx.executor().allow_parking();
23422342
const IGNORE_RULE: &str = "**/target";
23432343

2344-
let root = temp_tree(json!({
2344+
let root = TempTree::new(json!({
23452345
"project": {
23462346
"a.txt": "a",
23472347
"b.txt": "bb",
@@ -2530,7 +2530,7 @@ async fn test_git_repository_status(cx: &mut TestAppContext) {
25302530
init_test(cx);
25312531
cx.executor().allow_parking();
25322532

2533-
let root = temp_tree(json!({
2533+
let root = TempTree::new(json!({
25342534
"project": {
25352535
"a.txt": "a", // Modified
25362536
"b.txt": "bb", // Added
@@ -2644,7 +2644,7 @@ async fn test_git_status_postprocessing(cx: &mut TestAppContext) {
26442644
init_test(cx);
26452645
cx.executor().allow_parking();
26462646

2647-
let root = temp_tree(json!({
2647+
let root = TempTree::new(json!({
26482648
"project": {
26492649
"sub": {},
26502650
"a.txt": "",
@@ -2700,7 +2700,7 @@ async fn test_repository_subfolder_git_status(cx: &mut TestAppContext) {
27002700
init_test(cx);
27012701
cx.executor().allow_parking();
27022702

2703-
let root = temp_tree(json!({
2703+
let root = TempTree::new(json!({
27042704
"my-repo": {
27052705
// .git folder will go here
27062706
"a.txt": "a",

0 commit comments

Comments
 (0)