mortise_core/actions/
output.rs1use std::collections::BTreeSet;
10
11use crate::run::ansi::plain;
12
13#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct FailedMigration {
16 pub module: Option<String>,
18 pub migration: String,
20}
21
22pub fn failed_migration(lines: &[String]) -> Option<FailedMigration> {
24 let mut module = None;
25 for line in lines {
26 let line = plain(line);
27 if let Some(name) = running_module(&line) {
28 module = Some(name.to_string());
29 } else if let Some((migration, "FAIL")) = migration_line(&line) {
30 return Some(FailedMigration {
31 module,
32 migration: migration.to_string(),
33 });
34 }
35 }
36 None
37}
38
39const PROHIBITED: &str = "This command is prohibited from running in this environment.";
41
42pub fn prohibited(lines: &[String]) -> bool {
45 lines
46 .iter()
47 .any(|line| plain(line).trim().ends_with(PROHIBITED))
48}
49
50#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
52pub struct MigrationCount {
53 pub ran: usize,
55 pub rolled_back: usize,
57}
58
59pub fn migration_count(lines: &[String]) -> MigrationCount {
65 let mut ran = 0;
66 let mut rolled_back = BTreeSet::new();
67 for line in lines {
68 let line = plain(line);
69 if let Some(name) = rolled_back_name(&line) {
70 if line.trim_end().rsplit(' ').next() != Some("FAIL") {
71 rolled_back.insert(name.to_string());
72 }
73 } else if let Some((_, "DONE")) = migration_line(&line) {
74 ran += 1;
75 }
76 }
77 MigrationCount {
78 ran,
79 rolled_back: rolled_back.len(),
80 }
81}
82
83fn running_module(line: &str) -> Option<&str> {
85 let mut words = line.trim().strip_prefix("Running Migration ")?.split(' ');
86 let name = words.next()?;
87 (words.next() == Some("Module")).then_some(name)
88}
89
90fn migration_line(line: &str) -> Option<(&str, &str)> {
94 let line = line.trim();
95 let line = line.strip_prefix("Rollback: ").unwrap_or(line);
96 let name = line.split(' ').next()?;
97 let end = line.rsplit(' ').next()?;
98 (migration_name(name) && matches!(end, "DONE" | "FAIL")).then_some((name, end))
99}
100
101fn rolled_back_name(line: &str) -> Option<&str> {
103 let (_, rest) = line.split_once("Rollback: ")?;
104 let name = rest.split(' ').next()?.trim_end();
105 migration_name(name).then_some(name)
106}
107
108fn migration_name(name: &str) -> bool {
110 const SHAPE: &[u8] = b"dddd_dd_dd_dddddd_";
111 name.len() > SHAPE.len()
112 && name.bytes().zip(SHAPE).all(|(byte, &shape)| match shape {
113 b'd' => byte.is_ascii_digit(),
114 _ => byte == shape,
115 })
116}
117
118#[cfg(test)]
119pub(crate) mod tests {
120 use std::path::Path;
121
122 use super::*;
123
124 const FIXTURES: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../fixtures");
125
126 pub(crate) fn recording(name: &str) -> Vec<String> {
128 let path = Path::new(FIXTURES)
129 .join("output")
130 .join(format!("{name}.json"));
131 let json: serde_json::Value =
132 serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
133 json["stdout"]
134 .as_str()
135 .unwrap()
136 .lines()
137 .map(str::to_owned)
138 .collect()
139 }
140
141 #[test]
142 fn a_failing_migrate_names_its_migration_and_module() {
143 let lines = recording("module-migrate-failing-migration");
144 assert_eq!(
145 failed_migration(&lines),
146 Some(FailedMigration {
147 module: Some("Shipping".to_string()),
148 migration: "2026_11_22_140500_add_tracking_code_to_shipments_table".to_string(),
149 })
150 );
151 assert_eq!(migration_count(&lines), MigrationCount::default());
152 }
153
154 #[test]
155 fn a_run_counts_what_ran_and_what_rolled_back() {
156 let lines = recording("module-migrate-one-migration");
157 assert_eq!(failed_migration(&lines), None);
158 let table = [
160 ("module-migrate-one-migration", 1, 0),
161 ("module-migrate-rollback", 0, 1),
163 ("module-migrate-reset", 0, 3),
165 ("module-migrate-reset-pretend", 0, 0),
167 ("module-migrate-refresh", 3, 3),
169 ("module-migrate-fresh-all", 8, 0),
171 ];
172 for (name, ran, rolled_back) in table {
173 assert_eq!(
174 migration_count(&recording(name)),
175 MigrationCount { ran, rolled_back },
176 "{name}"
177 );
178 }
179 }
180
181 #[test]
182 fn a_plain_failure_names_no_migration() {
183 let lines = recording("module-failing");
184 assert_eq!(failed_migration(&lines), None);
185 assert!(!prohibited(&lines));
186 }
187
188 #[test]
189 fn a_prohibited_command_reads_as_prohibited() {
190 let lines = recording("module-migrate-reset-prohibited");
191 assert!(prohibited(&lines));
192 assert_eq!(failed_migration(&lines), None);
193 }
194}