rmux 0.10.0

A local terminal multiplexer with a tmux-style CLI, daemon runtime, Rust SDK, and ratatui integration.
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
#![cfg(unix)]

mod common;

use std::error::Error;
use std::fs;
use std::io::Write;
use std::os::unix::fs::{MetadataExt, PermissionsExt};
use std::process::Stdio;
#[cfg(target_os = "macos")]
use std::process::{Child, Command, ExitStatus};
#[cfg(target_os = "macos")]
use std::time::{Duration, Instant};

use common::{assert_success, stderr, stdout, terminate_child, CliHarness};

#[test]
fn load_buffer_reads_server_side_file() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("load-buffer")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let input_path = harness.tmpdir().join("input.txt");
    std::fs::write(&input_path, "loaded from file")?;

    assert_success(&harness.run(&[
        "load-buffer",
        "-b",
        "loaded",
        input_path.to_str().expect("utf-8 test path"),
    ])?);

    let show = harness.run(&["show-buffer", "-b", "loaded"])?;
    assert_eq!(show.status.code(), Some(0));
    assert_eq!(stdout(&show), "loaded from file");
    assert!(stderr(&show).is_empty());

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn load_buffer_accepts_mixed_flag_order() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("load-buffer-flag-order")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let input_path = harness.tmpdir().join("input.txt");
    std::fs::write(&input_path, "loaded with mixed flags")?;

    assert_success(&harness.run(&[
        "load-buffer",
        "-w",
        "-b",
        "loaded",
        input_path.to_str().expect("utf-8 test path"),
    ])?);

    let show = harness.run(&["show-buffer", "-b", "loaded"])?;
    assert_eq!(show.status.code(), Some(0));
    assert_eq!(stdout(&show), "loaded with mixed flags");
    assert!(stderr(&show).is_empty());

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn buffer_target_client_missing_is_a_successful_direct_cli_noop() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("buffer-target-client-missing")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let input_path = harness.tmpdir().join("input.txt");
    fs::write(&input_path, "loaded despite missing client")?;

    assert_success(&harness.run(&[
        "set-buffer",
        "-w",
        "-t",
        "missing-client",
        "-b",
        "set-targeted",
        "set despite missing client",
    ])?);
    assert_success(&harness.run(&[
        "load-buffer",
        "-w",
        "-t",
        "missing-client",
        "-b",
        "load-targeted",
        input_path.to_str().expect("utf-8 test path"),
    ])?);
    assert_success(&run_with_stdin(
        &harness,
        &[
            "load-buffer",
            "-w",
            "-t",
            "missing-client",
            "-b",
            "stdin-targeted",
            "-",
        ],
        b"stdin despite missing client",
    )?);

    for (name, expected) in [
        ("set-targeted", "set despite missing client"),
        ("load-targeted", "loaded despite missing client"),
        ("stdin-targeted", "stdin despite missing client"),
    ] {
        let shown = harness.run(&["show-buffer", "-b", name])?;
        assert_eq!(shown.status.code(), Some(0));
        assert_eq!(stdout(&shown), expected);
        assert!(stderr(&shown).is_empty());
    }

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn load_buffer_reads_stdin_when_path_is_dash() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("load-buffer-dash")?;
    let mut daemon = harness.start_hidden_daemon()?;

    let load = run_with_stdin(
        &harness,
        &["load-buffer", "-b", "dash", "-"],
        b"stdin bytes",
    )?;
    assert_success(&load);

    let show = harness.run(&["show-buffer", "-b", "dash"])?;
    assert_eq!(show.status.code(), Some(0));
    assert_eq!(stdout(&show), "stdin bytes");
    assert!(stderr(&show).is_empty());

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn load_buffer_empty_stdin_is_successful_noop() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("load-buffer-dash-empty")?;
    let mut daemon = harness.start_hidden_daemon()?;

    let load = run_with_stdin(&harness, &["load-buffer", "-b", "empty", "-"], b"")?;
    assert_success(&load);

    let show = harness.run(&["show-buffer", "-b", "empty"])?;
    assert_eq!(show.status.code(), Some(1));
    assert!(stderr(&show).contains("no buffer empty"));

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn load_buffer_empty_file_is_successful_noop() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("load-buffer-empty-file")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let input_path = harness.tmpdir().join("empty.txt");
    fs::write(&input_path, "")?;

    assert_success(&harness.run(&[
        "load-buffer",
        "-b",
        "empty",
        input_path.to_str().expect("utf-8 test path"),
    ])?);

    let show = harness.run(&["show-buffer", "-b", "empty"])?;
    assert_eq!(show.status.code(), Some(1));
    assert!(stderr(&show).contains("no buffer empty"));

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[cfg(target_os = "macos")]
#[test]
fn official_daemon_observes_instantaneous_empty_fifo_writers() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("load-buffer-empty-fifo")?;
    let mut daemon = harness.start_hidden_daemon()?;

    for attempt in 0..50 {
        let fifo_path = harness.tmpdir().join(format!("empty-{attempt}.fifo"));
        let created = Command::new("mkfifo").arg(&fifo_path).output()?;
        assert!(
            created.status.success(),
            "mkfifo failed: {}",
            String::from_utf8_lossy(&created.stderr)
        );

        let mut load_command = harness.base_command();
        load_command
            .args([
                "load-buffer",
                "-b",
                "empty-fifo",
                fifo_path.to_str().expect("utf-8 FIFO path"),
            ])
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        let mut load = load_command.spawn()?;
        let mut writer = Command::new("/bin/sh")
            .args(["-c", "exec 3>\"$1\"; exec 3>&-", "rmux-empty-fifo-writer"])
            .arg(&fifo_path)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()?;

        let deadline = Instant::now() + Duration::from_secs(5);
        let (load_status, writer_status) = loop {
            let load_status = load.try_wait()?;
            let writer_status = writer.try_wait()?;
            if let (Some(load_status), Some(writer_status)) = (load_status, writer_status) {
                break (load_status, writer_status);
            }
            if Instant::now() >= deadline {
                let _ = load.kill();
                let _ = writer.kill();
                let load_output = load.wait_with_output()?;
                let _ = writer.wait();
                return Err(format!(
                    "empty FIFO attempt {attempt} timed out: status={:?}, stderr={}",
                    load_output.status,
                    String::from_utf8_lossy(&load_output.stderr)
                )
                .into());
            }
            std::thread::sleep(Duration::from_millis(5));
        };

        let load_output = load.wait_with_output()?;
        assert_eq!(load_output.status, load_status);
        assert_success(&load_output);
        assert!(writer_status.success(), "empty FIFO writer failed");
        fs::remove_file(fifo_path)?;
    }

    let show = harness.run(&["show-buffer", "-b", "empty-fifo"])?;
    assert_eq!(show.status.code(), Some(1));
    assert!(stderr(&show).contains("no buffer empty-fifo"));

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[cfg(target_os = "macos")]
#[test]
fn official_daemon_drains_fifo_payload_larger_than_helper_pipe() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("load-buffer-large-fifo")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let fifo_path = harness.tmpdir().join("large.fifo");
    let first_payload_path = harness.tmpdir().join("large-first.payload");
    let second_payload_path = harness.tmpdir().join("large-second.payload");
    let first = vec![b'a'; 128 * 1024];
    let second = vec![b'b'; 128 * 1024];
    let expected = [first.as_slice(), second.as_slice()].concat();
    fs::write(&first_payload_path, first)?;
    fs::write(&second_payload_path, second)?;
    assert_success(&Command::new("mkfifo").arg(&fifo_path).output()?);

    let mut load = harness
        .base_command()
        .args([
            "load-buffer",
            "-b",
            "large-fifo",
            fifo_path.to_str().expect("utf-8 FIFO path"),
        ])
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()?;
    let mut writer = Command::new("/bin/sh")
        .args([
            "-c",
            "exec 3>\"$1\"; cat \"$2\" >&3; sleep 0.15; cat \"$3\" >&3; exec 3>&-",
            "rmux-large-fifo-writer",
        ])
        .arg(&fifo_path)
        .arg(&first_payload_path)
        .arg(&second_payload_path)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::piped())
        .spawn()?;

    let (load_status, writer_status) =
        wait_for_child_pair(&mut load, &mut writer, Duration::from_secs(10))?;
    let load_output = load.wait_with_output()?;
    let writer_output = writer.wait_with_output()?;
    assert_eq!(load_output.status, load_status);
    assert_eq!(writer_output.status, writer_status);
    assert_success(&load_output);
    assert_success(&writer_output);

    let shown = harness.run(&["show-buffer", "-b", "large-fifo"])?;
    assert_eq!(
        shown.status.code(),
        Some(0),
        "show-buffer failed: {}",
        stderr(&shown)
    );
    assert!(shown.stderr.is_empty(), "show-buffer wrote stderr");
    assert_eq!(shown.stdout, expected);

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[cfg(target_os = "macos")]
#[test]
fn private_fifo_helper_rejects_replacement_after_classification() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("fifo-helper-replacement")?;
    let fifo_path = harness.tmpdir().join("classified.fifo");
    let original_path = harness.tmpdir().join("classified.original.fifo");
    assert_success(&Command::new("mkfifo").arg(&fifo_path).output()?);
    let metadata = fs::metadata(&fifo_path)?;
    fs::rename(&fifo_path, &original_path)?;
    assert_success(&Command::new("mkfifo").arg(&fifo_path).output()?);

    let mut helper = harness
        .base_command()
        .arg("--__internal-fifo-reader")
        .arg(&fifo_path)
        .arg(metadata.dev().to_string())
        .arg(metadata.ino().to_string())
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()?;
    let mut writer = Command::new("/bin/sh")
        .args([
            "-c",
            "exec 3>\"$1\"; exec 3>&-",
            "rmux-replacement-fifo-writer",
        ])
        .arg(&fifo_path)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::piped())
        .spawn()?;

    let (helper_status, writer_status) =
        wait_for_child_pair(&mut helper, &mut writer, Duration::from_secs(10))?;
    let helper_output = helper.wait_with_output()?;
    let writer_output = writer.wait_with_output()?;
    assert_eq!(helper_output.status, helper_status);
    assert_eq!(writer_output.status, writer_status);
    assert_eq!(helper_output.status.code(), Some(65));
    assert!(helper_output.stdout.is_empty());
    assert!(stderr(&helper_output).contains("changed during blocking open"));
    assert_success(&writer_output);
    Ok(())
}

#[cfg(target_os = "macos")]
fn wait_for_child_pair(
    left: &mut Child,
    right: &mut Child,
    timeout: Duration,
) -> Result<(ExitStatus, ExitStatus), Box<dyn Error>> {
    let deadline = Instant::now() + timeout;
    loop {
        if let (Some(left_status), Some(right_status)) = (left.try_wait()?, right.try_wait()?) {
            return Ok((left_status, right_status));
        }
        if Instant::now() >= deadline {
            let _ = left.kill();
            let _ = right.kill();
            let _ = left.wait();
            let _ = right.wait();
            return Err("timed out waiting for FIFO helper process pair".into());
        }
        std::thread::sleep(Duration::from_millis(5));
    }
}

#[test]
fn save_buffer_writes_server_side_file() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("save-buffer")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let output_path = harness.tmpdir().join("output.txt");

    assert_success(&harness.run(&["set-buffer", "-b", "saved", "save this"])?);
    assert_success(&harness.run(&[
        "save-buffer",
        "-b",
        "saved",
        output_path.to_str().expect("utf-8 test path"),
    ])?);

    assert_eq!(std::fs::read_to_string(&output_path)?, "save this");

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn save_buffer_accepts_mixed_flag_order_and_appends() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("save-buffer-flag-order")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let output_path = harness.tmpdir().join("output.txt");

    std::fs::write(&output_path, "prefix:")?;
    assert_success(&harness.run(&["set-buffer", "-b", "saved", "tail"])?);
    assert_success(&harness.run(&[
        "save-buffer",
        "-a",
        "-b",
        "saved",
        output_path.to_str().expect("utf-8 test path"),
    ])?);

    assert_eq!(std::fs::read_to_string(&output_path)?, "prefix:tail");

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn save_buffer_writes_stdout_when_path_is_dash() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("save-buffer-dash")?;
    let mut daemon = harness.start_hidden_daemon()?;

    assert_success(&harness.run(&["set-buffer", "-b", "saved", "stdout bytes"])?);
    let save = harness.run(&["save-buffer", "-b", "saved", "-"])?;
    assert_eq!(save.status.code(), Some(0));
    assert_eq!(stdout(&save), "stdout bytes");
    assert!(stderr(&save).is_empty());

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn save_buffer_append_flag_still_writes_stdout_when_path_is_dash() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("save-buffer-dash-append")?;
    let mut daemon = harness.start_hidden_daemon()?;

    assert_success(&harness.run(&["set-buffer", "-b", "saved", "append stdout"])?);
    let save = harness.run(&["save-buffer", "-a", "-b", "saved", "-"])?;
    assert_eq!(save.status.code(), Some(0));
    assert_eq!(stdout(&save), "append stdout");
    assert!(stderr(&save).is_empty());

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn load_buffer_resolves_relative_paths_against_client_cwd() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("load-buffer-relative")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let caller_dir = harness.tmpdir().join("caller");
    let nested_dir = caller_dir.join("nested");
    fs::create_dir_all(&nested_dir)?;
    fs::write(nested_dir.join("input.txt"), "loaded from relative path")?;

    assert_success(&harness.run_with(
        &["load-buffer", "-b", "loaded", "nested/input.txt"],
        |command| {
            command.current_dir(&caller_dir);
        },
    )?);

    let show = harness.run(&["show-buffer", "-b", "loaded"])?;
    assert_eq!(show.status.code(), Some(0));
    assert_eq!(stdout(&show), "loaded from relative path");
    assert!(stderr(&show).is_empty());

    terminate_child(daemon.child_mut())?;
    Ok(())
}

fn run_with_stdin(
    harness: &CliHarness,
    args: &[&str],
    stdin: &[u8],
) -> Result<std::process::Output, Box<dyn Error>> {
    let mut command = harness.base_command();
    command.args(args);
    command.stdin(Stdio::piped());
    command.stdout(Stdio::piped());
    command.stderr(Stdio::piped());

    let mut child = command.spawn()?;
    child
        .stdin
        .take()
        .expect("stdin is piped")
        .write_all(stdin)?;
    Ok(child.wait_with_output()?)
}

#[test]
fn save_buffer_resolves_relative_paths_against_client_cwd() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("save-buffer-relative")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let caller_dir = harness.tmpdir().join("caller");
    let nested_dir = caller_dir.join("nested");
    fs::create_dir_all(&nested_dir)?;

    assert_success(&harness.run(&["set-buffer", "-b", "saved", "save this"])?);
    assert_success(&harness.run_with(
        &["save-buffer", "-b", "saved", "nested/output.txt"],
        |command| {
            command.current_dir(&caller_dir);
        },
    )?);

    assert_eq!(
        fs::read_to_string(nested_dir.join("output.txt"))?,
        "save this"
    );

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn save_buffer_replaces_existing_destination_file() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("save-buffer-replace")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let output_path = harness.tmpdir().join("output.txt");

    std::fs::write(&output_path, "stale data")?;
    assert_success(&harness.run(&["set-buffer", "-b", "saved", "fresh data"])?);
    assert_success(&harness.run(&[
        "save-buffer",
        "-b",
        "saved",
        output_path.to_str().expect("utf-8 test path"),
    ])?);

    assert_eq!(std::fs::read_to_string(&output_path)?, "fresh data");

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn save_buffer_preserves_existing_file_identity_metadata_and_links() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("save-buffer-existing-identity")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let output_path = harness.tmpdir().join("output.txt");
    let hard_link_path = harness.tmpdir().join("output-hard-link.txt");
    let symlink_target = harness.tmpdir().join("symlink-target.txt");
    let symlink_path = harness.tmpdir().join("output-symlink.txt");

    fs::write(&output_path, "stale data")?;
    fs::set_permissions(&output_path, fs::Permissions::from_mode(0o600))?;
    fs::hard_link(&output_path, &hard_link_path)?;
    let original = fs::metadata(&output_path)?;

    assert_success(&harness.run(&["set-buffer", "-b", "saved", "fresh data"])?);
    assert_success(&harness.run(&[
        "save-buffer",
        "-b",
        "saved",
        output_path.to_str().expect("utf-8 test path"),
    ])?);

    let replaced = fs::metadata(&output_path)?;
    assert_eq!(replaced.ino(), original.ino());
    assert_eq!(replaced.mode() & 0o777, 0o600);
    assert_eq!(fs::read_to_string(&hard_link_path)?, "fresh data");

    fs::write(&symlink_target, "target stale data")?;
    fs::set_permissions(&symlink_target, fs::Permissions::from_mode(0o600))?;
    std::os::unix::fs::symlink(&symlink_target, &symlink_path)?;
    let target_inode = fs::metadata(&symlink_target)?.ino();
    assert_success(&harness.run(&[
        "save-buffer",
        "-b",
        "saved",
        symlink_path.to_str().expect("utf-8 test path"),
    ])?);

    assert!(fs::symlink_metadata(&symlink_path)?
        .file_type()
        .is_symlink());
    let updated_target = fs::metadata(&symlink_target)?;
    assert_eq!(updated_target.ino(), target_inode);
    assert_eq!(updated_target.mode() & 0o777, 0o600);
    assert_eq!(fs::read_to_string(&symlink_target)?, "fresh data");

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn load_buffer_failure_does_not_replace_existing_buffer() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("load-buffer-failure")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let missing_path = harness.tmpdir().join("missing.txt");

    assert_success(&harness.run(&["set-buffer", "-b", "stable", "original"])?);

    let load = harness.run(&[
        "load-buffer",
        "-b",
        "stable",
        missing_path.to_str().expect("utf-8 test path"),
    ])?;
    assert_eq!(load.status.code(), Some(1));
    assert!(stderr(&load).contains(missing_path.to_str().expect("utf-8 test path")));

    let show = harness.run(&["show-buffer", "-b", "stable"])?;
    assert_eq!(show.status.code(), Some(0));
    assert_eq!(stdout(&show), "original");

    terminate_child(daemon.child_mut())?;
    Ok(())
}

#[test]
fn save_buffer_failure_does_not_delete_existing_buffer() -> Result<(), Box<dyn Error>> {
    let harness = CliHarness::new("save-buffer-failure")?;
    let mut daemon = harness.start_hidden_daemon()?;
    let output_path = harness.tmpdir().join("missing-parent").join("output.txt");

    assert_success(&harness.run(&["set-buffer", "-b", "stable", "original"])?);

    let save = harness.run(&[
        "save-buffer",
        "-b",
        "stable",
        output_path.to_str().expect("utf-8 test path"),
    ])?;
    assert_eq!(save.status.code(), Some(1));
    assert!(stderr(&save).contains(output_path.to_str().expect("utf-8 test path")));

    let show = harness.run(&["show-buffer", "-b", "stable"])?;
    assert_eq!(show.status.code(), Some(0));
    assert_eq!(stdout(&show), "original");

    terminate_child(daemon.child_mut())?;
    Ok(())
}