|
| 1 | +use anstream::println; |
| 2 | +use anyhow::Result; |
| 3 | +use std::path::PathBuf; |
| 4 | + |
| 5 | +use crate::api::{BacklogApi, BacklogClient, space::SpaceAttachment}; |
| 6 | + |
| 7 | +pub struct SpaceUploadAttachmentArgs { |
| 8 | + file: PathBuf, |
| 9 | + json: bool, |
| 10 | +} |
| 11 | + |
| 12 | +impl SpaceUploadAttachmentArgs { |
| 13 | + pub fn new(file: PathBuf, json: bool) -> Self { |
| 14 | + Self { file, json } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +pub fn upload_attachment(args: &SpaceUploadAttachmentArgs) -> Result<()> { |
| 19 | + let client = BacklogClient::from_config()?; |
| 20 | + upload_attachment_with(args, &client) |
| 21 | +} |
| 22 | + |
| 23 | +pub fn upload_attachment_with( |
| 24 | + args: &SpaceUploadAttachmentArgs, |
| 25 | + api: &dyn BacklogApi, |
| 26 | +) -> Result<()> { |
| 27 | + let attachment = api.upload_space_attachment(&args.file)?; |
| 28 | + if args.json { |
| 29 | + crate::cmd::print_json(&attachment)?; |
| 30 | + } else { |
| 31 | + println!("{}", format_attachment_text(&attachment)); |
| 32 | + } |
| 33 | + Ok(()) |
| 34 | +} |
| 35 | + |
| 36 | +fn format_attachment_text(a: &SpaceAttachment) -> String { |
| 37 | + format!( |
| 38 | + "ID: {}\nName: {}\nSize: {} bytes\nCreated: {}", |
| 39 | + a.id, a.name, a.size, a.created |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +#[cfg(test)] |
| 44 | +mod tests { |
| 45 | + use super::*; |
| 46 | + use anyhow::anyhow; |
| 47 | + use std::collections::BTreeMap; |
| 48 | + use tempfile::NamedTempFile; |
| 49 | + |
| 50 | + use crate::api::space::{SpaceAttachment, SpaceAttachmentUser}; |
| 51 | + |
| 52 | + struct MockApi { |
| 53 | + result: Option<SpaceAttachment>, |
| 54 | + } |
| 55 | + |
| 56 | + impl crate::api::BacklogApi for MockApi { |
| 57 | + fn upload_space_attachment( |
| 58 | + &self, |
| 59 | + _file_path: &std::path::Path, |
| 60 | + ) -> anyhow::Result<SpaceAttachment> { |
| 61 | + self.result.clone().ok_or_else(|| anyhow!("upload failed")) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + fn sample_attachment() -> SpaceAttachment { |
| 66 | + SpaceAttachment { |
| 67 | + id: 1, |
| 68 | + name: "test.txt".to_string(), |
| 69 | + size: 100, |
| 70 | + created_user: SpaceAttachmentUser { |
| 71 | + id: 1, |
| 72 | + user_id: Some("alice".to_string()), |
| 73 | + name: "Alice".to_string(), |
| 74 | + extra: BTreeMap::new(), |
| 75 | + }, |
| 76 | + created: "2024-01-01T00:00:00Z".to_string(), |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + fn args(json: bool) -> SpaceUploadAttachmentArgs { |
| 81 | + let tmp = NamedTempFile::new().unwrap(); |
| 82 | + SpaceUploadAttachmentArgs::new(tmp.path().to_path_buf(), json) |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn upload_attachment_with_text_output_succeeds() { |
| 87 | + let api = MockApi { |
| 88 | + result: Some(sample_attachment()), |
| 89 | + }; |
| 90 | + assert!(upload_attachment_with(&args(false), &api).is_ok()); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn upload_attachment_with_json_output_succeeds() { |
| 95 | + let api = MockApi { |
| 96 | + result: Some(sample_attachment()), |
| 97 | + }; |
| 98 | + assert!(upload_attachment_with(&args(true), &api).is_ok()); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn upload_attachment_with_propagates_api_error() { |
| 103 | + let api = MockApi { result: None }; |
| 104 | + let err = upload_attachment_with(&args(false), &api).unwrap_err(); |
| 105 | + assert!(err.to_string().contains("upload failed")); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn format_attachment_text_contains_all_fields() { |
| 110 | + let text = format_attachment_text(&sample_attachment()); |
| 111 | + assert!(text.contains("1")); |
| 112 | + assert!(text.contains("test.txt")); |
| 113 | + assert!(text.contains("100")); |
| 114 | + assert!(text.contains("2024-01-01T00:00:00Z")); |
| 115 | + } |
| 116 | +} |
0 commit comments