1use std::{path::Path, time::Duration};
2
3use gpui::{ModelHandle, TestAppContext, ViewHandle};
4
5use project::{Entry, Project, ProjectPath, Worktree};
6use workspace::{AppState, Workspace};
7
8pub struct TerminalTestContext<'a> {
9 pub cx: &'a mut TestAppContext,
10}
11
12impl<'a> TerminalTestContext<'a> {
13 pub fn new(cx: &'a mut TestAppContext) -> Self {
14 cx.set_condition_duration(Some(Duration::from_secs(5)));
15
16 TerminalTestContext { cx }
17 }
18
19 ///Creates a worktree with 1 file: /root.txt
20 pub async fn blank_workspace(&mut self) -> (ModelHandle<Project>, ViewHandle<Workspace>) {
21 let params = self.cx.update(AppState::test);
22
23 let project = Project::test(params.fs.clone(), [], self.cx).await;
24 let (_, workspace) = self.cx.add_window(|cx| Workspace::new(project.clone(), cx));
25
26 (project, workspace)
27 }
28
29 ///Creates a worktree with 1 folder: /root{suffix}/
30 pub async fn create_folder_wt(
31 &mut self,
32 project: ModelHandle<Project>,
33 path: impl AsRef<Path>,
34 ) -> (ModelHandle<Worktree>, Entry) {
35 self.create_wt(project, true, path).await
36 }
37
38 ///Creates a worktree with 1 file: /root{suffix}.txt
39 pub async fn create_file_wt(
40 &mut self,
41 project: ModelHandle<Project>,
42 path: impl AsRef<Path>,
43 ) -> (ModelHandle<Worktree>, Entry) {
44 self.create_wt(project, false, path).await
45 }
46
47 async fn create_wt(
48 &mut self,
49 project: ModelHandle<Project>,
50 is_dir: bool,
51 path: impl AsRef<Path>,
52 ) -> (ModelHandle<Worktree>, Entry) {
53 let (wt, _) = project
54 .update(self.cx, |project, cx| {
55 project.find_or_create_local_worktree(path, true, cx)
56 })
57 .await
58 .unwrap();
59
60 let entry = self
61 .cx
62 .update(|cx| {
63 wt.update(cx, |wt, cx| {
64 wt.as_local()
65 .unwrap()
66 .create_entry(Path::new(""), is_dir, cx)
67 })
68 })
69 .await
70 .unwrap();
71
72 (wt, entry)
73 }
74
75 pub fn insert_active_entry_for(
76 &mut self,
77 wt: ModelHandle<Worktree>,
78 entry: Entry,
79 project: ModelHandle<Project>,
80 ) {
81 self.cx.update(|cx| {
82 let p = ProjectPath {
83 worktree_id: wt.read(cx).id(),
84 path: entry.path,
85 };
86 project.update(cx, |project, cx| project.set_active_path(Some(p), cx));
87 });
88 }
89}
90
91impl<'a> Drop for TerminalTestContext<'a> {
92 fn drop(&mut self) {
93 self.cx.set_condition_duration(None);
94 }
95}