#![cfg(not(feature = "lt2024_2"))]
use std::path::Path;
use perforce_cli::P4Cli;
use perforce_cli::cmd::Diagnostics;
use perforce_cli::global::GlobalOpts;
mod common;
use common::{echo_bin, expect_args, expect_chain, spawned_args};
fn echo_diagnostics() -> Diagnostics {
Diagnostics::new(echo_bin("diagnostics"), GlobalOpts::new())
}
#[test]
fn basic_form() {
let p4 = P4Cli::default();
expect_chain!(["diagnostics"], p4.cmd_diagnostics());
expect_chain!(["diagnostics"], p4.cmd_diagnostics().basic());
let mut cmd = echo_diagnostics();
assert_eq!(spawned_args(&mut cmd, ()), ["diagnostics"]);
let mut cmd = echo_diagnostics().basic();
assert_eq!(spawned_args(&mut cmd, ()), ["diagnostics"]);
}
#[cfg(not(feature = "lt2025_2"))]
#[test]
fn journal_form() {
let p4 = P4Cli::default();
expect_chain!(["diagnostics", "-J"], p4.cmd_diagnostics().journal());
expect_chain!(
["diagnostics", "-J"],
p4.cmd_diagnostics().basic().journal()
);
let mut cmd = echo_diagnostics().journal();
assert_eq!(spawned_args(&mut cmd, ()), ["diagnostics", "-J"]);
}
#[test]
fn journal_with_limit() {
let p4 = P4Cli::default();
#[cfg(not(feature = "lt2025_2"))]
expect_chain!(
["diagnostics", "-J", "-L", "10m"],
p4.cmd_diagnostics().basic().journal_with("10m")
);
#[cfg(feature = "lt2025_2")]
expect_chain!(
["diagnostics", "-L", "10m"],
p4.cmd_diagnostics().limit("10m")
);
#[cfg(not(feature = "lt2025_2"))]
expect_chain!(
["diagnostics", "-J", "-L", "2g"],
p4.cmd_diagnostics().journal().limit("2g")
);
#[cfg(feature = "lt2025_2")]
expect_chain!(
["diagnostics", "-L", "2g"],
p4.cmd_diagnostics().basic().limit("2g")
);
#[cfg(not(feature = "lt2025_2"))]
let mut cmd = echo_diagnostics().basic().journal_with(String::from("10m"));
#[cfg(feature = "lt2025_2")]
let mut cmd = echo_diagnostics().limit(String::from("10m"));
#[cfg(not(feature = "lt2025_2"))]
assert_eq!(
spawned_args(&mut cmd, ()),
["diagnostics", "-J", "-L", "10m"]
);
#[cfg(feature = "lt2025_2")]
assert_eq!(spawned_args(&mut cmd, ()), ["diagnostics", "-L", "10m"]);
}
#[test]
fn journal_limit_accessors() {
#[cfg(not(feature = "lt2025_2"))]
let mut cmd = Diagnostics::new("p4", GlobalOpts::new())
.basic()
.journal_with("10m");
#[cfg(feature = "lt2025_2")]
let mut cmd = Diagnostics::new("p4", GlobalOpts::new()).limit("10m");
assert_eq!(cmd.get_limit(), Some("10m"));
cmd.set_limit("1t");
assert_eq!(cmd.get_limit(), Some("1t"));
#[cfg(not(feature = "lt2025_2"))]
expect_args(&cmd, &["diagnostics", "-J", "-L", "1t"]);
#[cfg(feature = "lt2025_2")]
expect_args(&cmd, &["diagnostics", "-L", "1t"]);
}
#[cfg(not(feature = "lt2025_2"))]
#[test]
fn json_format_form() {
let p4 = P4Cli::default();
expect_chain!(["diagnostics", "-j"], p4.cmd_diagnostics().json_format());
expect_chain!(
["diagnostics", "-j", "-J"],
p4.cmd_diagnostics().json_format().journal()
);
expect_chain!(
["diagnostics", "-j"],
p4.cmd_diagnostics().basic().json_format(true)
);
expect_chain!(
["diagnostics"],
p4.cmd_diagnostics().basic().json_format(false)
);
let mut cmd = Diagnostics::new("p4", GlobalOpts::new()).json_format();
assert!(cmd.get_json_format());
cmd.set_json_format(false);
assert!(!cmd.get_json_format());
expect_args(&cmd, &["diagnostics"]);
let mut cmd = echo_diagnostics().json_format();
assert_eq!(spawned_args(&mut cmd, ()), ["diagnostics", "-j"]);
}
#[cfg(not(feature = "lt2025_2"))]
#[test]
fn uncompressed_flag() {
let p4 = P4Cli::default();
expect_chain!(
["diagnostics", "-D"],
p4.cmd_diagnostics().uncompressed(true)
);
expect_chain!(
["diagnostics", "-D"],
p4.cmd_diagnostics().uncompressed(true).basic()
);
expect_chain!(
["diagnostics", "-D", "-l"],
p4.cmd_diagnostics().uncompressed(true).run_lsof()
);
expect_chain!(["diagnostics"], p4.cmd_diagnostics().uncompressed(false));
let mut cmd = Diagnostics::new("p4", GlobalOpts::new()).uncompressed(true);
assert!(cmd.get_uncompressed());
cmd.set_uncompressed(false);
assert!(!cmd.get_uncompressed());
expect_args(&cmd, &["diagnostics"]);
let mut cmd = echo_diagnostics().uncompressed(true);
assert_eq!(spawned_args(&mut cmd, ()), ["diagnostics", "-D"]);
}
#[test]
fn lsof_form() {
let p4 = P4Cli::default();
expect_chain!(["diagnostics", "-l"], p4.cmd_diagnostics().run_lsof());
expect_chain!(
["diagnostics"],
p4.cmd_diagnostics().run_lsof().run_lsof(false)
);
let mut cmd = Diagnostics::new("p4", GlobalOpts::new()).run_lsof();
assert!(cmd.get_run_lsof());
cmd.set_run_lsof(false);
assert!(!cmd.get_run_lsof());
expect_args(&cmd, &["diagnostics"]);
let mut cmd = echo_diagnostics().run_lsof();
assert_eq!(spawned_args(&mut cmd, ()), ["diagnostics", "-l"]);
}
#[cfg(not(feature = "lt2025_2"))]
#[test]
fn ps_form() {
let p4 = P4Cli::default();
expect_chain!(["diagnostics", "-p"], p4.cmd_diagnostics().run_ps());
expect_chain!(["diagnostics"], p4.cmd_diagnostics().run_ps().run_ps(false));
let mut cmd = Diagnostics::new("p4", GlobalOpts::new()).run_ps();
assert!(cmd.get_run_ps());
cmd.set_run_ps(false);
assert!(!cmd.get_run_ps());
expect_args(&cmd, &["diagnostics"]);
}
#[cfg(not(feature = "lt2025_2"))]
#[test]
fn top_form() {
let p4 = P4Cli::default();
expect_chain!(["diagnostics", "-t"], p4.cmd_diagnostics().run_top());
expect_chain!(
["diagnostics"],
p4.cmd_diagnostics().run_top().run_top(false)
);
let mut cmd = Diagnostics::new("p4", GlobalOpts::new()).run_top();
assert!(cmd.get_run_top());
cmd.set_run_top(false);
assert!(!cmd.get_run_top());
expect_args(&cmd, &["diagnostics"]);
}
#[test]
fn strace_form() {
let p4 = P4Cli::default();
expect_chain!(["diagnostics", "-s"], p4.cmd_diagnostics().run_strace());
expect_chain!(
["diagnostics", "-l", "-s"],
p4.cmd_diagnostics().run_lsof().run_strace()
);
let mut cmd = echo_diagnostics().run_strace();
assert_eq!(spawned_args(&mut cmd, ()), ["diagnostics", "-s"]);
}
#[cfg(not(feature = "lt2025_2"))]
#[test]
fn strace_runtime_option() {
let p4 = P4Cli::default();
expect_chain!(
["diagnostics", "-s", "--strace-runtime", "30"],
p4.cmd_diagnostics().run_strace_with(30)
);
expect_chain!(
["diagnostics", "-s", "--strace-runtime", "15"],
p4.cmd_diagnostics().run_strace().strace_runtime(15)
);
expect_chain!(
["diagnostics", "-l", "-s", "--strace-runtime", "15"],
p4.cmd_diagnostics().run_lsof().run_strace_with(15)
);
let mut cmd = Diagnostics::new("p4", GlobalOpts::new()).run_strace_with(30);
assert_eq!(cmd.get_strace_runtime(), Some(30));
cmd.set_strace_runtime(60);
assert_eq!(cmd.get_strace_runtime(), Some(60));
expect_args(&cmd, &["diagnostics", "-s", "--strace-runtime", "60"]);
let cmd = cmd.strace_runtime(45);
assert_eq!(cmd.get_strace_runtime(), Some(45));
let mut cmd = echo_diagnostics().run_strace_with(30);
assert_eq!(
spawned_args(&mut cmd, ()),
["diagnostics", "-s", "--strace-runtime", "30"]
);
}
#[test]
fn advance_with_basic_form() {
let p4 = P4Cli::default();
expect_chain!(
["diagnostics", "-a"],
p4.cmd_diagnostics().advance_with_basic()
);
expect_chain!(
["diagnostics", "-l", "-a"],
p4.cmd_diagnostics().run_lsof().with_basic()
);
expect_chain!(
["diagnostics", "-s", "-a"],
p4.cmd_diagnostics().run_strace().with_basic()
);
let mut cmd = echo_diagnostics().advance_with_basic();
assert_eq!(spawned_args(&mut cmd, ()), ["diagnostics", "-a"]);
}
#[test]
fn advance_with_basic_journal() {
let p4 = P4Cli::default();
#[cfg(not(feature = "lt2025_2"))]
expect_chain!(
["diagnostics", "-a", "-J"],
p4.cmd_diagnostics().advance_with_basic().journal()
);
#[cfg(not(feature = "lt2025_2"))]
expect_chain!(
["diagnostics", "-l", "-a", "-J", "-L", "10m"],
p4.cmd_diagnostics()
.run_lsof()
.with_basic()
.journal_with("10m")
);
#[cfg(feature = "lt2025_2")]
expect_chain!(
["diagnostics", "-l", "-a", "-L", "10m"],
p4.cmd_diagnostics().run_lsof().with_basic().limit("10m")
);
#[cfg(not(feature = "lt2025_2"))]
let mut cmd = Diagnostics::new("p4", GlobalOpts::new())
.advance_with_basic()
.journal_with("2g");
#[cfg(feature = "lt2025_2")]
let mut cmd = Diagnostics::new("p4", GlobalOpts::new())
.advance_with_basic()
.limit("2g");
assert_eq!(cmd.get_limit(), Some("2g"));
cmd.set_limit("512k");
assert_eq!(cmd.get_limit(), Some("512k"));
#[cfg(not(feature = "lt2025_2"))]
expect_args(&cmd, &["diagnostics", "-a", "-J", "-L", "512k"]);
#[cfg(feature = "lt2025_2")]
expect_args(&cmd, &["diagnostics", "-a", "-L", "512k"]);
#[cfg(not(feature = "lt2025_2"))]
let mut cmd = echo_diagnostics()
.run_strace()
.with_basic()
.journal_with("1g");
#[cfg(feature = "lt2025_2")]
let mut cmd = echo_diagnostics().run_strace().with_basic().limit("1g");
#[cfg(not(feature = "lt2025_2"))]
assert_eq!(
spawned_args(&mut cmd, ()),
["diagnostics", "-s", "-a", "-J", "-L", "1g"]
);
#[cfg(feature = "lt2025_2")]
assert_eq!(
spawned_args(&mut cmd, ()),
["diagnostics", "-s", "-a", "-L", "1g"]
);
}
#[cfg(not(feature = "lt2025_2"))]
#[test]
fn all_options_in_syntax_order() {
let p4 = P4Cli::default();
expect_chain!(
[
"diagnostics",
"-D",
"-l",
"-s",
"--strace-runtime",
"25",
"-p",
"-t",
"-a",
"-J",
"-L",
"2g"
],
p4.cmd_diagnostics()
.uncompressed(true)
.run_lsof()
.run_strace_with(25)
.run_ps(true)
.run_top(true)
.with_basic()
.journal_with("2g")
);
expect_chain!(
[
"diagnostics",
"-D",
"-l",
"-s",
"--strace-runtime",
"25",
"-p",
"-t",
"-a",
"-J",
"-L",
"2g"
],
p4.cmd_diagnostics()
.advance_with_basic()
.journal_with("2g")
.run_top(true)
.run_ps(true)
.run_lsof(true)
.run_strace_with(25)
.uncompressed(true)
);
}
#[cfg(feature = "lt2025_2")]
#[test]
fn all_options_in_syntax_order() {
let p4 = P4Cli::default();
expect_chain!(
["diagnostics", "-l", "-s", "-a", "-L", "2g"],
p4.cmd_diagnostics()
.run_lsof()
.run_strace()
.with_basic()
.limit("2g")
);
}
#[test]
fn global_options_precede_subcommand() {
let global_opts = GlobalOpts::new().port("localhost:1666").quiet_mode(true);
let plain = Diagnostics::new("p4", global_opts.clone());
expect_args(&plain, &["-p", "localhost:1666", "-q", "diagnostics"]);
let basic = Diagnostics::new("p4", global_opts.clone()).basic();
expect_args(&basic, &["-p", "localhost:1666", "-q", "diagnostics"]);
let advance = Diagnostics::new("p4", global_opts.clone()).run_lsof();
expect_args(
&advance,
&["-p", "localhost:1666", "-q", "diagnostics", "-l"],
);
#[cfg(not(feature = "lt2025_2"))]
let journal = Diagnostics::new("p4", global_opts)
.basic()
.journal_with("10m");
#[cfg(not(feature = "lt2025_2"))]
expect_args(
&journal,
&[
"-p",
"localhost:1666",
"-q",
"diagnostics",
"-J",
"-L",
"10m",
],
);
#[cfg(feature = "lt2025_2")]
let journal = Diagnostics::new("p4", global_opts).limit("10m");
#[cfg(feature = "lt2025_2")]
expect_args(
&journal,
&["-p", "localhost:1666", "-q", "diagnostics", "-L", "10m"],
);
}
#[test]
fn bin_accessors() {
let mut cmd = Diagnostics::new("p4", GlobalOpts::new());
assert_eq!(cmd.get_bin(), Path::new("p4"));
cmd.set_bin("/usr/local/bin/p4");
assert_eq!(cmd.get_bin(), Path::new("/usr/local/bin/p4"));
let cmd = cmd.bin("p4.exe");
assert_eq!(cmd.get_bin(), Path::new("p4.exe"));
}