|
| 1 | +use anstream::println; |
| 2 | +use anyhow::{Context, Result}; |
| 3 | + |
| 4 | +use crate::api::{BacklogApi, BacklogClient}; |
| 5 | + |
| 6 | +pub struct ResolutionListArgs { |
| 7 | + json: bool, |
| 8 | +} |
| 9 | + |
| 10 | +impl ResolutionListArgs { |
| 11 | + pub fn new(json: bool) -> Self { |
| 12 | + Self { json } |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +pub fn list(args: &ResolutionListArgs) -> Result<()> { |
| 17 | + let client = BacklogClient::from_config()?; |
| 18 | + list_with(args, &client) |
| 19 | +} |
| 20 | + |
| 21 | +pub fn list_with(args: &ResolutionListArgs, api: &dyn BacklogApi) -> Result<()> { |
| 22 | + let resolutions = api.get_resolutions()?; |
| 23 | + if args.json { |
| 24 | + println!( |
| 25 | + "{}", |
| 26 | + serde_json::to_string_pretty(&resolutions).context("Failed to serialize JSON")? |
| 27 | + ); |
| 28 | + } else { |
| 29 | + for r in &resolutions { |
| 30 | + println!("[{}] {}", r.id, r.name); |
| 31 | + } |
| 32 | + } |
| 33 | + Ok(()) |
| 34 | +} |
| 35 | + |
| 36 | +#[cfg(test)] |
| 37 | +mod tests { |
| 38 | + use super::*; |
| 39 | + use crate::api::resolution::Resolution; |
| 40 | + use anyhow::anyhow; |
| 41 | + |
| 42 | + struct MockApi { |
| 43 | + resolutions: Option<Vec<Resolution>>, |
| 44 | + } |
| 45 | + |
| 46 | + impl crate::api::BacklogApi for MockApi { |
| 47 | + fn get_resolutions(&self) -> anyhow::Result<Vec<Resolution>> { |
| 48 | + self.resolutions |
| 49 | + .clone() |
| 50 | + .ok_or_else(|| anyhow!("no resolutions")) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + fn sample_resolutions() -> Vec<Resolution> { |
| 55 | + vec![ |
| 56 | + Resolution { |
| 57 | + id: 0, |
| 58 | + name: "Fixed".to_string(), |
| 59 | + }, |
| 60 | + Resolution { |
| 61 | + id: 1, |
| 62 | + name: "Won't Fix".to_string(), |
| 63 | + }, |
| 64 | + Resolution { |
| 65 | + id: 2, |
| 66 | + name: "Invalid".to_string(), |
| 67 | + }, |
| 68 | + ] |
| 69 | + } |
| 70 | + |
| 71 | + fn args(json: bool) -> ResolutionListArgs { |
| 72 | + ResolutionListArgs::new(json) |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn list_with_text_output_succeeds() { |
| 77 | + let api = MockApi { |
| 78 | + resolutions: Some(sample_resolutions()), |
| 79 | + }; |
| 80 | + assert!(list_with(&args(false), &api).is_ok()); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn list_with_json_output_succeeds() { |
| 85 | + let api = MockApi { |
| 86 | + resolutions: Some(sample_resolutions()), |
| 87 | + }; |
| 88 | + assert!(list_with(&args(true), &api).is_ok()); |
| 89 | + } |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn list_with_propagates_api_error() { |
| 93 | + let api = MockApi { resolutions: None }; |
| 94 | + let err = list_with(&args(false), &api).unwrap_err(); |
| 95 | + assert!(err.to_string().contains("no resolutions")); |
| 96 | + } |
| 97 | +} |
0 commit comments