cli_tests.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. use super::*;
  2. use clap::Parser;
  3. /// Parse `sm121 health <args>` and return the parsed `--format`.
  4. fn health_format(args: &[&str]) -> Result<(OutputFormat, bool), clap::Error> {
  5. let cli = Cli::try_parse_from(
  6. std::iter::once("sm121")
  7. .chain(std::iter::once("health"))
  8. .chain(args.iter().copied()),
  9. )?;
  10. let Commands::Health {
  11. fingerprint,
  12. format,
  13. ..
  14. } = cli.command
  15. else {
  16. panic!("expected Health")
  17. };
  18. Ok((format, fingerprint))
  19. }
  20. #[test]
  21. fn health_format_defaults_to_plain() {
  22. assert_eq!(health_format(&[]).unwrap(), (OutputFormat::Plain, false));
  23. }
  24. #[test]
  25. fn health_format_parses_each_variant() {
  26. assert_eq!(
  27. health_format(&["--format", "plain"]).unwrap().0,
  28. OutputFormat::Plain
  29. );
  30. assert_eq!(
  31. health_format(&["--format", "md"]).unwrap().0,
  32. OutputFormat::Md
  33. );
  34. assert_eq!(
  35. health_format(&["--format", "html"]).unwrap().0,
  36. OutputFormat::Html
  37. );
  38. }
  39. #[test]
  40. fn health_format_combines_with_cluster_and_fingerprint() {
  41. let (fmt, fp) = health_format(&["--cluster", "c1", "--format", "md"]).unwrap();
  42. assert_eq!((fmt, fp), (OutputFormat::Md, false));
  43. let (fmt, fp) = health_format(&["--fingerprint", "--format", "html"]).unwrap();
  44. assert_eq!((fmt, fp), (OutputFormat::Html, true));
  45. }
  46. #[test]
  47. fn health_format_rejects_invalid_value() {
  48. assert!(health_format(&["--format", "bogus"]).is_err());
  49. }
  50. #[test]
  51. fn report_flag_defaults_and_overrides() {
  52. let cli = Cli::try_parse_from(["sm121", "report"]).unwrap();
  53. let Commands::Report {
  54. serve,
  55. port,
  56. open,
  57. archive: _,
  58. unarchive: _,
  59. archived: _,
  60. } = cli.command
  61. else {
  62. unreachable!()
  63. };
  64. assert!(!serve && !open);
  65. assert_eq!(port, 3101);
  66. let cli = Cli::try_parse_from(["sm121", "report", "--open", "--port", "4000"]).unwrap();
  67. let Commands::Report { open, port, .. } = cli.command else {
  68. unreachable!()
  69. };
  70. assert!(open);
  71. assert_eq!(port, 4000);
  72. }