proc-cli 2.0.0

Semantic CLI tool for process management. Target by port, PID, name or path.
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
//! `on` and `ports`, driven through the real binary.
//!
//! Every port and process these tests look for is one the test started: a copy
//! of the listener example (`examples/listener.rs`) under a name and path of its
//! own, in a fresh temp directory, so `--by <name>` and `--in <dir>` match only
//! it. A port nothing holds is one the OS just handed out and took back. Each
//! process is killed and reaped on drop.

use assert_cmd::Command;
use proc_cli::core::filters::strip_verbatim_prefix;
use serde_json::Value;
use std::io::{BufRead, BufReader};
use std::net::TcpListener;
use std::path::PathBuf;
use std::process::{Child, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};

fn proc_cmd() -> Command {
    let mut cmd = Command::cargo_bin("proc").unwrap();
    cmd.env("NO_COLOR", "1");
    cmd
}

/// Run proc and parse stdout as exactly one JSON document, with the exit code.
fn run_json(args: &[&str]) -> (Value, i32) {
    let output = proc_cmd().args(args).output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("stdout is not a single JSON document ({e}):\n{stdout}"));
    (json, output.status.code().unwrap())
}

/// Run proc and return its stdout, stderr and exit code.
fn run_text(args: &[&str]) -> (String, String, i32) {
    let output = proc_cmd().args(args).output().unwrap();
    (
        String::from_utf8(output.stdout).unwrap(),
        String::from_utf8(output.stderr).unwrap(),
        output.status.code().unwrap(),
    )
}

/// `target/debug/examples/listener`, found relative to this test binary
/// (`target/debug/deps/cmd_ports-<hash>`)
fn listener_path() -> PathBuf {
    let mut path = std::env::current_exe().unwrap();
    path.pop();
    path.pop();
    path.push("examples");
    path.push(format!("listener{}", std::env::consts::EXE_SUFFIX));
    assert!(path.exists(), "build the example first: {}", path.display());
    path
}

/// A fresh temp directory, as processes report paths: symlinks resolved (/var
/// is /private/var on macOS, and `--in` compares paths literally) and no
/// Windows `\\?\` prefix
fn fresh_dir() -> (tempfile::TempDir, PathBuf) {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path().canonicalize().unwrap();
    let dir = PathBuf::from(strip_verbatim_prefix(&dir.to_string_lossy()));
    (tmp, dir)
}

/// A process name no process has, for this call alone: the other tests run
/// proc at the same time, so a name two tests share would match the other
/// test's process.
fn nobody() -> String {
    let n = COUNTER.fetch_add(1, Ordering::SeqCst);
    // Ends in `x`, so no name is the start of another (`-1x` in `-10x`)
    format!("nobody-{}-{n}x", std::process::id())
}

/// A port nothing listens on, from below every OS's ephemeral range (Linux
/// starts at 32768, macOS and Windows at 49152), so a listener another test
/// binds to port 0 can never be handed it. A port the OS just handed out and
/// took back could be: on CI a parallel test's listener got one between the
/// lookup and proc's run
fn unused_port() -> u16 {
    static NEXT: AtomicUsize = AtomicUsize::new(0);
    let start = 20_000 + std::process::id() as usize % 10_000;
    loop {
        let port = start + NEXT.fetch_add(1, Ordering::SeqCst);
        assert!(port < 32_768, "no unused port below the ephemeral range");
        if TcpListener::bind(("0.0.0.0", port as u16)).is_ok() {
            return port as u16;
        }
    }
}

static COUNTER: AtomicUsize = AtomicUsize::new(0);

/// Run the copy, with its cwd in `dir`. On Linux a file another thread has
/// just written can be "busy" (ETXTBSY) for an instant: a fork in a parallel
/// test still holds the write handle until it execs. That clears in
/// milliseconds, so try again.
fn spawn_copy(copy: &std::path::Path, args: &[&str], dir: &std::path::Path) -> Child {
    let mut attempts = 0;
    loop {
        let spawned = std::process::Command::new(copy)
            .args(args)
            .current_dir(dir)
            .stdout(Stdio::piped())
            .stdin(Stdio::null())
            .spawn();
        match spawned {
            Ok(child) => return child,
            Err(e) if attempts < 100 && is_text_file_busy(&e) => {
                attempts += 1;
                std::thread::sleep(std::time::Duration::from_millis(20));
            }
            Err(e) => panic!("spawn the listener copy: {e}"),
        }
    }
}

#[cfg(unix)]
fn is_text_file_busy(e: &std::io::Error) -> bool {
    e.raw_os_error() == Some(libc::ETXTBSY)
}

#[cfg(not(unix))]
fn is_text_file_busy(_: &std::io::Error) -> bool {
    false
}

/// A listener the test owns: a copy of the listener example, run with its cwd
/// in a fresh temp directory. Killed and reaped on drop.
struct Owned {
    child: Child,
    port: u16,
    /// A name no other process has, short enough for any OS to report whole
    name: String,
    /// Keeps the directory alive; `dir` is its path as processes report it
    _tmp: tempfile::TempDir,
    dir: PathBuf,
}

impl Owned {
    fn listener() -> Self {
        let n = COUNTER.fetch_add(1, Ordering::SeqCst);
        Self::named(&format!("pcp{}n{n}x", std::process::id()))
    }

    /// A listener whose process name is exactly `name` (15 characters at most:
    /// Linux cuts a process name there)
    fn named(name: &str) -> Self {
        Self::named_with_args(name, &[])
    }

    /// [`Owned::named`], started with extra arguments (the listener ignores them,
    /// so they only put words in its command line)
    fn named_with_args(name: &str, args: &[&str]) -> Self {
        assert!(name.len() <= 15, "{name} is too long for Linux");
        let (tmp, dir) = fresh_dir();
        let copy = dir.join(format!("{name}{}", std::env::consts::EXE_SUFFIX));
        std::fs::copy(listener_path(), &copy).expect("copy the listener example");
        let mut child = spawn_copy(&copy, args, &dir);
        let mut line = String::new();
        let mut stdout = BufReader::new(child.stdout.take().unwrap());
        let read = stdout.read_line(&mut line);
        let port = read
            .ok()
            .and_then(|_| line.trim().strip_prefix("PORT "))
            .and_then(|p| p.parse().ok());
        match port {
            Some(port) => Owned {
                child,
                port,
                name: name.to_string(),
                _tmp: tmp,
                dir,
            },
            None => {
                let _ = child.kill();
                let _ = child.wait();
                panic!("the listener printed {line:?} instead of `PORT <n>`");
            }
        }
    }

    fn pid(&self) -> u32 {
        self.child.id()
    }

    fn dir(&self) -> String {
        self.dir.to_string_lossy().into_owned()
    }

    /// `:<port>`, as a target
    fn target(&self) -> String {
        format!(":{}", self.port)
    }

    /// The PID as a target
    fn pid_arg(&self) -> String {
        self.pid().to_string()
    }
}

impl Drop for Owned {
    fn drop(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
    }
}

/// How many `results` of an `on` JSON answer are for this PID
fn results_for(json: &Value, pid: u32) -> usize {
    json["results"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|r| r["process"]["pid"].as_u64() == Some(u64::from(pid)))
        .count()
}

/// A directory no test process runs in
fn other_dir() -> (tempfile::TempDir, String) {
    let (tmp, dir) = fresh_dir();
    let arg = dir.to_string_lossy().into_owned();
    (tmp, arg)
}

// ---- on: port to process ----

#[test]
fn on_port_names_the_process_holding_it() {
    let p = Owned::listener();
    let (out, _, code) = run_text(&["on", &p.target()]);
    assert_eq!(code, 0, "{out}");
    assert!(
        out.contains(&format!("Port {} is used by:", p.port)),
        "{out}"
    );
    assert!(out.contains(&format!("(PID {})", p.pid())), "{out}");
    assert!(out.contains("Process:"), "{out}");
    assert!(out.contains("Directory:"), "{out}");
    assert!(out.contains("Path:"), "{out}");
    assert!(out.contains("Listening: TCP on 127.0.0.1"), "{out}");
    assert!(out.contains("Resources:"), "{out}");
    assert!(out.contains("Uptime:"), "{out}");
    assert!(
        !out.contains("Command:"),
        "the command line is for --verbose: {out}"
    );
}

#[test]
fn on_port_verbose_adds_the_command_line() {
    let p = Owned::listener();
    let (out, _, code) = run_text(&["on", &p.target(), "--verbose"]);
    assert_eq!(code, 0, "{out}");
    assert!(out.contains("Command:"), "{out}");
    assert!(out.contains(&p.name), "{out}");
}

#[test]
fn on_port_json_describes_the_listener_and_its_process() {
    let p = Owned::listener();
    let (json, code) = run_json(&["on", &p.target(), "--json"]);
    assert_eq!(code, 0, "{json}");
    assert_eq!(json["action"], "on");
    assert_eq!(json["query_type"], "port_to_process");
    assert_eq!(json["success"], true);
    assert_eq!(json["port"], p.port);
    assert_eq!(json["protocol"], "tcp");
    assert_eq!(json["address"], "127.0.0.1");
    assert_eq!(json["process"]["pid"], p.pid());
    assert!(json.get("ports").is_none(), "{json}");
}

#[test]
fn on_port_nobody_holds_exits_not_found() {
    let port = format!(":{}", unused_port());
    let (out, err, code) = run_text(&["on", &port]);
    assert_eq!(code, 2, "{out}{err}");
    assert!(err.contains("No process listening on port"), "{err}");

    let (json, code) = run_json(&["on", &port, "--json"]);
    assert_eq!(code, 2, "{json}");
    assert_eq!(json["success"], false);
}

#[test]
fn on_port_keeps_the_process_only_when_it_passes_in_and_by() {
    let p = Owned::listener();
    let (_other, other) = other_dir();

    let (out, _, code) = run_text(&["on", &p.target(), "--in", &p.dir()]);
    assert_eq!(code, 0, "{out}");
    let (out, _, code) = run_text(&["on", &p.target(), "--by", &p.name]);
    assert_eq!(code, 0, "{out}");

    let (out, err, code) = run_text(&["on", &p.target(), "--in", &other]);
    assert_eq!(code, 2, "{out}{err}");
    assert_eq!(
        err.lines()
            .next()
            .unwrap_or_default()
            .trim_start_matches("Error: "),
        format!(
            "No process found matching '{}' (process not in specified directory)",
            p.target()
        ),
        "the target sits in the quotes, the reason follows them: {err}"
    );
    let (out, err, code) = run_text(&["on", &p.target(), "--by", "no-such-process-name"]);
    assert_eq!(code, 2, "{out}{err}");
}

/// `list` and `by` take process names, never targets: a colon is part of the
/// name, so `:70000` is not an impossible port there but a name nothing has, and
/// `:<port>` of a live listener does not find the process holding that port.
#[test]
fn list_and_by_read_a_colon_word_as_a_name_not_a_port() {
    let p = Owned::listener();
    for word in [":70000".to_string(), p.target()] {
        let (json, code) = run_json(&["list", &word, "--json"]);
        assert_eq!(code, 0, "list {word}: {json}");
        assert_eq!(json["count"], 0, "list {word}: {json}");

        let (out, err, code) = run_text(&["by", &word]);
        assert_eq!(code, 2, "by {word}: {out}{err}");
        assert!(
            err.contains(&format!("No process found matching '{word}'")),
            "by {word}: {err}"
        );
    }
}

// ---- on: process to ports ----

#[test]
fn on_pid_lists_the_ports_it_listens_on() {
    let p = Owned::listener();
    let (out, _, code) = run_text(&["on", &p.pid_arg()]);
    assert_eq!(code, 0, "{out}");
    assert!(out.contains(&p.name), "{out}");
    assert!(
        out.contains(&format!("(PID {}) is listening on:", p.pid())),
        "{out}"
    );
    assert!(
        out.contains(&format!(":{} (TCP on 127.0.0.1)", p.port)),
        "{out}"
    );
    assert!(out.contains("Directory:"), "{out}");
    assert!(out.contains("Uptime:"), "{out}");
    assert!(!out.contains("Path:"), "the path is for --verbose: {out}");
    assert!(!out.contains("Command:"), "{out}");
}

#[test]
fn on_pid_verbose_adds_path_and_command() {
    let p = Owned::listener();
    let (out, _, code) = run_text(&["on", &p.pid_arg(), "-v"]);
    assert_eq!(code, 0, "{out}");
    assert!(out.contains("Path:"), "{out}");
    assert!(out.contains("Command:"), "{out}");
}

#[test]
fn on_pid_json_lists_its_ports() {
    let p = Owned::listener();
    let (json, code) = run_json(&["on", &p.pid_arg(), "--json"]);
    assert_eq!(code, 0, "{json}");
    assert_eq!(json["query_type"], "process_to_ports");
    assert_eq!(json["process"]["pid"], p.pid());
    let ports = json["ports"].as_array().unwrap();
    assert!(ports.iter().any(|x| x["port"] == p.port), "{json}");
    assert!(json.get("port").is_none(), "{json}");
}

#[test]
fn on_a_pid_with_no_ports_says_so() {
    // This test binary listens on nothing
    let me = std::process::id().to_string();
    let (out, _, code) = run_text(&["on", &me]);
    assert_eq!(code, 0, "{out}");
    assert!(out.contains("No listening ports"), "{out}");

    let (json, code) = run_json(&["on", &me, "--json"]);
    assert_eq!(code, 0, "{json}");
    assert_eq!(json["ports"].as_array().unwrap().len(), 0, "{json}");
}

#[test]
fn on_pid_outside_in_or_by_exits_not_found() {
    let p = Owned::listener();
    let (_other, other) = other_dir();
    let (out, err, code) = run_text(&["on", &p.pid_arg(), "--in", &other]);
    assert_eq!(code, 2, "{out}{err}");
    assert_eq!(
        err.lines()
            .next()
            .unwrap_or_default()
            .trim_start_matches("Error: "),
        format!(
            "No process found matching '{}' (not in specified directory)",
            p.pid_arg()
        ),
        "the target sits in the quotes, the reason follows them: {err}"
    );

    let (out, err, code) = run_text(&["on", &p.pid_arg(), "--by", "no-such-process-name"]);
    assert_eq!(code, 2, "{out}{err}");

    let (out, _, code) = run_text(&["on", &p.pid_arg(), "--in", &p.dir(), "--by", &p.name]);
    assert_eq!(code, 0, "{out}");
}

#[test]
fn on_a_pid_nothing_runs_exits_not_found() {
    // A PID past anything a system hands out
    let (out, err, code) = run_text(&["on", "4294967"]);
    assert_eq!(code, 2, "{out}{err}");
}

// ---- on: process name ----

#[test]
fn on_name_lists_each_matching_process() {
    let p = Owned::listener();
    let (out, _, code) = run_text(&["on", &p.name]);
    assert_eq!(code, 0, "{out}");
    assert!(
        out.contains(&format!("(PID {}) is listening on:", p.pid())),
        "{out}"
    );
    assert!(out.contains(&format!(":{}", p.port)), "{out}");

    let (json, code) = run_json(&["on", &p.name, "--json"]);
    assert_eq!(code, 0, "{json}");
    assert_eq!(json["count"], 1, "{json}");
    assert_eq!(json["results"][0]["process"]["pid"], p.pid());
    assert_eq!(json["results"][0]["ports"][0]["port"], p.port);
}

#[test]
fn on_name_applies_in_and_by() {
    let p = Owned::listener();
    let (_other, other) = other_dir();

    let (json, code) = run_json(&["on", &p.name, "--in", &p.dir(), "--json"]);
    assert_eq!(code, 0, "{json}");
    assert_eq!(json["count"], 1, "{json}");

    let (out, err, code) = run_text(&["on", &p.name, "--in", &other]);
    assert_eq!(code, 2, "{out}{err}");
    assert_eq!(
        err.lines()
            .next()
            .unwrap_or_default()
            .trim_start_matches("Error: "),
        format!(
            "No process found matching '{}' (no matches with specified filters)",
            p.name
        ),
        "the target sits in the quotes, the reason follows them: {err}"
    );

    let (out, err, code) = run_text(&["on", &p.name, "--by", "no-such-process-name"]);
    assert_eq!(code, 2, "{out}{err}");
}

#[test]
fn on_name_is_the_whole_name_and_cmd_widens_it_to_the_command_line() {
    let word = nobody();
    let p = Owned::named_with_args(&format!("pcp{}on", std::process::id()), &[&word]);

    // the word is only in the command line: not a name, and the error says so
    let (out, err, code) = run_text(&["on", &word]);
    assert_eq!(code, 2, "{out}{err}");
    assert!(err.contains("No process found matching"), "{err}");
    assert!(
        err.contains(&format!(
            "1 process has '{word}' in its command line (add --cmd to match those)"
        )),
        "{err}"
    );

    let (json, code) = run_json(&["on", &word, "--cmd", "--json"]);
    assert_eq!(code, 0, "{json}");
    assert_eq!(json["count"], 1, "{json}");
    assert_eq!(json["results"][0]["process"]["pid"], p.pid());

    // part of the name is not the name either
    let part = &p.name[..p.name.len() - 1];
    let (out, err, code) = run_text(&["on", part]);
    assert_eq!(code, 2, "{out}{err}");
    let (json, code) = run_json(&["on", part, "--cmd", "--json"]);
    assert_eq!(code, 0, "{json}");
}

#[test]
fn on_a_name_nothing_runs_exits_not_found() {
    let (out, err, code) = run_text(&["on", &nobody()]);
    assert_eq!(code, 2, "{out}{err}");
    assert!(err.contains("No process found matching"), "{err}");
}

// ---- on: several targets ----

#[test]
fn on_several_targets_shows_each_and_warns_about_the_missing_one() {
    let a = Owned::listener();
    let b = Owned::listener();
    let missing = unused_port();
    let targets = format!("{},{},:{missing}", a.target(), b.pid_arg());
    let (out, _, code) = run_text(&["on", &targets]);
    assert_eq!(code, 0, "one match is enough: {out}");
    assert!(
        out.contains(&format!("Port {} is used by:", a.port)),
        "{out}"
    );
    assert!(
        out.contains(&format!("(PID {}) is listening on:", b.pid())),
        "{out}"
    );
    assert!(out.contains(&format!("Port {missing}:")), "{out}");
    assert!(out.contains("No process listening on port"), "{out}");
}

#[test]
fn on_several_targets_warns_about_a_missing_pid_and_name() {
    let a = Owned::listener();
    let nobody = nobody();
    let targets = format!("{},4294967,{nobody}", a.name);
    let (out, _, code) = run_text(&["on", &targets]);
    assert_eq!(code, 0, "{out}");
    assert!(out.contains("PID 4294967:"), "{out}");
    assert!(out.contains(&format!("'{nobody}':")), "{out}");
}

#[test]
fn on_several_targets_all_missing_exits_not_found() {
    let nobody = nobody();
    let targets = format!(":{},4294967,{nobody}", unused_port());
    let (out, err, code) = run_text(&["on", &targets]);
    assert_eq!(code, 2, "{out}{err}");
    // Each target printed its own warning, so the final error adds no message
    assert!(out.contains("No process listening on port"), "{out}");
    assert!(out.contains("PID 4294967:"), "{out}");
    assert!(out.contains(&format!("'{nobody}':")), "{out}");
    // the comma list is the one thing stderr says
    assert_eq!(err.lines().count(), 1, "{err}");
    assert!(err.starts_with("note: comma-separated targets are deprecated"));
}

#[test]
fn on_several_targets_json_has_one_result_per_process() {
    let a = Owned::listener();
    let b = Owned::listener();
    let missing = format!(":{}", unused_port());
    let targets = format!("{},{},{},{missing}", a.target(), b.pid_arg(), a.name);
    let (json, code) = run_json(&["on", &targets, "--json"]);
    assert_eq!(code, 0, "{json}");
    assert_eq!(json["action"], "on");
    assert_eq!(json["success"], true);
    assert_eq!(json["count"], 3, "{json}");
    assert_eq!(json["not_found"], serde_json::json!([missing]), "{json}");
    let results = json["results"].as_array().unwrap();
    assert_eq!(results[0]["target"], a.target());
    assert_eq!(results[0]["process"]["pid"], a.pid());
    assert_eq!(results[0]["ports"][0]["port"], a.port);
    assert_eq!(results[1]["target"], b.pid_arg());
    assert_eq!(results[1]["process"]["pid"], b.pid());
    assert_eq!(results[2]["target"], a.name);
    assert_eq!(results_for(&json, a.pid()), 2, "{json}");
}

#[test]
fn on_several_targets_json_with_nothing_found_exits_not_found() {
    let targets = format!(":{},4294967", unused_port());
    let (json, code) = run_json(&["on", &targets, "--json"]);
    assert_eq!(code, 2, "{json}");
    assert_eq!(json["success"], false);
}

#[test]
fn on_several_targets_json_counts_a_filtered_out_process_as_not_found() {
    // `a` runs in the directory, `b` somewhere else
    let a = Owned::listener();
    let b = Owned::listener();
    let targets = format!("{},{}", a.target(), b.pid_arg());
    let (json, code) = run_json(&["on", &targets, "--in", &a.dir(), "--json"]);
    assert_eq!(code, 0, "{json}");
    assert_eq!(json["count"], 1, "{json}");
    assert_eq!(json["results"][0]["process"]["pid"], a.pid());
    assert_eq!(
        json["not_found"],
        serde_json::json!([b.pid_arg()]),
        "{json}"
    );

    // Everything filtered out is an error, not an empty answer
    let (json, code) = run_json(&["on", &b.pid_arg(), "--in", &a.dir(), "--json"]);
    assert_eq!(code, 2, "{json}");
}

#[test]
fn on_takes_several_targets_as_separate_arguments_and_commas_still_work() {
    let a = Owned::listener();
    let b = Owned::listener();
    let missing = format!(":{}", unused_port());
    // (target, PID) of every result: one per matching process, in the order typed
    let found = |json: &Value| -> Vec<(String, u64)> {
        json["results"]
            .as_array()
            .unwrap()
            .iter()
            .map(|r| {
                (
                    r["target"].as_str().unwrap().to_string(),
                    r["process"]["pid"].as_u64().unwrap(),
                )
            })
            .collect()
    };
    let want = vec![
        (a.target(), u64::from(a.pid())),
        (b.pid_arg(), u64::from(b.pid())),
        (a.name.clone(), u64::from(a.pid())),
    ];

    let (separate, code) =
        run_json(&["on", &a.target(), &b.pid_arg(), &a.name, &missing, "--json"]);
    assert_eq!(code, 0, "{separate}");
    assert_eq!(found(&separate), want, "{separate}");
    assert_eq!(separate["count"], 3, "{separate}");
    assert_eq!(separate["not_found"], serde_json::json!([missing]));

    // the old form, and the two mixed, give the same answer
    let comma = format!("{},{},{},{missing}", a.target(), b.pid_arg(), a.name);
    let (joined, _) = run_json(&["on", &comma, "--json"]);
    assert_eq!(found(&joined), want, "{joined}");
    assert_eq!(joined["not_found"], separate["not_found"]);
    let tail = format!("{},{missing}", b.pid_arg());
    let (mixed, _) = run_json(&["on", &a.target(), &tail, &a.name, "--json"]);
    assert_eq!(found(&mixed), want, "{mixed}");
    assert_eq!(mixed["not_found"], separate["not_found"]);

    // human output: each target is shown, a missing one is warned about
    let (out, _, code) = run_text(&["on", &a.target(), &b.pid_arg(), &missing]);
    assert_eq!(code, 0, "{out}");
    assert!(
        out.contains(&format!("Port {} is used by:", a.port)),
        "{out}"
    );
    assert!(
        out.contains(&format!("(PID {}) is listening on:", b.pid())),
        "{out}"
    );
    assert!(out.contains("No process listening on port"), "{out}");
}

#[test]
fn on_with_nothing_found_echoes_the_targets_as_typed() {
    // two PIDs nothing runs: the error names both, as typed
    let (json, code) = run_json(&["on", "4294967", "4294968", "--json"]);
    assert_eq!(code, 2, "{json}");
    let error = json["error"].as_str().unwrap();
    assert!(
        error.contains("No process found matching '4294967 4294968'"),
        "{error}"
    );
    // and a target that splits into nothing
    let (json, code) = run_json(&["on", ",", ",,", "--json"]);
    assert_eq!(code, 2, "{json}");
    let error = json["error"].as_str().unwrap();
    assert!(
        error.contains("No process found matching ', ,,'"),
        "{error}"
    );
    let (out, err, code) = run_text(&["on", ",", ",,"]);
    assert_eq!(code, 2, "{out}{err}");
}

// ---- ports ----

/// The `ports` rows (JSON) for the listeners whose process name is `name`
/// (`--cmd` in `extra` makes it any part of the name)
fn ports_by(name: &str, extra: &[&str]) -> Value {
    let mut args = vec!["ports", "--by", name, "--json"];
    args.extend_from_slice(extra);
    let (json, code) = run_json(&args);
    assert_eq!(code, 0, "{json}");
    json
}

#[test]
fn ports_lists_a_listener_as_a_table() {
    let p = Owned::listener();
    let (out, _, code) = run_text(&["ports", "--by", &p.name]);
    assert_eq!(code, 0, "{out}");
    assert!(out.contains("Found 1 listening port\n"), "{out}");
    for heading in ["PORT", "PROTO", "PID", "PROCESS", "ADDRESS"] {
        assert!(out.contains(heading), "{out}");
    }
    let row = out
        .lines()
        .find(|l| l.contains(&p.name))
        .unwrap_or_else(|| panic!("no row for {}: {out}", p.name));
    assert!(row.contains(&p.port.to_string()), "{row}");
    assert!(row.contains("TCP"), "{row}");
    assert!(row.contains(&p.pid().to_string()), "{row}");
    assert!(row.contains("127.0.0.1"), "{row}");
    assert!(!out.contains('↳'), "paths are for --verbose: {out}");
}

#[test]
fn ports_pluralises_the_count() {
    // two listeners with one name, in directories of their own
    let name = format!("pcp{}pl", std::process::id());
    let a = Owned::named(&name);
    let b = Owned::named(&name);
    let (out, _, code) = run_text(&["ports", "--by", &name]);
    assert_eq!(code, 0, "{out}");
    assert!(out.contains("Found 2 listening ports\n"), "{out}");
    drop((a, b));
}

#[test]
fn ports_with_no_match_says_so() {
    let (out, _, code) = run_text(&["ports", "--by", "no-such-process-name-anywhere"]);
    assert_eq!(code, 0, "{out}");
    assert!(out.contains("No listening ports found"), "{out}");

    let (json, code) = run_json(&["ports", "--by", "no-such-process-name-anywhere", "--json"]);
    assert_eq!(code, 0, "{json}");
    assert_eq!(json["action"], "ports");
    assert_eq!(json["count"], 0);
    assert_eq!(json["ports"].as_array().unwrap().len(), 0);
}

#[test]
fn ports_json_describes_each_listener() {
    let p = Owned::listener();
    let json = ports_by(&p.name, &[]);
    assert_eq!(json["success"], true);
    assert_eq!(json["count"], 1, "{json}");
    let row = &json["ports"][0];
    assert_eq!(row["port"], p.port);
    assert_eq!(row["protocol"], "tcp");
    assert_eq!(row["pid"], p.pid());
    assert_eq!(row["address"], "127.0.0.1");
    assert!(row.get("exe_path").is_none(), "{row}");
}

#[test]
fn ports_by_matches_the_whole_name_in_any_case() {
    let p = Owned::listener();
    let shouted = p.name.to_uppercase();
    let json = ports_by(&shouted, &[]);
    assert_eq!(json["count"], 1, "{json}");
    assert_eq!(json["ports"][0]["pid"], p.pid());
}

#[test]
fn ports_by_does_not_match_part_of_a_name_unless_cmd_is_given() {
    let p = Owned::listener();
    // the name without its last character
    let part = &p.name[..p.name.len() - 1];
    assert_eq!(ports_by(part, &[])["count"], 0);
    let json = ports_by(part, &["--cmd"]);
    assert!(
        json["ports"]
            .as_array()
            .unwrap()
            .iter()
            .any(|row| row["pid"] == p.pid()),
        "{json}"
    );
}

#[test]
fn ports_by_ignores_a_word_that_is_only_in_the_command_line_unless_cmd_is_given() {
    let word = nobody();
    let p = Owned::named_with_args(&format!("pcp{}cl", std::process::id()), &[&word]);
    assert_eq!(ports_by(&word, &[])["count"], 0);
    let json = ports_by(&word, &["--cmd"]);
    assert_eq!(json["count"], 1, "{json}");
    assert_eq!(json["ports"][0]["pid"], p.pid());
}

#[test]
fn ports_verbose_shows_the_executable_path() {
    let p = Owned::listener();
    let (out, _, code) = run_text(&["ports", "--by", &p.name, "--verbose"]);
    assert_eq!(code, 0, "{out}");
    let path_line = out
        .lines()
        .find(|l| l.contains('↳'))
        .unwrap_or_else(|| panic!("no path line: {out}"));
    let json = ports_by(&p.name, &["--verbose"]);
    let exe = json["ports"][0]["exe_path"].as_str().unwrap();
    assert!(exe.contains(&p.name), "{exe}");

    // The table cuts a long path; whatever it shows is the start of the real one
    let shown = path_line.trim().trim_start_matches('↳').trim();
    let start: String = exe.chars().take(40).collect();
    assert!(shown.starts_with(&start), "{shown} is not the path {exe}");
    if exe.chars().count() <= 55 {
        assert_eq!(shown, exe);
    }
}

#[test]
fn ports_in_keeps_only_processes_started_in_that_directory() {
    let p = Owned::listener();
    let (_other, other) = other_dir();
    let json = ports_by(&p.name, &["--in", &p.dir()]);
    assert_eq!(json["count"], 1, "{json}");
    let json = ports_by(&p.name, &["--in", &other]);
    assert_eq!(json["count"], 0, "{json}");
}

#[test]
fn ports_exposed_and_local_split_on_the_bind_address() {
    // The listener binds 127.0.0.1 only
    let p = Owned::listener();
    assert_eq!(ports_by(&p.name, &["--local"])["count"], 1);
    assert_eq!(ports_by(&p.name, &["--exposed"])["count"], 0);
}

#[test]
fn ports_range_keeps_ports_inside_it() {
    let p = Owned::listener();
    let inside = format!("{}-{}", p.port, p.port);
    assert_eq!(ports_by(&p.name, &["--range", &inside])["count"], 1);
    let below = format!("1-{}", p.port - 1);
    assert_eq!(ports_by(&p.name, &["--range", &below])["count"], 0);
    if p.port < u16::MAX {
        let above = format!("{}-65535", p.port + 1);
        assert_eq!(ports_by(&p.name, &["--range", &above])["count"], 0);
    }
}

#[test]
fn ports_sorts_by_port_pid_or_name() {
    let pid = std::process::id();
    // The first one started has the later name, so name order differs from start order
    let first = Owned::named(&format!("pcp{pid}so_z"));
    let second = Owned::named(&format!("pcp{pid}so_a"));
    // both names start with it, so this needs --cmd
    let filter = format!("pcp{pid}so_");

    let order = |sort: &str, key: &str| -> Vec<Value> {
        let json = ports_by(&filter, &["--cmd", "--sort", sort]);
        assert_eq!(json["count"], 2, "{json}");
        json["ports"]
            .as_array()
            .unwrap()
            .iter()
            .map(|p| p[key].clone())
            .collect()
    };

    let mut by_port = [first.port, second.port];
    by_port.sort_unstable();
    assert_eq!(order("port", "port"), by_port.map(Value::from).to_vec());

    let mut by_pid = [first.pid(), second.pid()];
    by_pid.sort_unstable();
    assert_eq!(order("pid", "pid"), by_pid.map(Value::from).to_vec());

    // "so_a" before "so_z", whatever the PIDs and ports are
    assert_eq!(
        order("name", "pid"),
        vec![Value::from(second.pid()), Value::from(first.pid())]
    );
}

#[test]
fn ports_limit_cuts_the_list() {
    let pid = std::process::id();
    // two listeners with one name, in directories of their own
    let filter = format!("pcp{pid}lm");
    let _a = Owned::named(&filter);
    let _b = Owned::named(&filter);
    assert_eq!(ports_by(&filter, &[])["count"], 2);
    let json = ports_by(&filter, &["--limit", "1"]);
    assert_eq!(json["count"], 1, "{json}");
    assert_eq!(json["ports"].as_array().unwrap().len(), 1);
}