cursus 0.9.2

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
use std::path::Path;
use std::sync::Arc;

use crate::command::test_support::RecordingCommandRunner;
use crate::command::{CommandRunner, shell_program};
use crate::env::Env;
use crate::filesystem::LocalFilesystem;
use crate::forge::CodeForgeClient;
use crate::forge::test_support::RecordingCodeForgeClient;

fn recording_env(exit_code: i32) -> (Arc<RecordingCommandRunner>, Env, tempfile::TempDir) {
	let dir = tempfile::tempdir().unwrap();
	std::fs::create_dir(dir.path().join(".git")).unwrap();
	let runner = Arc::new(RecordingCommandRunner::new(exit_code));
	let git = Arc::new(crate::git::GitWorkdir::new(
		Arc::clone(&runner) as Arc<dyn CommandRunner>,
		crate::path::AbsolutePath::new(dir.path()).unwrap(),
	));
	let env = Env::new(
		Arc::clone(&runner) as Arc<dyn CommandRunner>,
		Arc::new(LocalFilesystem),
		git,
	);
	(runner, env, dir)
}

#[test]
fn new_has_no_editor_or_code_forge_client() {
	let (_, env, _dir) = recording_env(0);
	assert!(env.editor().is_none());
	assert!(env.code_forge_client().is_err());
}

#[test]
fn new_has_false_auth_flags() {
	let (_, env, _dir) = recording_env(0);
	assert!(!env.oidc_environment());
	assert!(!env.node_auth_token_present());
	assert!(!env.cargo_registry_token_present());
}

#[test]
fn with_oidc_environment_sets_flag() {
	let (_, env, _dir) = recording_env(0);
	let env = env.with_oidc_environment(true);
	assert!(env.oidc_environment());
}

#[test]
fn with_node_auth_token_present_sets_flag() {
	let (_, env, _dir) = recording_env(0);
	let env = env.with_node_auth_token_present(true);
	assert!(env.node_auth_token_present());
}

#[test]
fn with_cargo_registry_token_present_sets_flag() {
	let (_, env, _dir) = recording_env(0);
	let env = env.with_cargo_registry_token_present(true);
	assert!(env.cargo_registry_token_present());
}

#[test]
fn new_has_default_locale() {
	let (_, env, _dir) = recording_env(0);
	assert_eq!(env.locale(), crate::locale::DEFAULT_LOCALE);
}

#[test]
fn with_locale_sets_locale() {
	let (_, env, _dir) = recording_env(0);
	let env = env.with_locale("pt-BR".to_string());
	assert_eq!(env.locale(), "pt-BR");
}

#[test]
fn with_dry_run_runner_preserves_auth_flags() {
	let (_, env, _dir) = recording_env(0);
	let env = env
		.with_oidc_environment(true)
		.with_node_auth_token_present(true)
		.with_cargo_registry_token_present(true);
	let dry_env = env.with_dry_run_runner();
	assert!(dry_env.oidc_environment());
	assert!(dry_env.node_auth_token_present());
	assert!(dry_env.cargo_registry_token_present());
}

#[test]
fn with_dry_run_runner_preserves_locale() {
	let (_, env, _dir) = recording_env(0);
	let env = env.with_locale("fr".to_string());
	let dry_env = env.with_dry_run_runner();
	assert_eq!(dry_env.locale(), "fr");
}

#[test]
fn with_dry_run_runner_preserves_git() {
	let (_, env, _dir) = recording_env(0);
	let path = env.git().path().clone();
	let dry_env = env.with_dry_run_runner();
	assert_eq!(dry_env.git().path(), &path);
}

#[test]
fn with_editor_sets_editor() {
	let (_, env, _dir) = recording_env(0);
	let env = env.with_editor("vim".to_string());
	assert_eq!(env.editor(), Some("vim"));
}

#[test]
fn with_code_forge_client_sets_client() {
	let (_, env, _dir) = recording_env(0);
	let client = Arc::new(RecordingCodeForgeClient::new()) as Arc<dyn CodeForgeClient>;
	let env = env.with_code_forge_client(Arc::clone(&client));
	assert!(env.code_forge_client().is_ok());
}

#[test]
fn with_editor_opt_some_sets_editor() {
	let (_, env, _dir) = recording_env(0);
	let env = env.with_editor_opt(Some("nano".to_string()));
	assert_eq!(env.editor(), Some("nano"));
}

#[test]
fn with_editor_opt_none_clears_editor() {
	let (_, env, _dir) = recording_env(0);
	let env = env.with_editor("vim".to_string()).with_editor_opt(None);
	assert!(env.editor().is_none());
}

#[test]
fn with_code_forge_client_result_ok_sets_client() {
	let (_, env, _dir) = recording_env(0);
	let client = Arc::new(RecordingCodeForgeClient::new()) as Arc<dyn CodeForgeClient>;
	let env = env.with_code_forge_client_result(Ok(client));
	assert!(env.code_forge_client().is_ok());
}

#[test]
fn with_code_forge_client_result_err_clears_client() {
	let (_, env, _dir) = recording_env(0);
	let client = Arc::new(RecordingCodeForgeClient::new()) as Arc<dyn CodeForgeClient>;
	let env = env
		.with_code_forge_client(client)
		.with_code_forge_client_result(Err("no token".into()));
	assert!(env.code_forge_client().is_err());
}

#[test]
fn code_forge_name_defaults_to_configured_forge() {
	let (_, env, _dir) = recording_env(0);
	assert_eq!(env.code_forge_name(), "the configured forge");
}

#[test]
fn with_code_forge_client_captures_forge_name() {
	let (_, env, _dir) = recording_env(0);
	let client = Arc::new(RecordingCodeForgeClient::new().with_forge_name("GitLab"))
		as Arc<dyn CodeForgeClient>;
	let env = env.with_code_forge_client(client);
	assert_eq!(env.code_forge_name(), "GitLab");
}

#[test]
fn with_code_forge_client_result_ok_captures_forge_name() {
	let (_, env, _dir) = recording_env(0);
	let client = Arc::new(RecordingCodeForgeClient::new().with_forge_name("GitLab"))
		as Arc<dyn CodeForgeClient>;
	let env = env.with_code_forge_client_result(Ok(client));
	assert_eq!(env.code_forge_name(), "GitLab");
}

#[test]
fn with_code_forge_client_result_err_leaves_default_name() {
	// Initial Err sets no client and keeps the default name; the contract
	// the orchestration layer relies on is that `code_forge_name` is the
	// label of the *active* client when one is configured.
	let (_, env, _dir) = recording_env(0);
	let env = env.with_code_forge_client_result(Err("no token".into()));
	assert_eq!(env.code_forge_name(), "the configured forge");
}

#[tokio::test]
async fn run_delegates_to_runner() {
	let (runner, env, _dir) = recording_env(0);
	env.run("echo", &["hello"], Path::new(".")).await.unwrap();
	let invocations = runner.invocations();
	assert_eq!(invocations[0].program, "echo");
	assert_eq!(invocations[0].args, ["hello"]);
}

#[tokio::test]
async fn run_mut_delegates_to_runner() {
	let (runner, env, _dir) = recording_env(0);
	env.run_mut("git", &["commit", "-m", "msg"], Path::new("."))
		.await
		.unwrap();
	let invocations = runner.invocations();
	assert_eq!(invocations[0].program, "git");
	assert_eq!(invocations[0].args, ["commit", "-m", "msg"]);
}

#[tokio::test]
async fn run_streaming_delegates_to_runner() {
	let (runner, env, _dir) = recording_env(0);
	env.run_streaming("npm install", Path::new("."))
		.await
		.unwrap();
	let invocations = runner.invocations();
	assert_eq!(invocations[0].program, shell_program());
	assert!(invocations[0].is_shell);
	assert!(invocations[0].is_streaming);
}

#[tokio::test]
async fn run_interactive_delegates_to_runner() {
	let (runner, env, _dir) = recording_env(0);
	env.run_interactive("vim", &[], Path::new("."))
		.await
		.unwrap();
	let invocations = runner.invocations();
	assert_eq!(invocations[0].program, "vim");
	assert!(invocations[0].is_interactive);
}

#[tokio::test]
async fn with_dry_run_runner_suppresses_run_mut() {
	let (runner, env, _dir) = recording_env(0);
	let dry_env = env.with_dry_run_runner();
	dry_env
		.run_mut("git", &["push", "origin", "HEAD"], Path::new("."))
		.await
		.unwrap();
	// The inner recording runner must NOT have been called (DryRunCommandRunner intercepts)
	assert!(runner.invocations().is_empty());
}

#[tokio::test]
async fn with_dry_run_runner_still_forwards_run() {
	let (runner, env, _dir) = recording_env(0);
	let dry_env = env.with_dry_run_runner();
	dry_env
		.run("git", &["status"], Path::new("."))
		.await
		.unwrap();
	// Read-only run is forwarded to the inner runner
	assert_eq!(runner.invocations().len(), 1);
	assert_eq!(runner.invocations()[0].program, "git");
}

// run_editor_on tests

#[tokio::test]
async fn run_editor_on_uses_editor_when_set() {
	let workdir = tempfile::tempdir().unwrap();
	let path = workdir.path().join("config.toml");
	std::fs::write(&path, "").unwrap();

	let (runner, env, _dir) = recording_env(0);
	let env = env.with_editor("vim".to_string());
	env.run_editor_on(&path, workdir.path()).await.unwrap();

	let invocations = runner.invocations();
	let editor_call = invocations
		.iter()
		.find(|i| i.is_interactive && i.is_shell)
		.expect("Expected a shell interactive invocation");
	let expected = format!("vim {}", crate::shell::shell_quote(&path.to_string_lossy()));
	assert_eq!(editor_call.args[1], expected);
}

#[tokio::test]
async fn run_editor_on_ignores_empty_editor_string() {
	let workdir = tempfile::tempdir().unwrap();
	let path = workdir.path().join("config.toml");
	std::fs::write(&path, "").unwrap();

	// Empty editor → falls back to find_default_editor → runner returns 0 → "nano"
	let (runner, env, _dir) = recording_env(0);
	let env = env.with_editor(String::new());
	env.run_editor_on(&path, workdir.path()).await.unwrap();

	let invocations = runner.invocations();
	let editor_call = invocations
		.iter()
		.find(|i| i.is_interactive && i.is_shell)
		.expect("Expected a shell interactive invocation");
	let expected = format!(
		"nano {}",
		crate::shell::shell_quote(&path.to_string_lossy())
	);
	assert_eq!(editor_call.args[1], expected, "Should fall back to nano");
}

#[tokio::test]
async fn run_editor_on_nonzero_exit_returns_error() {
	let workdir = tempfile::tempdir().unwrap();
	let path = workdir.path().join("config.toml");
	std::fs::write(&path, "").unwrap();

	let (_, env, _dir) = recording_env(1);
	let env = env.with_editor("vim".to_string());
	let result = env.run_editor_on(&path, workdir.path()).await;

	assert!(result.is_err());
	assert!(
		result
			.unwrap_err()
			.to_string()
			.contains("Editor exited with status")
	);
}

#[tokio::test]
async fn run_editor_on_falls_back_to_default_editor() {
	let workdir = tempfile::tempdir().unwrap();
	let path = workdir.path().join("config.toml");
	std::fs::write(&path, "").unwrap();

	// No editor set, runner exit_code=0 → which nano succeeds → "nano"
	let (runner, env, _dir) = recording_env(0);
	env.run_editor_on(&path, workdir.path()).await.unwrap();

	let invocations = runner.invocations();
	let editor_call = invocations
		.iter()
		.find(|i| i.is_interactive && i.is_shell)
		.expect("Expected a shell interactive invocation");
	let expected = format!(
		"nano {}",
		crate::shell::shell_quote(&path.to_string_lossy())
	);
	assert_eq!(editor_call.args[1], expected);
}

#[tokio::test]
async fn run_editor_on_no_editor_found_returns_error() {
	let workdir = tempfile::tempdir().unwrap();
	let path = workdir.path().join("config.toml");
	std::fs::write(&path, "").unwrap();

	// Runner exit_code=1 → all which calls fail → no default found
	let (_, env, _dir) = recording_env(1);
	let result = env.run_editor_on(&path, workdir.path()).await;

	assert!(result.is_err());
	assert!(result.unwrap_err().to_string().contains("No editor found"));
}

#[tokio::test]
async fn run_editor_on_uses_provided_cwd() {
	let workdir = tempfile::tempdir().unwrap();
	let cursus_dir = workdir.path().join(".cursus");
	std::fs::create_dir_all(&cursus_dir).unwrap();
	let path = cursus_dir.join("config.toml");
	std::fs::write(&path, "").unwrap();

	let (runner, env, _dir) = recording_env(0);
	let env = env.with_editor("vim".to_string());
	env.run_editor_on(&path, workdir.path()).await.unwrap();

	let invocations = runner.invocations();
	let editor_call = invocations
		.iter()
		.find(|i| i.is_interactive && i.is_shell)
		.expect("Expected a shell interactive editor invocation");
	assert_eq!(
		editor_call.cwd,
		workdir.path(),
		"Editor should be invoked with the provided cwd, not the file's parent"
	);
}

#[tokio::test]
async fn run_editor_on_handles_multi_word_editor() {
	let workdir = tempfile::tempdir().unwrap();
	let path = workdir.path().join("config.toml");
	std::fs::write(&path, "").unwrap();

	let (runner, env, _dir) = recording_env(0);
	let env = env.with_editor("code --wait".to_string());
	env.run_editor_on(&path, workdir.path()).await.unwrap();

	let invocations = runner.invocations();
	let editor_call = invocations
		.iter()
		.find(|i| i.is_interactive && i.is_shell)
		.expect("Expected a shell interactive invocation");
	let expected = format!(
		"code --wait {}",
		crate::shell::shell_quote(&path.to_string_lossy())
	);
	assert_eq!(editor_call.args[1], expected);
}

#[tokio::test]
async fn run_editor_on_handles_path_with_single_quote() {
	let workdir = tempfile::tempdir().unwrap();
	// Path whose name contains a single quote — tests the '\\'' escaping logic.
	let path = workdir.path().join("it's a file.toml");
	std::fs::write(&path, "").unwrap();

	let (runner, env, _dir) = recording_env(0);
	let env = env.with_editor("vim".to_string());
	env.run_editor_on(&path, workdir.path()).await.unwrap();

	let invocations = runner.invocations();
	let editor_call = invocations
		.iter()
		.find(|i| i.is_interactive && i.is_shell)
		.expect("Expected a shell interactive invocation");
	let expected = format!("vim {}", crate::shell::shell_quote(&path.to_string_lossy()));
	assert_eq!(editor_call.args[1], expected);
}

#[tokio::test]
async fn run_shell_interactive_delegates_to_runner() {
	let (runner, env, _dir) = recording_env(0);
	let cwd = tempfile::tempdir().unwrap();
	env.run_shell_interactive("echo hello", cwd.path())
		.await
		.unwrap();
	let invocations = runner.invocations();
	assert_eq!(invocations.len(), 1);
	assert!(invocations[0].is_shell);
	assert!(invocations[0].is_interactive);
}