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
25 .cx
26 .add_window(|cx| Workspace::new(project.clone(), |_, _| unimplemented!(), cx));
27
28 (project, workspace)
29 }
30
31 ///Creates a worktree with 1 folder: /root{suffix}/
32 pub async fn create_folder_wt(
33 &mut self,
34 project: ModelHandle<Project>,
35 path: impl AsRef<Path>,
36 ) -> (ModelHandle<Worktree>, Entry) {
37 self.create_wt(project, true, path).await
38 }
39
40 ///Creates a worktree with 1 file: /root{suffix}.txt
41 pub async fn create_file_wt(
42 &mut self,
43 project: ModelHandle<Project>,
44 path: impl AsRef<Path>,
45 ) -> (ModelHandle<Worktree>, Entry) {
46 self.create_wt(project, false, path).await
47 }
48
49 async fn create_wt(
50 &mut self,
51 project: ModelHandle<Project>,
52 is_dir: bool,
53 path: impl AsRef<Path>,
54 ) -> (ModelHandle<Worktree>, Entry) {
55 let (wt, _) = project
56 .update(self.cx, |project, cx| {
57 project.find_or_create_local_worktree(path, true, cx)
58 })
59 .await
60 .unwrap();
61
62 let entry = self
63 .cx
64 .update(|cx| {
65 wt.update(cx, |wt, cx| {
66 wt.as_local()
67 .unwrap()
68 .create_entry(Path::new(""), is_dir, cx)
69 })
70 })
71 .await
72 .unwrap();
73
74 (wt, entry)
75 }
76
77 pub fn insert_active_entry_for(
78 &mut self,
79 wt: ModelHandle<Worktree>,
80 entry: Entry,
81 project: ModelHandle<Project>,
82 ) {
83 self.cx.update(|cx| {
84 let p = ProjectPath {
85 worktree_id: wt.read(cx).id(),
86 path: entry.path,
87 };
88 project.update(cx, |project, cx| project.set_active_path(Some(p), cx));
89 });
90 }
91}
92
93impl<'a> Drop for TerminalTestContext<'a> {
94 fn drop(&mut self) {
95 self.cx.set_condition_duration(None);
96 }
97}