proto_pdk_test_utils/
wrapper.rs

1use proto_core::{Backend, Tool};
2use proto_pdk_api::*;
3
4#[derive(Debug)]
5pub struct WasmTestWrapper {
6    pub tool: Tool,
7}
8
9impl WasmTestWrapper {
10    pub async fn set_backend(&mut self, backend: Backend) {
11        self.tool.backend = Some(backend);
12        self.tool.register_backend().await.unwrap();
13    }
14
15    pub async fn detect_version_files(&self, mut input: DetectVersionInput) -> DetectVersionOutput {
16        input.context = self.prepare_unresolved_context(input.context);
17
18        self.tool
19            .plugin
20            .call_func_with(PluginFunction::DetectVersionFiles, input)
21            .await
22            .unwrap()
23    }
24
25    pub async fn download_prebuilt(
26        &self,
27        mut input: DownloadPrebuiltInput,
28    ) -> DownloadPrebuiltOutput {
29        input.context = self.prepare_context(input.context);
30
31        self.tool
32            .plugin
33            .call_func_with(PluginFunction::DownloadPrebuilt, input)
34            .await
35            .unwrap()
36    }
37
38    pub async fn load_versions(&self, mut input: LoadVersionsInput) -> LoadVersionsOutput {
39        input.context = self.prepare_unresolved_context(input.context);
40
41        self.tool
42            .plugin
43            .call_func_with(PluginFunction::LoadVersions, input)
44            .await
45            .unwrap()
46    }
47
48    pub async fn locate_executables(
49        &self,
50        mut input: LocateExecutablesInput,
51    ) -> LocateExecutablesOutput {
52        input.context = self.prepare_context(input.context);
53
54        self.tool
55            .plugin
56            .call_func_with(PluginFunction::LocateExecutables, input)
57            .await
58            .unwrap()
59    }
60
61    pub async fn native_install(&self, mut input: NativeInstallInput) -> NativeInstallOutput {
62        input.context = self.prepare_context(input.context);
63
64        self.tool
65            .plugin
66            .call_func_with(PluginFunction::NativeInstall, input)
67            .await
68            .unwrap()
69    }
70
71    pub async fn native_uninstall(&self, mut input: NativeUninstallInput) -> NativeUninstallOutput {
72        input.context = self.prepare_context(input.context);
73
74        self.tool
75            .plugin
76            .call_func_with(PluginFunction::NativeUninstall, input)
77            .await
78            .unwrap()
79    }
80
81    pub async fn parse_version_file(
82        &self,
83        mut input: ParseVersionFileInput,
84    ) -> ParseVersionFileOutput {
85        input.context = self.prepare_unresolved_context(input.context);
86        input.path = self.tool.to_virtual_path(&input.path);
87
88        self.tool
89            .plugin
90            .call_func_with(PluginFunction::ParseVersionFile, input)
91            .await
92            .unwrap()
93    }
94
95    pub async fn pre_install(&self, mut input: InstallHook) {
96        input.context = self.prepare_context(input.context);
97
98        self.tool
99            .plugin
100            .call_func_without_output(HookFunction::PreInstall, input)
101            .await
102            .unwrap();
103    }
104
105    pub async fn pre_run(&self, mut input: RunHook) -> RunHookResult {
106        input.context = self.prepare_context(input.context);
107
108        self.tool
109            .plugin
110            .call_func_with(HookFunction::PreRun, input)
111            .await
112            .unwrap()
113    }
114
115    pub async fn post_install(&self, mut input: InstallHook) {
116        input.context = self.prepare_context(input.context);
117
118        self.tool
119            .plugin
120            .call_func_without_output(HookFunction::PostInstall, input)
121            .await
122            .unwrap();
123    }
124
125    pub async fn register_backend(&self, mut input: RegisterBackendInput) -> RegisterBackendOutput {
126        input.context = self.prepare_unresolved_context(input.context);
127
128        self.tool
129            .plugin
130            .call_func_with(PluginFunction::RegisterBackend, input)
131            .await
132            .unwrap()
133    }
134
135    pub async fn register_tool(&self, input: RegisterToolInput) -> RegisterToolOutput {
136        self.tool
137            .plugin
138            .call_func_with(PluginFunction::RegisterTool, input)
139            .await
140            .unwrap()
141    }
142
143    pub async fn resolve_version(&self, mut input: ResolveVersionInput) -> ResolveVersionOutput {
144        input.context = self.prepare_unresolved_context(input.context);
145
146        self.tool
147            .plugin
148            .call_func_with(PluginFunction::ResolveVersion, input)
149            .await
150            .unwrap()
151    }
152
153    pub async fn sync_manifest(&self, mut input: SyncManifestInput) -> SyncManifestOutput {
154        input.context = self.prepare_context(input.context);
155
156        self.tool
157            .plugin
158            .call_func_with(PluginFunction::SyncManifest, input)
159            .await
160            .unwrap()
161    }
162
163    pub async fn sync_shell_profile(
164        &self,
165        mut input: SyncShellProfileInput,
166    ) -> SyncShellProfileOutput {
167        input.context = self.prepare_context(input.context);
168
169        self.tool
170            .plugin
171            .call_func_with(PluginFunction::SyncShellProfile, input)
172            .await
173            .unwrap()
174    }
175
176    pub async fn unpack_archive(&self, mut input: UnpackArchiveInput) {
177        input.input_file = self.tool.to_virtual_path(&input.input_file);
178        input.output_dir = self.tool.to_virtual_path(&input.output_dir);
179
180        let _: EmptyInput = self
181            .tool
182            .plugin
183            .call_func_with(PluginFunction::UnpackArchive, input)
184            .await
185            .unwrap();
186    }
187
188    pub async fn verify_checksum(&self, mut input: VerifyChecksumInput) -> VerifyChecksumOutput {
189        input.checksum_file = self.tool.to_virtual_path(&input.checksum_file);
190        input.download_file = self.tool.to_virtual_path(&input.download_file);
191
192        self.tool
193            .plugin
194            .call_func_with(PluginFunction::VerifyChecksum, input)
195            .await
196            .unwrap()
197    }
198
199    fn prepare_context(&self, context: ToolContext) -> ToolContext {
200        let tool_dir = if context.tool_dir.any_path().components().count() == 0 {
201            self.tool.get_product_dir()
202        } else {
203            context.tool_dir.any_path().to_path_buf()
204        };
205
206        let temp_dir = if context.temp_dir.any_path().components().count() == 0 {
207            self.tool.get_temp_dir()
208        } else {
209            context.temp_dir.any_path().to_path_buf()
210        };
211
212        ToolContext {
213            temp_dir: self.tool.to_virtual_path(&temp_dir),
214            tool_dir: self.tool.to_virtual_path(&tool_dir),
215            ..context
216        }
217    }
218
219    fn prepare_unresolved_context(&self, context: ToolUnresolvedContext) -> ToolUnresolvedContext {
220        let temp_dir = if context.temp_dir.any_path().components().count() == 0 {
221            self.tool.get_temp_dir()
222        } else {
223            context.temp_dir.any_path().to_path_buf()
224        };
225
226        ToolUnresolvedContext {
227            temp_dir: self.tool.to_virtual_path(&temp_dir),
228            ..context
229        }
230    }
231}