cursus 0.7.0

Library crate for the cursus release management CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use std::sync::Arc;

use crate::command::CommandRunner;
use crate::command::test_support::RecordingCommandRunner;
use crate::forge::gitlab::remote::*;
use crate::git::GitWorkdir;
use crate::model::config::GitLabConfig;

fn workdir() -> crate::path::AbsolutePath {
	crate::path::AbsolutePath::new("/tmp").unwrap()
}

// --- GitLabProject::parse_url ---

#[tokio::test]
async fn parse_https_gitlab_com() {
	let result = GitLabProject::parse_url("https://gitlab.com/acme/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.com", "acme", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_https_self_managed() {
	let result = GitLabProject::parse_url("https://gitlab.example.com/acme/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.example.com", "acme", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_https_subgroup() {
	let result = GitLabProject::parse_url("https://gitlab.com/acme/sub/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.com", "acme/sub", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_https_deep_subgroup() {
	let result = GitLabProject::parse_url("https://gitlab.com/a/b/c/d/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.com", "a/b/c/d", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_https_without_git_suffix() {
	let result = GitLabProject::parse_url("https://gitlab.com/acme/app");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.com", "acme", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_https_with_port_preserves_port() {
	let result = GitLabProject::parse_url("https://gitlab.example.com:8443/acme/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.example.com:8443", "acme", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_https_no_group_returns_none() {
	// `host/project` alone — no group separator.
	assert!(GitLabProject::parse_url("https://gitlab.com/app.git").is_none());
}

#[tokio::test]
async fn parse_scp_gitlab_com() {
	let result = GitLabProject::parse_url("git@gitlab.com:acme/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.com", "acme", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_scp_self_managed() {
	let result = GitLabProject::parse_url("git@gitlab.example.com:acme/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.example.com", "acme", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_scp_subgroup() {
	let result = GitLabProject::parse_url("git@gitlab.com:acme/sub/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.com", "acme/sub", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_ssh_url_with_user() {
	let result = GitLabProject::parse_url("ssh://git@gitlab.example.com/acme/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.example.com", "acme", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_ssh_url_without_user() {
	let result = GitLabProject::parse_url("ssh://gitlab.example.com/acme/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.example.com", "acme", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_ssh_url_with_port_preserves_port() {
	let result = GitLabProject::parse_url("ssh://git@gitlab.example.com:2222/acme/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.example.com:2222", "acme", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_ssh_url_subgroup() {
	let result = GitLabProject::parse_url("ssh://git@gitlab.example.com/acme/sub/app.git");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.example.com", "acme/sub", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_empty_returns_none() {
	assert!(GitLabProject::parse_url("").is_none());
}

#[tokio::test]
async fn parse_unsupported_scheme_returns_none() {
	assert!(GitLabProject::parse_url("ftp://gitlab.example.com/acme/app.git").is_none());
}

#[tokio::test]
async fn parse_trims_whitespace() {
	let result = GitLabProject::parse_url("  git@gitlab.com:acme/app.git\n");
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.com", "acme", "app").unwrap())
	);
}

#[tokio::test]
async fn parse_https_malformed_port_returns_none() {
	assert!(GitLabProject::parse_url("https://gitlab.com:/acme/app.git").is_none());
}

// --- GitLabProject scheme handling ---

#[tokio::test]
async fn new_defaults_to_https_scheme() {
	let project = GitLabProject::new("gitlab.com", "acme", "app").unwrap();
	assert_eq!(project.scheme, "https");
}

#[tokio::test]
async fn with_scheme_accepts_https_and_http() {
	let project = GitLabProject::new("gitlab.com", "acme", "app").unwrap();
	assert_eq!(project.clone().with_scheme("https").scheme, "https");
	assert_eq!(project.with_scheme("http").scheme, "http");
}

#[tokio::test]
async fn with_scheme_ignores_unknown_schemes() {
	// A typo or attacker-controlled config value cannot inject `ftp` or
	// `file` into the composed asset URL — unknown schemes leave the field
	// at its previous value.
	let project = GitLabProject::new("gitlab.com", "acme", "app").unwrap();
	let updated = project.with_scheme("ftp");
	assert_eq!(updated.scheme, "https");
}

#[tokio::test]
async fn parse_url_preserves_http_scheme() {
	let project = GitLabProject::parse_url("http://gitlab.internal/acme/app.git").unwrap();
	assert_eq!(project.scheme, "http");
	assert_eq!(project.host, "gitlab.internal");
}

#[tokio::test]
async fn parse_url_defaults_https_for_ssh_remotes() {
	// SSH-cloned remotes still surface assets over HTTPS in practice.
	let project = GitLabProject::parse_url("git@gitlab.example.com:acme/app.git").unwrap();
	assert_eq!(project.scheme, "https");
}

// --- GitLabProject::new validation ---

#[tokio::test]
async fn new_accepts_valid_segments() {
	assert!(GitLabProject::new("gitlab.com", "acme", "my-app").is_ok());
	assert!(GitLabProject::new("gitlab.example.com", "acme/sub", "app.svc").is_ok());
	assert!(GitLabProject::new("gitlab.com", "Org123", "repo").is_ok());
}

#[tokio::test]
async fn new_accepts_explicit_port_in_host() {
	// Self-managed GitLab instances on non-standard ports.
	assert!(GitLabProject::new("gitlab.example.com:8443", "acme", "app").is_ok());
	assert!(GitLabProject::new("gitlab.example.com:22", "acme", "app").is_ok());
}

#[tokio::test]
async fn new_rejects_invalid_host() {
	assert!(GitLabProject::new("", "acme", "app").is_err());
	assert!(GitLabProject::new("git lab.com", "acme", "app").is_err());
	assert!(GitLabProject::new("../evil", "acme", "app").is_err());
}

#[tokio::test]
async fn new_rejects_malformed_port_in_host() {
	// Empty port, non-digit port, and double-colon forms must all fail.
	assert!(GitLabProject::new("gitlab.example.com:", "acme", "app").is_err());
	assert!(GitLabProject::new("gitlab.example.com:abc", "acme", "app").is_err());
	assert!(GitLabProject::new("gitlab.example.com:80:443", "acme", "app").is_err());
}

#[tokio::test]
async fn new_rejects_invalid_group() {
	assert!(GitLabProject::new("gitlab.com", "", "app").is_err());
	assert!(GitLabProject::new("gitlab.com", "ac me", "app").is_err());
	assert!(GitLabProject::new("gitlab.com", "ac/../evil", "app").is_err());
}

#[tokio::test]
async fn new_rejects_invalid_project() {
	assert!(GitLabProject::new("gitlab.com", "acme", "").is_err());
	assert!(GitLabProject::new("gitlab.com", "acme", "a/b").is_err());
	assert!(GitLabProject::new("gitlab.com", "acme", "../evil").is_err());
}

// --- GitLabProject::detect_in ---

#[tokio::test]
async fn detect_returns_project_for_https_remote() {
	let runner = Arc::new(
		RecordingCommandRunner::new(0).with_stdout(b"https://gitlab.com/acme/app.git\n".to_vec()),
	);
	let wd = workdir();
	let git = GitWorkdir::new(Arc::clone(&runner) as Arc<dyn CommandRunner>, wd.clone());
	let result = GitLabProject::detect_in(&git).await.unwrap();
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.com", "acme", "app").unwrap())
	);
}

#[tokio::test]
async fn detect_returns_project_for_ssh_remote() {
	let runner = Arc::new(
		RecordingCommandRunner::new(0)
			.with_stdout(b"git@gitlab.example.com:acme/sub/app.git\n".to_vec()),
	);
	let wd = workdir();
	let git = GitWorkdir::new(Arc::clone(&runner) as Arc<dyn CommandRunner>, wd.clone());
	let result = GitLabProject::detect_in(&git).await.unwrap();
	assert_eq!(
		result,
		Some(GitLabProject::new("gitlab.example.com", "acme/sub", "app").unwrap())
	);
}

#[tokio::test]
async fn detect_returns_none_when_git_fails() {
	let runner = Arc::new(RecordingCommandRunner::new(1));
	let wd = workdir();
	let git = GitWorkdir::new(Arc::clone(&runner) as Arc<dyn CommandRunner>, wd.clone());
	let result = GitLabProject::detect_in(&git).await.unwrap();
	assert_eq!(result, None);
}

// --- GitLabProject::resolve ---

fn make_config(group: Option<&str>, project: Option<&str>, host: &str) -> GitLabConfig {
	let mut config = GitLabConfig::enabled_config().with_host(host.to_string());
	if let Some(g) = group {
		config = config.with_group(g.to_string());
	}
	if let Some(p) = project {
		config = config.with_project(p.to_string());
	}
	config
}

#[tokio::test]
async fn resolve_uses_config_when_set() {
	let config = make_config(Some("acme"), Some("app"), "");
	let runner = Arc::new(RecordingCommandRunner::new(0));
	let wd = workdir();
	let git = GitWorkdir::new(Arc::clone(&runner) as Arc<dyn CommandRunner>, wd.clone());
	let project = GitLabProject::resolve(&config, &git).await.unwrap();
	assert_eq!(project.host, "gitlab.com");
	assert_eq!(project.group, "acme");
	assert_eq!(project.project, "app");
	assert!(runner.invocations().is_empty());
}

#[tokio::test]
async fn resolve_uses_config_host_when_set() {
	let config = make_config(Some("acme"), Some("app"), "https://gitlab.example.com/");
	let runner = Arc::new(RecordingCommandRunner::new(0));
	let wd = workdir();
	let git = GitWorkdir::new(Arc::clone(&runner) as Arc<dyn CommandRunner>, wd.clone());
	let project = GitLabProject::resolve(&config, &git).await.unwrap();
	assert_eq!(project.host, "gitlab.example.com");
	assert_eq!(project.scheme, "https");
}

#[tokio::test]
async fn resolve_preserves_http_scheme_from_config_host() {
	// Self-managed instances served over plain HTTP must propagate the
	// scheme through `resolve` so callers that do not perform the
	// binary-boundary endpoint override still produce reachable URLs.
	let config = make_config(Some("acme"), Some("app"), "http://gitlab.internal");
	let runner = Arc::new(RecordingCommandRunner::new(0));
	let wd = workdir();
	let git = GitWorkdir::new(Arc::clone(&runner) as Arc<dyn CommandRunner>, wd.clone());
	let project = GitLabProject::resolve(&config, &git).await.unwrap();
	assert_eq!(project.host, "gitlab.internal");
	assert_eq!(project.scheme, "http");
}

#[tokio::test]
async fn resolve_subgroup_from_config() {
	let config = make_config(Some("acme/sub"), Some("app"), "");
	let runner = Arc::new(RecordingCommandRunner::new(0));
	let wd = workdir();
	let git = GitWorkdir::new(Arc::clone(&runner) as Arc<dyn CommandRunner>, wd.clone());
	let project = GitLabProject::resolve(&config, &git).await.unwrap();
	assert_eq!(project.group, "acme/sub");
}

#[tokio::test]
async fn resolve_falls_back_to_git_remote() {
	let config = make_config(None, None, "");
	let runner = Arc::new(
		RecordingCommandRunner::new(0)
			.with_stdout(b"https://gitlab.com/myorg/myapp.git\n".to_vec()),
	);
	let wd = workdir();
	let git = GitWorkdir::new(Arc::clone(&runner) as Arc<dyn CommandRunner>, wd.clone());
	let project = GitLabProject::resolve(&config, &git).await.unwrap();
	assert_eq!(project.host, "gitlab.com");
	assert_eq!(project.group, "myorg");
	assert_eq!(project.project, "myapp");
}

#[tokio::test]
async fn resolve_errors_when_neither_config_nor_remote() {
	let config = make_config(None, None, "");
	let runner = Arc::new(RecordingCommandRunner::new(1));
	let wd = workdir();
	let git = GitWorkdir::new(Arc::clone(&runner) as Arc<dyn CommandRunner>, wd.clone());
	let result = GitLabProject::resolve(&config, &git).await;
	assert!(result.is_err());
	let msg = format!("{:#}", result.unwrap_err());
	assert!(
		msg.contains("Could not determine GitLab project"),
		"Expected project detection error, got: {msg}"
	);
}

#[tokio::test]
async fn resolve_errors_when_only_group_set() {
	let config = make_config(Some("acme"), None, "");
	let runner = Arc::new(RecordingCommandRunner::new(0));
	let wd = workdir();
	let git = GitWorkdir::new(Arc::clone(&runner) as Arc<dyn CommandRunner>, wd.clone());
	let result = GitLabProject::resolve(&config, &git).await;
	assert!(result.is_err());
	let msg = format!("{:#}", result.unwrap_err());
	assert!(
		msg.contains("must be set together"),
		"Expected partial config error, got: {msg}"
	);
}

#[tokio::test]
async fn resolve_errors_when_only_project_set() {
	let config = make_config(None, Some("app"), "");
	let runner = Arc::new(RecordingCommandRunner::new(0));
	let wd = workdir();
	let git = GitWorkdir::new(Arc::clone(&runner) as Arc<dyn CommandRunner>, wd.clone());
	let result = GitLabProject::resolve(&config, &git).await;
	assert!(result.is_err());
}

// --- scheme_and_host_from_config ---

#[test]
fn host_from_empty_returns_gitlab_com() {
	assert_eq!(
		scheme_and_host_from_config(""),
		("https".to_string(), "gitlab.com".to_string())
	);
}

#[test]
fn host_from_whitespace_returns_gitlab_com() {
	assert_eq!(
		scheme_and_host_from_config("   "),
		("https".to_string(), "gitlab.com".to_string())
	);
}

#[test]
fn host_strips_https_scheme() {
	assert_eq!(
		scheme_and_host_from_config("https://gitlab.example.com"),
		("https".to_string(), "gitlab.example.com".to_string())
	);
}

#[test]
fn host_preserves_http_scheme() {
	assert_eq!(
		scheme_and_host_from_config("http://gitlab.internal"),
		("http".to_string(), "gitlab.internal".to_string())
	);
}

#[test]
fn host_strips_trailing_slash() {
	assert_eq!(
		scheme_and_host_from_config("https://gitlab.example.com/"),
		("https".to_string(), "gitlab.example.com".to_string())
	);
}

#[test]
fn host_passes_through_bare_host() {
	assert_eq!(
		scheme_and_host_from_config("gitlab.example.com"),
		("https".to_string(), "gitlab.example.com".to_string())
	);
}

#[test]
fn host_strips_unknown_scheme_and_defaults_to_https() {
	// Mirrors `split_scheme` semantics so library-layer and binary-layer
	// scheme handling stay consistent. The bare host that comes back then
	// fails validation cleanly inside `GitLabProject::new`.
	assert_eq!(
		scheme_and_host_from_config("ftp://gitlab.internal"),
		("https".to_string(), "gitlab.internal".to_string())
	);
}