| 1 | //! A library for parsing and generating Debian patch headers. |
| 2 | //! |
| 3 | //! # Examples |
| 4 | //! |
| 5 | //! ```rust |
| 6 | //! use dep3::lossless::PatchHeader; |
| 7 | //! use std::str::FromStr; |
| 8 | //! let text = r#"From: John Doe <john.doe@example> |
| 9 | //! Date: Mon, 1 Jan 2000 00:00:00 +0000 |
| 10 | //! Subject: [PATCH] fix a bug |
| 11 | //! Bug-Debian: https://bugs.debian.org/123456 |
| 12 | //! Bug: https://bugzilla.example.com/bug.cgi?id=123456 |
| 13 | //! Forwarded: not-needed |
| 14 | //! "#; |
| 15 | //! |
| 16 | //! let patch_header = PatchHeader::from_str(text).unwrap(); |
| 17 | //! assert_eq!(patch_header.description(), Some("[PATCH] fix a bug".to_string())); |
| 18 | //! assert_eq!(patch_header.vendor_bugs("Debian").collect::<Vec<_>>(), vec!["https://bugs.debian.org/123456".to_string()]); |
| 19 | //! ``` |
| 20 | use deb822_lossless::Paragraph; |
| 21 | |
| 22 | use crate::fields::*; |
| 23 | |
| 24 | /// Encode free text for storage in a deb822 multi-line field value. |
| 25 | /// |
| 26 | /// deb822 multi-line values cannot contain blank lines (lines that are |
| 27 | /// empty or contain only whitespace), because the parser interprets those |
| 28 | /// as paragraph separators. By convention, a blank line is represented |
| 29 | /// as a continuation line containing only a single dot (` .`). This |
| 30 | /// function applies that encoding so that the result is safe to pass to |
| 31 | /// [`Paragraph::insert`]. |
| 32 | fn encode_multiline_value(text: &str) -> String { |
| 33 | // Replace every \n followed by another \n (or by whitespace-only |
| 34 | // content up to the next \n) with the deb822 blank-line marker. |
| 35 | let mut out = String::with_capacity(text.len()); |
| 36 | for line in text.split('\n') { |
| 37 | if !out.is_empty() { |
| 38 | out.push('\n'); |
| 39 | } |
| 40 | if line.trim().is_empty() && !out.is_empty() { |
| 41 | out.push_str(" ."); |
| 42 | } else { |
| 43 | out.push_str(line); |
| 44 | } |
| 45 | } |
| 46 | out |
| 47 | } |
| 48 | |
| 49 | /// A Debian patch header. |
| 50 | pub struct PatchHeader(Paragraph); |
| 51 | |
| 52 | impl PatchHeader { |
| 53 | /// Create a new, empty patch header. |
| 54 | pub fn new() -> Self { |
| 55 | PatchHeader(Paragraph::new()) |
| 56 | } |
| 57 | |
| 58 | /// Get a reference to the underlying `Paragraph`. |
| 59 | pub fn as_deb822(&self) -> &Paragraph { |
| 60 | &self.0 |
| 61 | } |
| 62 | |
| 63 | /// Get a mutable reference to the underlying `Paragraph`, mutably. |
| 64 | pub fn as_deb822_mut(&mut self) -> &mut Paragraph { |
| 65 | &mut self.0 |
| 66 | } |
| 67 | |
| 68 | /// The origin of the patch. |
| 69 | pub fn origin(&self) -> Option<(Option<OriginCategory>, Origin)> { |
| 70 | self.0 |
| 71 | .get("Origin") |
| 72 | .as_deref() |
| 73 | .map(crate::fields::parse_origin) |
| 74 | } |
| 75 | |
| 76 | /// Set the origin of the patch. |
| 77 | pub fn set_origin(&mut self, category: Option<OriginCategory>, origin: Origin) { |
| 78 | self.0.insert( |
| 79 | "Origin", |
| 80 | crate::fields::format_origin(&category, &origin).as_str(), |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /// The `Forwarded` field. |
| 85 | pub fn forwarded(&self) -> Option<Forwarded> { |
| 86 | self.0 |
| 87 | .get("Forwarded") |
| 88 | .as_deref() |
| 89 | .map(|s| s.parse().unwrap()) |
| 90 | } |
| 91 | |
| 92 | /// Set the `Forwarded` field. |
| 93 | pub fn set_forwarded(&mut self, forwarded: Forwarded) { |
| 94 | self.0.insert("Forwarded", forwarded.to_string().as_str()); |
| 95 | } |
| 96 | |
| 97 | /// The author of the patch. |
| 98 | pub fn author(&self) -> Option<String> { |
| 99 | self.0.get("Author").or_else(|| self.0.get("From")) |
| 100 | } |
| 101 | |
| 102 | /// Set the author of the patch. |
| 103 | pub fn set_author(&mut self, author: &str) { |
| 104 | if self.0.contains_key("From") { |
| 105 | self.0.insert("From", author); |
| 106 | } else { |
| 107 | self.0.insert("Author", author); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// The `Reviewed-By` field. |
| 112 | pub fn reviewed_by(&self) -> Vec<String> { |
| 113 | self.0.get_all("Reviewed-By").collect() |
| 114 | } |
| 115 | |
| 116 | /// Get the last update date of the patch. |
| 117 | pub fn last_update(&self) -> Option<chrono::NaiveDate> { |
| 118 | self.0 |
| 119 | .get("Last-Update") |
| 120 | .as_deref() |
| 121 | .and_then(|s| chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d").ok()) |
| 122 | } |
| 123 | |
| 124 | /// Set the date of the last update |
| 125 | pub fn set_last_update(&mut self, date: chrono::NaiveDate) { |
| 126 | self.0 |
| 127 | .insert("Last-Update", date.format("%Y-%m-%d").to_string().as_str()); |
| 128 | } |
| 129 | |
| 130 | /// The `Applied-Upstream` field. |
| 131 | pub fn applied_upstream(&self) -> Option<AppliedUpstream> { |
| 132 | self.0 |
| 133 | .get("Applied-Upstream") |
| 134 | .as_deref() |
| 135 | .map(|s| s.parse().unwrap()) |
| 136 | } |
| 137 | |
| 138 | /// Set the `Applied-Upstream` field. |
| 139 | pub fn set_applied_upstream(&mut self, applied_upstream: AppliedUpstream) { |
| 140 | self.0 |
| 141 | .insert("Applied-Upstream", applied_upstream.to_string().as_str()); |
| 142 | } |
| 143 | |
| 144 | /// Get the bugs associated with the patch. |
| 145 | pub fn bugs(&self) -> impl Iterator<Item = (Option<String>, String)> + '_ { |
| 146 | self.0.items().filter_map(|(k, v)| { |
| 147 | if k.starts_with("Bug-") { |
| 148 | Some((Some(k.strip_prefix("Bug-").unwrap().to_string()), v)) |
| 149 | } else if k == "Bug" { |
| 150 | Some((None, v)) |
| 151 | } else { |
| 152 | None |
| 153 | } |
| 154 | }) |
| 155 | } |
| 156 | |
| 157 | /// Get the bugs associated with a specific vendor. |
| 158 | pub fn vendor_bugs<'a>(&'a self, vendor: &'a str) -> impl Iterator<Item = String> + 'a { |
| 159 | self.bugs().filter_map(|(k, v)| { |
| 160 | if k == Some(vendor.to_string()) { |
| 161 | Some(v) |
| 162 | } else { |
| 163 | None |
| 164 | } |
| 165 | }) |
| 166 | } |
| 167 | |
| 168 | /// Iterate over Debian BTS bug numbers parsed from `Bug-Debian:` (and a |
| 169 | /// bare `Bug:` field when no vendor is specified). |
| 170 | /// |
| 171 | /// Accepts three value forms: a bare decimal number (`123456`), a |
| 172 | /// `#`-prefixed number, or a `https://bugs.debian.org/NNNNNN` URL. Values |
| 173 | /// that don't match are silently skipped. |
| 174 | pub fn debian_bug_ids(&self) -> impl Iterator<Item = u32> + '_ { |
| 175 | self.bugs().filter_map(|(vendor, value)| { |
| 176 | if let Some(v) = vendor { |
| 177 | if !v.eq_ignore_ascii_case("debian") { |
| 178 | return None; |
| 179 | } |
| 180 | } |
| 181 | crate::fields::parse_debian_bug_id(&value) |
| 182 | }) |
| 183 | } |
| 184 | |
| 185 | /// Set the upstream bug associated with the patch. |
| 186 | pub fn set_upstream_bug(&mut self, bug: &str) { |
| 187 | self.0.insert("Bug", bug); |
| 188 | } |
| 189 | |
| 190 | /// Set the bug associated with a specific vendor. |
| 191 | pub fn set_vendor_bug(&mut self, vendor: &str, bug: &str) { |
| 192 | self.0.insert(format!("Bug-{}", vendor).as_str(), bug); |
| 193 | } |
| 194 | |
| 195 | /// Get the description or subject field. |
| 196 | fn description_field(&self) -> Option<String> { |
| 197 | self.0.get("Description").or_else(|| self.0.get("Subject")) |
| 198 | } |
| 199 | |
| 200 | /// Get the description of the patch. |
| 201 | pub fn description(&self) -> Option<String> { |
| 202 | self.description_field() |
| 203 | .as_deref() |
| 204 | .map(|s| s.split('\n').next().unwrap_or(s).to_string()) |
| 205 | } |
| 206 | |
| 207 | /// Set the description of the patch. |
| 208 | pub fn set_description(&mut self, description: &str) { |
| 209 | let description = encode_multiline_value(description); |
| 210 | if let Some(subject) = self.0.get("Subject") { |
| 211 | // Replace the first line with ours |
| 212 | let new = format!( |
| 213 | "{}\n{}", |
| 214 | description, |
| 215 | subject.split_once('\n').map(|x| x.1).unwrap_or("") |
| 216 | ); |
| 217 | self.0.set("Subject", new.as_str()); |
| 218 | } else if let Some(existing) = self.0.get("Description") { |
| 219 | // Replace the first line with ours |
| 220 | let new = format!( |
| 221 | "{}\n{}", |
| 222 | description, |
| 223 | existing.split_once('\n').map(|x| x.1).unwrap_or("") |
| 224 | ); |
| 225 | self.0.set("Description", new.as_str()); |
| 226 | } else { |
| 227 | self.0.insert("Description", description.as_str()); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /// Get the long description of the patch. |
| 232 | pub fn long_description(&self) -> Option<String> { |
| 233 | self.description_field() |
| 234 | .as_deref() |
| 235 | .map(|s| s.split_once('\n').map(|x| x.1).unwrap_or("").to_string()) |
| 236 | } |
| 237 | |
| 238 | /// Set the long description of the patch. |
| 239 | pub fn set_long_description(&mut self, long_description: &str) { |
| 240 | let long_description = encode_multiline_value(long_description); |
| 241 | if let Some(subject) = self.0.get("Subject") { |
| 242 | // Keep the first line, but replace the rest with our text |
| 243 | let first_line = subject |
| 244 | .split_once('\n') |
| 245 | .map(|x| x.0) |
| 246 | .unwrap_or(subject.as_str()); |
| 247 | let new = format!("{}\n{}", first_line, long_description); |
| 248 | self.0.set("Subject", new.as_str()); |
| 249 | } else if let Some(description) = self.0.get("Description") { |
| 250 | // Keep the first line, but replace the rest with our text |
| 251 | let first_line = description |
| 252 | .split_once('\n') |
| 253 | .map(|x| x.0) |
| 254 | .unwrap_or(description.as_str()); |
| 255 | let new = format!("{}\n{}", first_line, long_description); |
| 256 | self.0.set("Description", new.as_str()); |
| 257 | } else { |
| 258 | self.0.insert("Description", long_description.as_str()); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /// Write the patch header |
| 263 | pub fn write<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { |
| 264 | writer.write_all(self.to_string().as_bytes()) |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | impl std::fmt::Display for PatchHeader { |
| 269 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 270 | f.write_str(&self.0.to_string()) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | impl Default for PatchHeader { |
| 275 | fn default() -> Self { |
| 276 | Self::new() |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | impl std::str::FromStr for PatchHeader { |
| 281 | type Err = deb822_lossless::ParseError; |
| 282 | |
| 283 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 284 | Ok(PatchHeader(Paragraph::from_str(s)?)) |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | pub use crate::header_end; |
| 289 | |
| 290 | impl PatchHeader { |
| 291 | /// Parse a patch file's DEP-3 header, tolerating a trailing unified |
| 292 | /// diff body. Splits `content` at the first `---` / `diff ` / |
| 293 | /// `Index:` line and parses only the header portion. |
| 294 | /// |
| 295 | /// Returns the parsed header and the byte offset where the diff |
| 296 | /// body starts (equal to `content.len()` if there is no diff body). |
| 297 | /// Use this for files like `debian/patches/foo.patch` where the |
| 298 | /// caller has the whole file in hand and wants just the header. |
| 299 | /// |
| 300 | /// `from_str` parses the input as deb822 in its entirety and will |
| 301 | /// fail (or, with malformed continuations, misparse) when handed a |
| 302 | /// patch with diff content; `parse_relaxed` is the appropriate |
| 303 | /// entry point for that case. |
| 304 | pub fn parse_relaxed(content: &str) -> Result<(Self, usize), deb822_lossless::ParseError> { |
| 305 | use std::str::FromStr; |
| 306 | let end = header_end(content); |
| 307 | let header = PatchHeader::from_str(&content[..end])?; |
| 308 | Ok((header, end)) |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | #[cfg(test)] |
| 313 | mod tests { |
| 314 | use super::PatchHeader; |
| 315 | use std::borrow::Cow; |
| 316 | use std::str::FromStr; |
| 317 | |
| 318 | #[test] |
| 319 | fn test_upstream() { |
| 320 | let text = r#"From: Ulrich Drepper <[email protected]> |
| 321 | Subject: Fix regex problems with some multi-bytes characters |
| 322 | . |
| 323 | * posix/bug-regex17.c: Add testcases. |
| 324 | * posix/regcomp.c (re_compile_fastmap_iter): Rewrite COMPLEX_BRACKET |
| 325 | handling. |
| 326 | . |
| 327 | Origin: upstream, http://sourceware.org/git/?p=glibc.git;a=commitdiff;h=bdb56bac |
| 328 | Bug: http://sourceware.org/bugzilla/show_bug.cgi?id=9697 |
| 329 | Bug-Debian: http://bugs.debian.org/510219 |
| 330 | "#; |
| 331 | |
| 332 | let header = PatchHeader::from_str(text).unwrap(); |
| 333 | |
| 334 | assert_eq!( |
| 335 | header.origin(), |
| 336 | Some(( |
| 337 | Some(super::OriginCategory::Upstream), |
| 338 | super::Origin::Other(Cow::Borrowed( |
| 339 | "http://sourceware.org/git/?p=glibc.git;a=commitdiff;h=bdb56bac" |
| 340 | )) |
| 341 | )) |
| 342 | ); |
| 343 | assert_eq!(header.forwarded(), None); |
| 344 | assert_eq!( |
| 345 | header.author(), |
| 346 | Some("Ulrich Drepper <[email protected]>".to_string()) |
| 347 | ); |
| 348 | assert_eq!(header.reviewed_by(), Vec::<&str>::new()); |
| 349 | assert_eq!(header.last_update(), None); |
| 350 | assert_eq!(header.applied_upstream(), None); |
| 351 | assert_eq!( |
| 352 | header.bugs().collect::<Vec<_>>(), |
| 353 | vec![ |
| 354 | ( |
| 355 | None, |
| 356 | "http://sourceware.org/bugzilla/show_bug.cgi?id=9697".to_string() |
| 357 | ), |
| 358 | ( |
| 359 | Some("Debian".to_string()), |
| 360 | "http://bugs.debian.org/510219".to_string() |
| 361 | ), |
| 362 | ] |
| 363 | ); |
| 364 | assert_eq!( |
| 365 | header.description(), |
| 366 | Some("Fix regex problems with some multi-bytes characters".to_string()) |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | #[test] |
| 371 | fn test_forwarded() { |
| 372 | let text = r#"Description: Use FHS compliant paths by default |
| 373 | Upstream is not interested in switching to those paths. |
| 374 | . |
| 375 | But we will continue using them in Debian nevertheless to comply with |
| 376 | our policy. |
| 377 | Forwarded: http://lists.example.com/oct-2006/1234.html |
| 378 | Author: John Doe <[email protected]> |
| 379 | Last-Update: 2006-12-21 |
| 380 | "#; |
| 381 | let header = PatchHeader::from_str(text).unwrap(); |
| 382 | |
| 383 | assert_eq!(header.origin(), None); |
| 384 | assert_eq!( |
| 385 | header.forwarded(), |
| 386 | Some(super::Forwarded::Yes(Cow::Borrowed( |
| 387 | "http://lists.example.com/oct-2006/1234.html" |
| 388 | ))) |
| 389 | ); |
| 390 | assert_eq!( |
| 391 | header.author(), |
| 392 | Some("John Doe <[email protected]>".to_string()) |
| 393 | ); |
| 394 | assert_eq!(header.reviewed_by(), Vec::<&str>::new()); |
| 395 | assert_eq!( |
| 396 | header.last_update(), |
| 397 | Some(chrono::NaiveDate::from_ymd_opt(2006, 12, 21).unwrap()) |
| 398 | ); |
| 399 | assert_eq!(header.applied_upstream(), None); |
| 400 | assert_eq!(header.bugs().collect::<Vec<_>>(), vec![]); |
| 401 | assert_eq!( |
| 402 | header.description(), |
| 403 | Some("Use FHS compliant paths by default".to_string()) |
| 404 | ); |
| 405 | } |
| 406 | |
| 407 | #[test] |
| 408 | fn test_not_forwarded() { |
| 409 | let text = r#"Description: Workaround for broken symbol resolving on mips/mipsel |
| 410 | The correct fix will be done in etch and it will require toolchain |
| 411 | fixes. |
| 412 | Forwarded: not-needed |
| 413 | Origin: vendor, http://bugs.debian.org/cgi-bin/bugreport.cgi?msg=80;bug=265678 |
| 414 | Bug-Debian: http://bugs.debian.org/265678 |
| 415 | Author: Thiemo Seufer <[email protected]> |
| 416 | "#; |
| 417 | |
| 418 | let header = PatchHeader::from_str(text).unwrap(); |
| 419 | |
| 420 | assert_eq!( |
| 421 | header.origin(), |
| 422 | Some(( |
| 423 | Some(super::OriginCategory::Vendor), |
| 424 | super::Origin::Other(Cow::Borrowed( |
| 425 | "http://bugs.debian.org/cgi-bin/bugreport.cgi?msg=80;bug=265678" |
| 426 | )) |
| 427 | )) |
| 428 | ); |
| 429 | assert_eq!(header.forwarded(), Some(super::Forwarded::NotNeeded)); |
| 430 | assert_eq!( |
| 431 | header.author(), |
| 432 | Some("Thiemo Seufer <[email protected]>".to_string()) |
| 433 | ); |
| 434 | assert_eq!(header.reviewed_by(), Vec::<&str>::new()); |
| 435 | assert_eq!(header.last_update(), None); |
| 436 | assert_eq!(header.applied_upstream(), None); |
| 437 | assert_eq!( |
| 438 | header.bugs().collect::<Vec<_>>(), |
| 439 | vec![( |
| 440 | Some("Debian".to_string()), |
| 441 | "http://bugs.debian.org/265678".to_string() |
| 442 | ),] |
| 443 | ); |
| 444 | |
| 445 | assert_eq!( |
| 446 | header.description(), |
| 447 | Some("Workaround for broken symbol resolving on mips/mipsel".to_string()) |
| 448 | ); |
| 449 | } |
| 450 | |
| 451 | #[test] |
| 452 | fn test_applied_upstream() { |
| 453 | let text = r#"Description: Fix widget frobnication speeds |
| 454 | Frobnicating widgets too quickly tended to cause explosions. |
| 455 | Forwarded: http://lists.example.com/2010/03/1234.html |
| 456 | Author: John Doe <[email protected]> |
| 457 | Applied-Upstream: 1.2, http://bzr.example.com/frobnicator/trunk/revision/123 |
| 458 | Last-Update: 2010-03-29 |
| 459 | "#; |
| 460 | let header = PatchHeader::from_str(text).unwrap(); |
| 461 | |
| 462 | assert_eq!(header.origin(), None); |
| 463 | assert_eq!( |
| 464 | header.forwarded(), |
| 465 | Some(super::Forwarded::Yes(Cow::Borrowed( |
| 466 | "http://lists.example.com/2010/03/1234.html" |
| 467 | ))) |
| 468 | ); |
| 469 | assert_eq!( |
| 470 | header.author(), |
| 471 | Some("John Doe <[email protected]>".to_string()) |
| 472 | ); |
| 473 | assert_eq!(header.reviewed_by(), Vec::<&str>::new()); |
| 474 | assert_eq!( |
| 475 | header.last_update(), |
| 476 | Some(chrono::NaiveDate::from_ymd_opt(2010, 3, 29).unwrap()) |
| 477 | ); |
| 478 | assert_eq!( |
| 479 | header.applied_upstream(), |
| 480 | Some(super::AppliedUpstream::Other(Cow::Borrowed( |
| 481 | "1.2, http://bzr.example.com/frobnicator/trunk/revision/123" |
| 482 | ))) |
| 483 | ); |
| 484 | assert_eq!(header.bugs().collect::<Vec<_>>(), vec![]); |
| 485 | assert_eq!( |
| 486 | header.description(), |
| 487 | Some("Fix widget frobnication speeds".to_string()) |
| 488 | ); |
| 489 | } |
| 490 | |
| 491 | #[test] |
| 492 | fn test_vendor_bugs() { |
| 493 | let text = r#"Description: Fix widget frobnication speeds |
| 494 | Bug: http://bugs.example.com/123 |
| 495 | Bug-Debian: http://bugs.debian.org/123 |
| 496 | Bug-Ubuntu: http://bugs.launchpad.net/123 |
| 497 | "#; |
| 498 | |
| 499 | let header = PatchHeader::from_str(text).unwrap(); |
| 500 | |
| 501 | assert_eq!( |
| 502 | header.vendor_bugs("Debian").collect::<Vec<_>>(), |
| 503 | vec!["http://bugs.debian.org/123".to_string()] |
| 504 | ); |
| 505 | assert_eq!( |
| 506 | header.vendor_bugs("Ubuntu").collect::<Vec<_>>(), |
| 507 | vec!["http://bugs.launchpad.net/123".to_string()] |
| 508 | ); |
| 509 | } |
| 510 | |
| 511 | #[test] |
| 512 | fn parse_relaxed_splits_at_dashes() { |
| 513 | let text = "Author: alice\nDescription: bla\n---\n@@ -1 +1 @@\n-x\n+y\n"; |
| 514 | let (header, end) = PatchHeader::parse_relaxed(text).unwrap(); |
| 515 | assert_eq!(end, "Author: alice\nDescription: bla\n".len()); |
| 516 | assert_eq!(header.author(), Some("alice".to_string())); |
| 517 | assert_eq!(header.description(), Some("bla".to_string())); |
| 518 | } |
| 519 | |
| 520 | #[test] |
| 521 | fn parse_relaxed_splits_at_diff_word() { |
| 522 | let text = "Author: alice\ndiff --git a/foo b/foo\n"; |
| 523 | let (header, end) = PatchHeader::parse_relaxed(text).unwrap(); |
| 524 | assert_eq!(end, "Author: alice\n".len()); |
| 525 | assert_eq!(header.author(), Some("alice".to_string())); |
| 526 | } |
| 527 | |
| 528 | #[test] |
| 529 | fn parse_relaxed_splits_at_index_marker() { |
| 530 | let text = "Author: alice\nIndex: foo\n@@ -1 +1 @@\n"; |
| 531 | let (header, end) = PatchHeader::parse_relaxed(text).unwrap(); |
| 532 | assert_eq!(end, "Author: alice\n".len()); |
| 533 | assert_eq!(header.author(), Some("alice".to_string())); |
| 534 | } |
| 535 | |
| 536 | #[test] |
| 537 | fn parse_relaxed_handles_header_only() { |
| 538 | let text = "Author: alice\nDescription: bla\n"; |
| 539 | let (header, end) = PatchHeader::parse_relaxed(text).unwrap(); |
| 540 | assert_eq!(end, text.len()); |
| 541 | assert_eq!(header.author(), Some("alice".to_string())); |
| 542 | } |
| 543 | |
| 544 | #[test] |
| 545 | fn header_end_handles_empty() { |
| 546 | assert_eq!(super::header_end(""), 0); |
| 547 | } |
| 548 | |
| 549 | #[test] |
| 550 | fn test_set_last_update() { |
| 551 | let text = r#"Description: Fix widget frobnication speeds |
| 552 | "#; |
| 553 | let mut header = PatchHeader::from_str(text).unwrap(); |
| 554 | let date = chrono::NaiveDate::from_ymd_opt(2023, 5, 15).unwrap(); |
| 555 | header.set_last_update(date); |
| 556 | assert_eq!(header.last_update(), Some(date)); |
| 557 | } |
| 558 | |
| 559 | #[test] |
| 560 | fn test_set_description_with_blank_lines() { |
| 561 | // Descriptions containing blank lines (e.g. from fixer result messages) |
| 562 | // must be encoded as " ." continuation lines so that deb822-lossless |
| 563 | // does not reject them as empty continuation lines. |
| 564 | let mut header = PatchHeader::new(); |
| 565 | header.set_description("Fix frobnication\n\nDetails follow."); |
| 566 | // The stored Description value must be parseable (no panic / error). |
| 567 | let rendered = header.to_string(); |
| 568 | let reparsed = PatchHeader::from_str(&rendered).unwrap(); |
| 569 | assert_eq!(reparsed.description(), Some("Fix frobnication".to_string())); |
| 570 | // Blank line must have been encoded as " ." |
| 571 | assert!( |
| 572 | rendered.contains(" ."), |
| 573 | "blank line should be encoded as ' .'" |
| 574 | ); |
| 575 | } |
| 576 | |
| 577 | #[test] |
| 578 | fn test_set_description_replaces_first_line() { |
| 579 | // When a Description already exists, set_description replaces its |
| 580 | // first line while preserving the rest. |
| 581 | let text = "Description: old summary\n old long description\n"; |
| 582 | let mut header = PatchHeader::from_str(text).unwrap(); |
| 583 | header.set_description("new summary"); |
| 584 | assert_eq!(header.description(), Some("new summary".to_string())); |
| 585 | assert_eq!( |
| 586 | header.long_description(), |
| 587 | Some("old long description".to_string()) |
| 588 | ); |
| 589 | } |
| 590 | |
| 591 | #[test] |
| 592 | fn test_debian_bug_ids_from_url() { |
| 593 | let text = "Bug-Debian: https://bugs.debian.org/510219\n"; |
| 594 | let header = PatchHeader::from_str(text).unwrap(); |
| 595 | assert_eq!(header.debian_bug_ids().collect::<Vec<_>>(), vec![510219]); |
| 596 | } |
| 597 | |
| 598 | #[test] |
| 599 | fn test_debian_bug_ids_bare_bug_field_included() { |
| 600 | // A bare `Bug:` field with no vendor counts as Debian per the doc. |
| 601 | let text = "Bug: 12345\n"; |
| 602 | let header = PatchHeader::from_str(text).unwrap(); |
| 603 | assert_eq!(header.debian_bug_ids().collect::<Vec<_>>(), vec![12345]); |
| 604 | } |
| 605 | |
| 606 | #[test] |
| 607 | fn test_debian_bug_ids_vendor_case_insensitive() { |
| 608 | // Bug-DEBIAN should match too. |
| 609 | let text = "Bug-DEBIAN: #42\n"; |
| 610 | let header = PatchHeader::from_str(text).unwrap(); |
| 611 | assert_eq!(header.debian_bug_ids().collect::<Vec<_>>(), vec![42]); |
| 612 | } |
| 613 | |
| 614 | #[test] |
| 615 | fn test_debian_bug_ids_skips_other_vendors() { |
| 616 | // Non-Debian vendor bugs (and unparseable Debian values) are skipped. |
| 617 | let text = "Bug: http://sourceware.org/bugzilla/show_bug.cgi?id=9697\n\ |
| 618 | Bug-Debian: https://bugs.debian.org/510219\n\ |
| 619 | Bug-Ubuntu: https://bugs.launchpad.net/bugs/12345\n"; |
| 620 | let header = PatchHeader::from_str(text).unwrap(); |
| 621 | assert_eq!(header.debian_bug_ids().collect::<Vec<_>>(), vec![510219]); |
| 622 | } |
| 623 | |
| 624 | #[test] |
| 625 | fn test_debian_bug_ids_multiple_entries() { |
| 626 | let text = "Bug-Debian: https://bugs.debian.org/100\n\ |
| 627 | Bug-Debian: #200\n\ |
| 628 | Bug-Debian: 300\n"; |
| 629 | let header = PatchHeader::from_str(text).unwrap(); |
| 630 | assert_eq!( |
| 631 | header.debian_bug_ids().collect::<Vec<_>>(), |
| 632 | vec![100, 200, 300] |
| 633 | ); |
| 634 | } |
| 635 | |
| 636 | #[test] |
| 637 | fn test_debian_bug_ids_empty_when_no_bug_fields() { |
| 638 | let text = "Description: nothing to see here\n"; |
| 639 | let header = PatchHeader::from_str(text).unwrap(); |
| 640 | assert_eq!( |
| 641 | header.debian_bug_ids().collect::<Vec<_>>(), |
| 642 | Vec::<u32>::new() |
| 643 | ); |
| 644 | } |
| 645 | } |