cursus 0.3.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
//! Abstraction over command execution for testability.
//!
//! Provides the [`CommandRunner`] trait so that code that shells out to external
//! programs can be tested without hitting real registries or remotes.
//!
//! [`RealCommandRunner`] is the production implementation used by the binary.
//! [`DryRunCommandRunner`] is a decorator that skips mutating operations.
//! [`test_support::RecordingCommandRunner`] is a fake implementation for unit tests.

use std::path::Path;
use std::process::Output;
use std::sync::Arc;

use anyhow::Context;
use async_trait::async_trait;

/// Returns the platform shell executable: `cmd.exe` on Windows, `/bin/sh` on Unix.
pub(crate) fn shell_program() -> &'static str {
	if cfg!(windows) { "cmd.exe" } else { "/bin/sh" }
}

/// Returns the platform shell command flag: `/C` on Windows, `-c` on Unix.
pub(crate) fn shell_flag() -> &'static str {
	if cfg!(windows) { "/C" } else { "-c" }
}

/// Abstracts command execution to allow testing without real processes.
///
/// All commands run with the specified working directory (`cwd`), removing
/// the need for `-C` flags before execution.
///
/// Methods are split into read-only (`run`) and mutating
/// (`run_mut`, `run_interactive`, `run_shell_interactive`, `run_streaming`) variants. The
/// [`DryRunCommandRunner`] decorator intercepts mutating variants and
/// suppresses them, while read-only variants always execute.
#[async_trait]
pub trait CommandRunner: Send + Sync + std::fmt::Debug {
	/// Runs a program with the given arguments in the specified directory.
	///
	/// Read-only — always executes, even in dry-run mode.
	async fn run(&self, program: &str, args: &[&str], cwd: &Path) -> anyhow::Result<Output>;

	/// Runs a program with the given arguments and records it as a mutating operation.
	///
	/// Mutating — skipped by [`DryRunCommandRunner`]. Use this for commands that
	/// modify state (e.g. `git add`, `git commit`, `cargo publish`).
	async fn run_mut(&self, program: &str, args: &[&str], cwd: &Path) -> anyhow::Result<Output>;

	/// Runs a program with inherited stdin/stdout/stderr for interactive use (e.g. editors).
	///
	/// Mutating — skipped by [`DryRunCommandRunner`]. Unlike [`run`], this does not
	/// capture output — the child process shares the terminal directly. Returns the
	/// exit status of the child process.
	async fn run_interactive(
		&self,
		program: &str,
		args: &[&str],
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus>;

	/// Runs a shell command via the platform shell with inherited stdin/stdout/stderr.
	///
	/// Mutating — skipped by [`DryRunCommandRunner`]. Use this for user-supplied commands
	/// that must be shell-interpreted (e.g. `EDITOR="code --wait"`) and need to interact
	/// with the terminal directly, including reading from stdin.
	async fn run_shell_interactive(
		&self,
		command: &str,
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus>;

	/// Runs a shell command via the platform shell, streaming output live to the terminal.
	///
	/// Mutating — skipped by [`DryRunCommandRunner`]. The child process's `stdout` and
	/// `stderr` are inherited from the parent so all output appears immediately on the
	/// user's terminal. `stdin` is set to null so the command cannot block waiting for
	/// user input. Returns the exit status of the child process.
	///
	/// Use this for user-configurable shell commands (e.g. `github.build_command`,
	/// `npm.lock_command`) where live progress feedback matters more than captured output.
	/// [`RealCommandRunner`] emits `log::info!("Running: {command}")` before spawning;
	/// callers must not add their own pre-log.
	async fn run_streaming(
		&self,
		command: &str,
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus>;
}

// --- Helpers for constructing synthetic outputs ---

fn make_success_output() -> Output {
	#[cfg(unix)]
	let status = {
		use std::os::unix::process::ExitStatusExt;
		std::process::ExitStatus::from_raw(0)
	};
	#[cfg(windows)]
	let status = {
		use std::os::windows::process::ExitStatusExt;
		std::process::ExitStatus::from_raw(0)
	};
	Output {
		status,
		stdout: Vec::new(),
		stderr: Vec::new(),
	}
}

fn make_success_exit_status() -> std::process::ExitStatus {
	make_success_output().status
}

// ---

/// A command runner decorator that logs each invocation at `debug` level.
///
/// Wraps any [`CommandRunner`] and emits a `log::debug!` message before
/// delegating to the inner runner. The global log level filter suppresses
/// these messages when the level is above `Debug`.
#[derive(Debug)]
pub struct VerboseCommandRunner<R: CommandRunner> {
	inner: R,
}

impl<R: CommandRunner> VerboseCommandRunner<R> {
	/// Creates a new `VerboseCommandRunner` wrapping the given runner.
	pub fn new(inner: R) -> Self {
		Self { inner }
	}
}

#[async_trait]
impl<R: CommandRunner> CommandRunner for VerboseCommandRunner<R> {
	async fn run(&self, program: &str, args: &[&str], cwd: &Path) -> anyhow::Result<Output> {
		log::debug!("run: {program} {} (cwd: {})", args.join(" "), cwd.display());
		self.inner.run(program, args, cwd).await
	}

	async fn run_mut(&self, program: &str, args: &[&str], cwd: &Path) -> anyhow::Result<Output> {
		log::debug!(
			"run_mut: {program} {} (cwd: {})",
			args.join(" "),
			cwd.display()
		);
		self.inner.run_mut(program, args, cwd).await
	}

	async fn run_interactive(
		&self,
		program: &str,
		args: &[&str],
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus> {
		log::debug!(
			"run_interactive: {program} {} (cwd: {})",
			args.join(" "),
			cwd.display()
		);
		self.inner.run_interactive(program, args, cwd).await
	}

	async fn run_shell_interactive(
		&self,
		command: &str,
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus> {
		log::debug!(
			"run_shell_interactive: {command:?} (cwd: {})",
			cwd.display()
		);
		self.inner.run_shell_interactive(command, cwd).await
	}

	async fn run_streaming(
		&self,
		command: &str,
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus> {
		log::debug!("run_streaming: {command:?} (cwd: {})", cwd.display());
		self.inner.run_streaming(command, cwd).await
	}
}

/// A command runner that executes real system processes.
///
/// This is the production implementation, used by the binary and by integration
/// tests that require actual shell commands (git, cargo, npm) to run.
#[derive(Debug)]
pub struct RealCommandRunner;

#[async_trait]
impl CommandRunner for RealCommandRunner {
	async fn run(&self, program: &str, args: &[&str], cwd: &Path) -> anyhow::Result<Output> {
		tokio::process::Command::new(program)
			.args(args)
			.current_dir(cwd)
			.output()
			.await
			.with_context(|| format!("Failed to run '{program}'"))
	}

	async fn run_mut(&self, program: &str, args: &[&str], cwd: &Path) -> anyhow::Result<Output> {
		self.run(program, args, cwd).await
	}

	async fn run_interactive(
		&self,
		program: &str,
		args: &[&str],
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus> {
		let program = program.to_string();
		let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();
		let cwd = cwd.to_path_buf();
		tokio::task::spawn_blocking(move || {
			std::process::Command::new(&program)
				.args(&args)
				.current_dir(&cwd)
				.status()
				.with_context(|| format!("Failed to run '{program}'"))
		})
		.await
		.context("spawn_blocking panicked")?
	}

	async fn run_shell_interactive(
		&self,
		command: &str,
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus> {
		let command = command.to_string();
		let cwd = cwd.to_path_buf();
		tokio::task::spawn_blocking(move || {
			std::process::Command::new(shell_program())
				.args([shell_flag(), &command])
				.current_dir(&cwd)
				.status()
				.with_context(|| format!("Failed to run shell command: '{command}'"))
		})
		.await
		.context("spawn_blocking panicked")?
	}

	async fn run_streaming(
		&self,
		command: &str,
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus> {
		use std::process::Stdio;
		log::info!("Running: {command}");
		tokio::process::Command::new(shell_program())
			.args([shell_flag(), command])
			.current_dir(cwd)
			.stdin(Stdio::null())
			.stdout(Stdio::inherit())
			.stderr(Stdio::inherit())
			.status()
			.await
			.with_context(|| format!("Failed to run streaming command: '{command}'"))
	}
}

/// A command runner decorator that suppresses all mutating operations in dry-run mode.
///
/// Wraps any [`CommandRunner`] and intercepts `run_mut`, `run_interactive`,
/// `run_shell_interactive`, and `run_streaming` calls, logging them at `info` level
/// and returning a synthetic success result without running the actual command.
/// Read-only operations (`run`) are always forwarded to the inner runner.
///
/// Compose this at the outermost layer when dry-run mode is active.
#[derive(Debug)]
pub struct DryRunCommandRunner {
	inner: Arc<dyn CommandRunner>,
}

impl DryRunCommandRunner {
	/// Creates a new `DryRunCommandRunner` wrapping the given runner.
	pub fn new(inner: Arc<dyn CommandRunner>) -> Self {
		Self { inner }
	}
}

#[async_trait]
impl CommandRunner for DryRunCommandRunner {
	async fn run(&self, program: &str, args: &[&str], cwd: &Path) -> anyhow::Result<Output> {
		self.inner.run(program, args, cwd).await
	}

	async fn run_mut(&self, program: &str, args: &[&str], cwd: &Path) -> anyhow::Result<Output> {
		log::info!(
			"[dry-run] would run: {program} {} (cwd: {})",
			args.join(" "),
			cwd.display()
		);
		Ok(make_success_output())
	}

	async fn run_interactive(
		&self,
		program: &str,
		args: &[&str],
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus> {
		log::info!(
			"[dry-run] would run (interactive): {program} {} (cwd: {})",
			args.join(" "),
			cwd.display()
		);
		Ok(make_success_exit_status())
	}

	async fn run_shell_interactive(
		&self,
		command: &str,
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus> {
		log::info!(
			"[dry-run] would run (shell interactive): {command:?} (cwd: {})",
			cwd.display()
		);
		Ok(make_success_exit_status())
	}

	async fn run_streaming(
		&self,
		command: &str,
		cwd: &Path,
	) -> anyhow::Result<std::process::ExitStatus> {
		log::info!(
			"[dry-run] would run (streaming): {command:?} (cwd: {})",
			cwd.display()
		);
		Ok(make_success_exit_status())
	}
}

#[cfg(test)]
mod verbose_tests {
	use std::path::Path;

	use super::*;
	use crate::command::test_support::RecordingCommandRunner;
	use crate::test_logging::{init_test_logger, take_logs};

	#[tokio::test]
	async fn verbose_runner_delegates_run_to_inner() {
		init_test_logger();
		let inner = RecordingCommandRunner::new(0);
		let runner = VerboseCommandRunner::new(inner);
		let cwd = Path::new("/tmp");
		let _ = runner.run("git", &["status"], cwd).await;
		let invocations = runner.inner.invocations();
		assert_eq!(invocations.len(), 1);
		assert_eq!(invocations[0].program, "git");
		assert_eq!(invocations[0].args, vec!["status"]);
	}

	#[tokio::test]
	async fn verbose_runner_delegates_run_interactive_to_inner() {
		init_test_logger();
		let inner = RecordingCommandRunner::new(0);
		let runner = VerboseCommandRunner::new(inner);
		let cwd = Path::new("/tmp");
		let _ = runner.run_interactive("vim", &["file.txt"], cwd).await;
		let invocations = runner.inner.invocations();
		assert_eq!(invocations.len(), 1);
		assert!(invocations[0].is_interactive);
		assert_eq!(invocations[0].program, "vim");
	}

	#[tokio::test]
	async fn verbose_runner_delegates_run_mut_to_inner() {
		init_test_logger();
		let inner = RecordingCommandRunner::new(0);
		let runner = VerboseCommandRunner::new(inner);
		let cwd = Path::new("/tmp");
		let _ = runner.run_mut("git", &["commit", "-m", "msg"], cwd).await;
		let invocations = runner.inner.invocations();
		assert_eq!(invocations.len(), 1);
		assert_eq!(invocations[0].program, "git");
		assert_eq!(invocations[0].args, vec!["commit", "-m", "msg"]);
	}

	#[tokio::test]
	async fn verbose_runner_logs_run_with_program_and_args() {
		init_test_logger();
		let _ = take_logs(); // clear any accumulated messages
		let inner = RecordingCommandRunner::new(0);
		let runner = VerboseCommandRunner::new(inner);
		let cwd = Path::new("/some/dir");
		let _ = runner.run("cargo", &["build", "--release"], cwd).await;
		let logs = take_logs();
		let msg = logs
			.iter()
			.find(|(_, m)| m.contains("cargo"))
			.map(|(_, m)| m.as_str())
			.expect("expected a log message about cargo");
		assert!(msg.contains("build"), "log should contain args: {msg}");
		assert!(msg.contains("/some/dir"), "log should contain cwd: {msg}");
	}

	#[tokio::test]
	async fn verbose_runner_logs_run_interactive_with_program_and_cwd() {
		init_test_logger();
		let _ = take_logs();
		let inner = RecordingCommandRunner::new(0);
		let runner = VerboseCommandRunner::new(inner);
		let cwd = Path::new("/edit");
		let _ = runner.run_interactive("nano", &["CHANGELOG.md"], cwd).await;
		let logs = take_logs();
		let msg = logs
			.iter()
			.find(|(_, m)| m.contains("nano"))
			.map(|(_, m)| m.as_str())
			.expect("expected a log message about nano");
		assert!(
			msg.contains("CHANGELOG.md"),
			"log should contain args: {msg}"
		);
		assert!(msg.contains("/edit"), "log should contain cwd: {msg}");
	}

	#[tokio::test]
	async fn verbose_runner_logs_run_mut_with_program_and_args() {
		init_test_logger();
		let _ = take_logs();
		let inner = RecordingCommandRunner::new(0);
		let runner = VerboseCommandRunner::new(inner);
		let cwd = Path::new("/repo");
		let _ = runner
			.run_mut("git", &["push", "origin", "HEAD"], cwd)
			.await;
		let logs = take_logs();
		let msg = logs
			.iter()
			.find(|(_, m)| m.contains("push"))
			.map(|(_, m)| m.as_str())
			.expect("expected a log message about push");
		assert!(msg.contains("/repo"), "log should contain cwd: {msg}");
	}

	#[tokio::test]
	async fn verbose_runner_delegates_run_shell_interactive_to_inner() {
		init_test_logger();
		let inner = RecordingCommandRunner::new(0);
		let runner = VerboseCommandRunner::new(inner);
		let cwd = Path::new("/tmp");
		let _ = runner
			.run_shell_interactive("code --wait file.txt", cwd)
			.await;
		let invocations = runner.inner.invocations();
		assert_eq!(invocations.len(), 1);
		assert!(invocations[0].is_shell);
		assert!(invocations[0].is_interactive);
	}

	#[tokio::test]
	async fn verbose_runner_logs_run_shell_interactive_with_command_and_cwd() {
		init_test_logger();
		let _ = take_logs();
		let inner = RecordingCommandRunner::new(0);
		let runner = VerboseCommandRunner::new(inner);
		let cwd = Path::new("/edit");
		let _ = runner
			.run_shell_interactive("code --wait CHANGELOG.md", cwd)
			.await;
		let logs = take_logs();
		let msg = logs
			.iter()
			.find(|(_, m)| m.contains("code --wait"))
			.map(|(_, m)| m.as_str())
			.expect("expected a log message about code --wait");
		assert!(msg.contains("/edit"), "log should contain cwd: {msg}");
	}

	#[tokio::test]
	async fn verbose_runner_delegates_run_streaming_to_inner() {
		init_test_logger();
		let inner = RecordingCommandRunner::new(0);
		let runner = VerboseCommandRunner::new(inner);
		let cwd = Path::new("/tmp");
		let _ = runner
			.run_streaming("pnpm install --lockfile-only", cwd)
			.await;
		let invocations = runner.inner.invocations();
		assert_eq!(invocations.len(), 1);
		assert!(invocations[0].is_shell);
		assert!(invocations[0].is_streaming);
	}

	#[tokio::test]
	async fn verbose_runner_logs_run_streaming_with_command_and_cwd() {
		init_test_logger();
		let _ = take_logs();
		let inner = RecordingCommandRunner::new(0);
		let runner = VerboseCommandRunner::new(inner);
		let cwd = Path::new("/workspace");
		let _ = runner
			.run_streaming("pnpm install --lockfile-only", cwd)
			.await;
		let logs = take_logs();
		let msg = logs
			.iter()
			.find(|(_, m)| m.contains("pnpm install"))
			.map(|(_, m)| m.as_str())
			.expect("expected a log message about pnpm install");
		assert!(msg.contains("/workspace"), "log should contain cwd: {msg}");
	}
}

#[cfg(test)]
mod dry_run_tests {
	use std::path::Path;
	use std::sync::Arc;

	use super::*;
	use crate::command::test_support::RecordingCommandRunner;
	use crate::test_logging::{init_test_logger, take_logs};

	fn make_dry_run_runner() -> DryRunCommandRunner {
		DryRunCommandRunner::new(Arc::new(RecordingCommandRunner::new(0)))
	}

	#[tokio::test]
	async fn dry_run_runner_forwards_run_to_inner() {
		let inner = Arc::new(RecordingCommandRunner::new(0));
		let runner = DryRunCommandRunner::new(Arc::clone(&inner) as Arc<dyn CommandRunner>);
		let cwd = Path::new("/tmp");
		let _ = runner.run("git", &["status"], cwd).await;
		let invocations = inner.invocations();
		assert_eq!(invocations.len(), 1);
		assert_eq!(invocations[0].program, "git");
	}

	#[tokio::test]
	async fn dry_run_runner_suppresses_run_mut() {
		let inner = Arc::new(RecordingCommandRunner::new(0));
		let runner = DryRunCommandRunner::new(Arc::clone(&inner) as Arc<dyn CommandRunner>);
		let cwd = Path::new("/tmp");
		let result = runner.run_mut("git", &["commit", "-m", "msg"], cwd).await;
		assert!(result.is_ok());
		assert!(result.unwrap().status.success());
		// Inner runner must NOT have been called
		assert!(inner.invocations().is_empty());
	}

	#[tokio::test]
	async fn dry_run_runner_suppresses_run_interactive() {
		let inner = Arc::new(RecordingCommandRunner::new(0));
		let runner = DryRunCommandRunner::new(Arc::clone(&inner) as Arc<dyn CommandRunner>);
		let cwd = Path::new("/tmp");
		let result = runner.run_interactive("vim", &["file.txt"], cwd).await;
		assert!(result.is_ok());
		assert!(result.unwrap().success());
		assert!(inner.invocations().is_empty());
	}

	#[tokio::test]
	async fn dry_run_runner_logs_run_mut_at_info() {
		init_test_logger();
		let _ = take_logs();
		let runner = make_dry_run_runner();
		let cwd = Path::new("/repo");
		let _ = runner
			.run_mut("git", &["push", "origin", "HEAD"], cwd)
			.await;
		let logs = take_logs();
		let (level, msg) = logs
			.iter()
			.find(|(_, m)| m.contains("push"))
			.expect("expected a log message about push");
		assert_eq!(*level, log::Level::Info, "should log at info level");
		assert!(msg.contains("dry-run"), "log should mention dry-run: {msg}");
	}

	#[tokio::test]
	async fn dry_run_runner_suppresses_run_shell_interactive() {
		let inner = Arc::new(RecordingCommandRunner::new(0));
		let runner = DryRunCommandRunner::new(Arc::clone(&inner) as Arc<dyn CommandRunner>);
		let cwd = Path::new("/tmp");
		let result = runner
			.run_shell_interactive("code --wait file.txt", cwd)
			.await;
		assert!(result.is_ok());
		assert!(result.unwrap().success());
		assert!(inner.invocations().is_empty());
	}

	#[tokio::test]
	async fn dry_run_runner_logs_run_shell_interactive_at_info() {
		init_test_logger();
		let _ = take_logs();
		let runner = make_dry_run_runner();
		let cwd = Path::new("/edit");
		let _ = runner
			.run_shell_interactive("code --wait README.md", cwd)
			.await;
		let logs = take_logs();
		let (level, msg) = logs
			.iter()
			.find(|(_, m)| m.contains("code --wait"))
			.expect("expected a log message about code --wait");
		assert_eq!(*level, log::Level::Info, "should log at info level");
		assert!(msg.contains("dry-run"), "log should mention dry-run: {msg}");
	}

	#[tokio::test]
	async fn dry_run_runner_logs_run_interactive_at_info() {
		init_test_logger();
		let _ = take_logs();
		let runner = make_dry_run_runner();
		let cwd = Path::new("/edit");
		let _ = runner.run_interactive("vim", &["README.md"], cwd).await;
		let logs = take_logs();
		let (level, msg) = logs
			.iter()
			.find(|(_, m)| m.contains("vim"))
			.expect("expected a log message about vim");
		assert_eq!(*level, log::Level::Info, "should log at info level");
		assert!(msg.contains("dry-run"), "log should mention dry-run: {msg}");
		assert!(
			msg.contains("interactive"),
			"log should mention interactive: {msg}"
		);
	}

	#[tokio::test]
	async fn dry_run_runner_suppresses_run_streaming() {
		let inner = Arc::new(RecordingCommandRunner::new(0));
		let runner = DryRunCommandRunner::new(Arc::clone(&inner) as Arc<dyn CommandRunner>);
		let cwd = Path::new("/tmp");
		let result = runner
			.run_streaming("npm install --lockfile-only", cwd)
			.await;
		assert!(result.is_ok());
		assert!(result.unwrap().success());
		assert!(inner.invocations().is_empty());
	}

	#[tokio::test]
	async fn dry_run_runner_logs_run_streaming_at_info() {
		init_test_logger();
		let _ = take_logs();
		let runner = make_dry_run_runner();
		let cwd = Path::new("/workspace");
		let _ = runner
			.run_streaming("npm install --package-lock-only", cwd)
			.await;
		let logs = take_logs();
		let (level, msg) = logs
			.iter()
			.find(|(_, m)| m.contains("npm install"))
			.expect("expected a log message about npm install");
		assert_eq!(*level, log::Level::Info, "should log at info level");
		assert!(msg.contains("dry-run"), "log should mention dry-run: {msg}");
		assert!(
			msg.contains("streaming"),
			"log should mention streaming: {msg}"
		);
	}
}

#[cfg(any(test, feature = "test-support"))]
pub mod test_support;

#[cfg(test)]
mod real_command_tests {
	use super::*;

	#[tokio::test]
	async fn real_runner_run_interactive_returns_success() {
		// Exercises RealCommandRunner::run_interactive so the .status() call is
		// covered — mutations that replace it with something else would break this.
		// git is a prerequisite on all platforms and is a real executable.
		let runner = RealCommandRunner;
		let cwd = std::env::temp_dir();
		let result = runner.run_interactive("git", &["--version"], &cwd).await;
		assert!(result.is_ok(), "run_interactive should succeed: {result:?}");
		assert!(
			result.unwrap().success(),
			"'git --version' must exit with status 0"
		);
	}

	#[tokio::test]
	async fn real_runner_run_shell_interactive_returns_success() {
		// Exercises RealCommandRunner::run_shell_interactive so the .status() call is
		// covered — mutations that replace it with something else would break this.
		// "echo ok" works on both /bin/sh and cmd.exe.
		let runner = RealCommandRunner;
		let cwd = std::env::temp_dir();
		let result = runner.run_shell_interactive("echo ok", &cwd).await;
		assert!(
			result.is_ok(),
			"run_shell_interactive should succeed: {result:?}"
		);
		assert!(
			result.unwrap().success(),
			"'echo ok' must exit with status 0"
		);
	}

	#[tokio::test]
	async fn real_runner_run_streaming_returns_success() {
		// Exercises RealCommandRunner::run_streaming so the .status() call is
		// covered — mutations that replace it with something else would break this.
		// "echo ok" works on both /bin/sh and cmd.exe.
		let runner = RealCommandRunner;
		let cwd = std::env::temp_dir();
		let result = runner.run_streaming("echo ok", &cwd).await;
		assert!(result.is_ok(), "run_streaming should succeed: {result:?}");
		assert!(
			result.unwrap().success(),
			"'echo ok' must exit with status 0"
		);
	}

	#[tokio::test]
	async fn real_runner_run_streaming_logs_running_at_info() {
		// The centralised pre-log is load-bearing: callers delete their own
		// pre-logs on the strength of this guarantee (ADR-046 line 38).
		// A mutant that removes log::info! would silently break that contract.
		crate::test_logging::init_test_logger();
		let _ = crate::test_logging::take_logs();
		let runner = RealCommandRunner;
		let cwd = std::env::temp_dir();
		let _ = runner.run_streaming("echo ok", &cwd).await;
		let logs = crate::test_logging::take_logs();
		let (level, msg) = logs
			.iter()
			.find(|(_, m)| m.contains("Running:"))
			.expect("expected a 'Running:' log message before streaming command");
		assert_eq!(*level, log::Level::Info, "pre-log must be at info level");
		assert!(msg.contains("echo ok"), "pre-log must include the command");
	}
}