use std::collections::VecDeque;
use crate::tool_output::ruby::{is_backtrace_frame, is_harmless_warning};
const KEPT: usize = 10;
const HEAD: usize = 5;
#[derive(Debug, Default)]
pub(crate) struct Tail {
head: Vec<String>,
last: VecDeque<String>,
dropped: usize,
}
impl Tail {
pub(crate) fn push(&mut self, line: &str) {
if is_backtrace_frame(line) || is_harmless_warning(line) {
return;
}
if self.head.len() < HEAD {
self.head.push(line.to_owned());
return;
}
if self.last.len() == KEPT {
self.last.pop_front();
self.dropped += 1;
}
self.last.push_back(line.to_owned());
}
pub(crate) fn into_lines(self) -> Vec<String> {
let gap = (self.dropped > 0).then(|| format!("[{} lines left out]", self.dropped));
self.head.into_iter().chain(gap).chain(self.last).collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_tail_keeps_the_first_and_last_lines_without_backtrace_frames() {
let mut tail = Tail::default();
for i in 0..HEAD + KEPT + 2 {
tail.push(&format!("line {i}"));
}
tail.push("\tfrom /usr/lib/vagrant/action/builder.rb:180:in `call'");
let lines = tail.into_lines();
assert_eq!(lines.len(), HEAD + 1 + KEPT);
assert_eq!(lines[0], "line 0", "the first kept");
assert_eq!(lines[HEAD], "[2 lines left out]");
assert_eq!(lines[HEAD + 1], "line 7", "the oldest of the rest dropped");
assert_eq!(lines.last().map(String::as_str), Some("line 16"), "the frame not kept");
}
#[test]
fn a_short_error_is_kept_whole() {
let mut tail = Tail::default();
for i in 0..HEAD + KEPT {
tail.push(&format!("line {i}"));
}
let lines = tail.into_lines();
assert_eq!(lines.len(), HEAD + KEPT);
assert!(lines.iter().all(|l| !l.contains("left out")));
}
#[test]
fn fogs_warnings_arent_a_failures_last_lines() {
let mut tail = Tail::default();
tail.push("[fog][WARNING] Unrecognized arguments: libvirt_ip_command");
tail.push("The SSH command responded with a non-zero exit status.");
tail.push(" [fog][WARNING] Unrecognized arguments: libvirt_ip_command");
assert_eq!(tail.into_lines(), ["The SSH command responded with a non-zero exit status."]);
assert!(!is_harmless_warning("[fog][ERROR] connection refused"));
}
}