1pub mod cli;
2mod error;
3mod proxy;
4mod resource;
5pub use error::ConversionError;
6pub use proxy::Proxy;
7pub use resource::Resource;
8
9#[cfg(test)]
10mod tests {
11 use std::str::FromStr as _;
12
13 use super::*;
14
15 #[test]
16 fn test_file_resource_xget() {
17 let resource = Resource::file(
18 "easy-install".to_string(),
19 "easy-install".to_string(),
20 "main".to_string(),
21 "install.sh".to_string(),
22 );
23 let url = resource.url(&Proxy::Xget).unwrap();
24 assert_eq!(
25 url,
26 "https://xget.xi-xu.me/gh/easy-install/easy-install/raw/main/install.sh"
27 );
28 }
29
30 #[test]
31 fn test_file_resource_gh_proxy() {
32 let resource = Resource::file(
33 "owner".to_string(),
34 "repo".to_string(),
35 "main".to_string(),
36 "file.sh".to_string(),
37 );
38 let url = resource.url(&Proxy::GhProxy).unwrap();
39 assert_eq!(
40 url,
41 "https://gh-proxy.com/https://github.com/owner/repo/raw/main/file.sh"
42 );
43 }
44
45 #[test]
46 fn test_file_resource_jsdelivr() {
47 let resource = Resource::file(
48 "owner".to_string(),
49 "repo".to_string(),
50 "main".to_string(),
51 "file.js".to_string(),
52 );
53 let url = resource.url(&Proxy::Jsdelivr).unwrap();
54 assert_eq!(url, "https://cdn.jsdelivr.net/gh/owner/repo@main/file.js");
55 }
56
57 #[test]
58 fn test_file_resource_github() {
59 let resource = Resource::file(
60 "owner".to_string(),
61 "repo".to_string(),
62 "main".to_string(),
63 "file.sh".to_string(),
64 );
65 let url = resource.url(&Proxy::Github).unwrap();
66 assert_eq!(url, "https://github.com/owner/repo/raw/main/file.sh");
67 }
68
69 #[test]
70 fn test_release_resource_xget() {
71 let resource = Resource::release(
72 "easy-install".to_string(),
73 "easy-install".to_string(),
74 "nightly".to_string(),
75 "ei-aarch64-apple-darwin.tar.gz".to_string(),
76 );
77 let url = resource.url(&Proxy::Xget).unwrap();
78 assert_eq!(
79 url,
80 "https://xget.xi-xu.me/gh/easy-install/easy-install/releases/download/nightly/ei-aarch64-apple-darwin.tar.gz"
81 );
82 }
83
84 #[test]
85 fn test_release_resource_gh_proxy() {
86 let resource = Resource::release(
87 "owner".to_string(),
88 "repo".to_string(),
89 "v1.0.0".to_string(),
90 "app.tar.gz".to_string(),
91 );
92 let url = resource.url(&Proxy::GhProxy).unwrap();
93 assert_eq!(
94 url,
95 "https://gh-proxy.com/https://github.com/owner/repo/releases/download/v1.0.0/app.tar.gz"
96 );
97 }
98
99 #[test]
100 fn test_release_resource_jsdelivr_not_supported() {
101 let resource = Resource::release(
102 "owner".to_string(),
103 "repo".to_string(),
104 "v1.0.0".to_string(),
105 "app.tar.gz".to_string(),
106 );
107 let url = resource.url(&Proxy::Jsdelivr);
108 assert!(url.is_none(), "jsdelivr should not support release assets");
109 }
110
111 #[test]
112 fn test_release_resource_github() {
113 let resource = Resource::release(
114 "owner".to_string(),
115 "repo".to_string(),
116 "v1.0.0".to_string(),
117 "app.tar.gz".to_string(),
118 );
119 let url = resource.url(&Proxy::Github).unwrap();
120 assert_eq!(
121 url,
122 "https://github.com/owner/repo/releases/download/v1.0.0/app.tar.gz"
123 );
124 }
125
126 #[test]
127 fn test_proxy_type_from_str() {
128 assert_eq!(Proxy::from_str("github").unwrap(), Proxy::Github);
129 assert_eq!(Proxy::from_str("gh-proxy").unwrap(), Proxy::GhProxy);
130 assert_eq!(Proxy::from_str("xget").unwrap(), Proxy::Xget);
131 assert_eq!(Proxy::from_str("jsdelivr").unwrap(), Proxy::Jsdelivr);
132 assert_eq!(Proxy::from_str("statically").unwrap(), Proxy::Statically);
133 assert_eq!(Proxy::from_str("XGET").unwrap(), Proxy::Xget);
134 assert!(Proxy::from_str("invalid").is_err());
135 }
136
137 #[test]
138 fn test_file_resource_statically() {
139 let resource = Resource::file(
140 "easy-install".to_string(),
141 "easy-install".to_string(),
142 "main".to_string(),
143 "install.sh".to_string(),
144 );
145 let url = resource.url(&Proxy::Statically).unwrap();
146 assert_eq!(
147 url,
148 "https://cdn.statically.io/gh/easy-install/easy-install/main/install.sh"
149 );
150 }
151
152 #[test]
153 fn test_release_resource_statically_not_supported() {
154 let resource = Resource::release(
155 "owner".to_string(),
156 "repo".to_string(),
157 "v1.0.0".to_string(),
158 "app.tar.gz".to_string(),
159 );
160 let url = resource.url(&Proxy::Statically);
161 assert!(
162 url.is_none(),
163 "statically should not support release assets"
164 );
165 }
166
167 #[test]
168 fn test_nested_file_path() {
169 let resource = Resource::file(
170 "owner".to_string(),
171 "repo".to_string(),
172 "main".to_string(),
173 "src/lib/file.rs".to_string(),
174 );
175 let url = resource.url(&Proxy::Xget).unwrap();
176 assert_eq!(
177 url,
178 "https://xget.xi-xu.me/gh/owner/repo/raw/main/src/lib/file.rs"
179 );
180 }
181
182 #[test]
183 fn test_branch_with_refs() {
184 let resource = Resource::file(
185 "owner".to_string(),
186 "repo".to_string(),
187 "refs/heads/main".to_string(),
188 "file.sh".to_string(),
189 );
190 let url = resource.url(&Proxy::Github).unwrap();
191 assert_eq!(
192 url,
193 "https://github.com/owner/repo/raw/refs/heads/main/file.sh"
194 );
195 }
196
197 #[test]
198 fn test_parse_raw_file_url() {
199 let url = "https://github.com/easy-install/easy-install/raw/main/install.sh";
200 let resource = Resource::try_from(url).unwrap();
201 assert_eq!(
202 resource,
203 Resource::file(
204 "easy-install".to_string(),
205 "easy-install".to_string(),
206 "main".to_string(),
207 "install.sh".to_string()
208 )
209 );
210 }
211
212 #[test]
213 fn test_parse_blob_file_url() {
214 let url = "https://github.com/owner/repo/blob/main/src/lib.rs";
215 let resource = Resource::try_from(url).unwrap();
216 assert_eq!(
217 resource,
218 Resource::file(
219 "owner".to_string(),
220 "repo".to_string(),
221 "main".to_string(),
222 "src/lib.rs".to_string()
223 )
224 );
225 }
226
227 #[test]
228 fn test_parse_release_download_url() {
229 let url = "https://github.com/easy-install/easy-install/releases/download/nightly/ei-aarch64-apple-darwin.tar.gz";
230 let resource = Resource::try_from(url).unwrap();
231 assert_eq!(
232 resource,
233 Resource::release(
234 "easy-install".to_string(),
235 "easy-install".to_string(),
236 "nightly".to_string(),
237 "ei-aarch64-apple-darwin.tar.gz".to_string()
238 )
239 );
240 }
241
242 #[test]
243 fn test_parse_raw_file_with_refs() {
244 let url = "https://github.com/owner/repo/raw/refs/heads/main/file.sh";
245 let resource = Resource::try_from(url).unwrap();
246 assert_eq!(
247 resource,
248 Resource::file(
249 "owner".to_string(),
250 "repo".to_string(),
251 "refs/heads/main".to_string(),
252 "file.sh".to_string()
253 )
254 );
255 }
256
257 #[test]
258 fn test_parse_nested_path() {
259 let url = "https://github.com/owner/repo/raw/main/src/lib/file.rs";
260 let resource = Resource::try_from(url).unwrap();
261 assert_eq!(
262 resource,
263 Resource::file(
264 "owner".to_string(),
265 "repo".to_string(),
266 "main".to_string(),
267 "src/lib/file.rs".to_string()
268 )
269 );
270 }
271
272 #[test]
273 fn test_parse_invalid_url() {
274 let url = "https://example.com/file.sh";
275 let result = Resource::try_from(url);
276 assert!(result.is_err());
277 }
278
279 #[test]
280 fn test_parse_http_url() {
281 let url = "http://github.com/owner/repo/raw/main/file.sh";
282 let resource = Resource::try_from(url).unwrap();
283 assert_eq!(
284 resource,
285 Resource::file(
286 "owner".to_string(),
287 "repo".to_string(),
288 "main".to_string(),
289 "file.sh".to_string()
290 )
291 );
292 }
293 #[test]
294 fn test_parse_fish() {
295 let url = "https://github.com/fish-shell/fish-shell/releases/download/4.1.2/fish-4.1.2-linux-aarch64.tar.xz";
296 let resource = Resource::try_from(url).unwrap();
297 assert_eq!(
298 resource,
299 Resource::release(
300 "fish-shell".to_string(),
301 "fish-shell".to_string(),
302 "4.1.2".to_string(),
303 "fish-4.1.2-linux-aarch64.tar.xz".to_string()
304 )
305 );
306 }
307}