From fb5160d2d91a428f84673b5b0fdf422a4b7c2fca Mon Sep 17 00:00:00 2001 From: Wataru Otsubo Date: Tue, 23 Jul 2024 22:38:22 +0900 Subject: [PATCH] fix: master log parse (invalid station detection in status parse) - missed switch for `Station1` - added integrated test to check lines count of output csv --- src/masterlog.rs | 28 ++++++++++++++++------------ tests/cli.rs | 23 ++++++++++++++--------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/masterlog.rs b/src/masterlog.rs index 1f90e95..23fd45a 100644 --- a/src/masterlog.rs +++ b/src/masterlog.rs @@ -2,7 +2,7 @@ use std::{collections::BTreeMap, io::BufRead, str::FromStr}; use anyhow::{anyhow, Context, Result}; use chrono::{DateTime, Utc}; -use log::{debug, info, trace}; +use log::{debug, error, info, trace}; use regex::Regex; use semver::Version; @@ -123,7 +123,9 @@ impl MasterLogResult { debug!("End of assignments"); break; } + debug!("line: {}", line); let (pos, id) = extract_position_id(&line)?; + trace!("pos: {}", pos); match assignments.insert(pos.clone(), id) { None => (), Some(old_id) => { @@ -163,9 +165,17 @@ impl MasterLogResult { let mut board_results = BTreeMap::new(); for station_minor in [0, 1] { info!("Result for {:?}", station_minor); - for _ in 1..10 { + + // Loop for sufficiently large number to reach `Station1` + for _ in 1..20 { let line = lines.next().context("Invalid log format(result body)")??; - if line.contains("Station1") || line.contains("======") { + trace!("line: {}", line); + if line.contains("======") { + // end of status + break; + } + if line.contains(&format!("Station{}", station_minor + 1)) { + debug!("Next station: line: {}", line); break; } let parts: Vec<&str> = line.split('|').collect(); @@ -182,7 +192,7 @@ impl MasterLogResult { .parse::() }) } - .context("Invalid station format")??; + .context(format!("Invalid station format: {}", line))??; let station_id = match station_minor { 0 => raw_station_id, 1 => raw_station_id - 10, @@ -283,14 +293,8 @@ mod test { #[test] fn parse_runid_line() { - assert_eq!( - extract_runid_line("QAQC runid: 7").unwrap(), - 7 - ); - assert_eq!( - extract_runid_line("QAQC runid: 12345").unwrap(), - 12345 - ); + assert_eq!(extract_runid_line("QAQC runid: 7").unwrap(), 7); + assert_eq!(extract_runid_line("QAQC runid: 12345").unwrap(), 12345); } #[test] diff --git a/tests/cli.rs b/tests/cli.rs index af3aa72..daa9449 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -7,6 +7,7 @@ mod integrated_test { use anyhow::Result; use assert_cmd::Command; + use itertools::Itertools; use predicates::prelude::*; #[test] @@ -26,17 +27,21 @@ mod integrated_test { // check output { + // check output content let f = File::open(test_out.clone())?; let r = BufReader::new(f); - assert!(r - .lines() - .any(|line| { - line - .unwrap() - .contains( - "8866,,B-0-1,0,1,1,0,1,8,1,,7,2024-07-20T17:15:46Z,7.log,0.1.0,alice,,,," - ) - })); + assert!(r.lines().any(|line| { + line.unwrap().contains( + "8866,,B-0-1,0,1,1,0,1,8,1,,7,2024-07-20T17:15:46Z,7.log,0.1.0,alice,,,,", + ) + })); + } + { + // Check output lines count + let f = File::open(test_out.clone())?; + let r = BufReader::new(f); + let lc = r.lines().collect_vec().len(); + assert_eq!(lc, 19); } // 2nd file