diff options
Diffstat (limited to 'src/db/model/build_state.rs')
| -rw-r--r-- | src/db/model/build_state.rs | 68 |
1 files changed, 64 insertions, 4 deletions
diff --git a/src/db/model/build_state.rs b/src/db/model/build_state.rs index a5998de..d081bca 100644 --- a/src/db/model/build_state.rs +++ b/src/db/model/build_state.rs @@ -13,7 +13,9 @@ pub enum BuildState { } impl BuildState { - pub const ALL: &'static [BuildState] = &[ + /// Every state, in column-text order. `parse` and `predecessors` + /// below are its only production readers, so it is not exported. + const ALL: &'static [BuildState] = &[ BuildState::Queued, BuildState::Leased, BuildState::Running, @@ -46,7 +48,9 @@ impl BuildState { ) } - pub fn can_transition(&self, to: BuildState) -> bool { + /// The transition table itself. `predecessors` is how production + /// enforces it; nothing outside this module asks the question directly. + fn can_transition(&self, to: BuildState) -> bool { use BuildState::*; if *self == to { return true; @@ -98,7 +102,7 @@ pub enum StepState { } impl StepState { - pub const ALL: &'static [StepState] = &[ + const ALL: &'static [StepState] = &[ StepState::Pending, StepState::Running, StepState::Succeeded, @@ -126,7 +130,7 @@ impl StepState { matches!(self, StepState::Succeeded | StepState::Skipped) } - pub fn can_transition(&self, to: StepState) -> bool { + fn can_transition(&self, to: StepState) -> bool { use StepState::*; if *self == to { return true; @@ -167,3 +171,59 @@ pub const BUILD_STEPS: &[&str] = &[ "publish", ]; +#[cfg(test)] +mod tests { + use super::*; + + // Moved verbatim from tests/state_machine.rs. Both are properties of the + // state enum itself — a column-text round trip and the shape of the + // transition table — and neither needed the database the rest of that file + // was built around. + + #[test] + fn every_state_round_trips_through_its_column_text() { + for s in BuildState::ALL { + assert_eq!(BuildState::parse(s.as_str()), Some(*s), "{s:?} does not round-trip"); + } + assert_eq!(BuildState::parse("exploded"), None); + assert_eq!(BuildState::parse(""), None); + } + + #[test] + fn the_transition_table_is_the_machine_the_code_actually_uses() { + let reachable = |from: BuildState| -> Vec<&'static str> { + BuildState::ALL + .iter() + .filter(|to| from.can_transition(**to)) + .map(|to| to.as_str()) + .collect() + }; + assert_eq!(reachable(BuildState::Queued), ["queued", "leased", "running", "blocked_missing_dep", "cancelled"]); + assert_eq!( + reachable(BuildState::Leased), + ["queued", "leased", "running", "blocked_missing_dep", "succeeded", "failed", "cancelled"] + ); + assert_eq!( + reachable(BuildState::Running), + ["queued", "running", "blocked_missing_dep", "succeeded", "failed", "cancelled"] + ); + assert_eq!(reachable(BuildState::BlockedMissingDep), ["queued", "blocked_missing_dep", "cancelled"]); + assert_eq!(reachable(BuildState::Failed), ["queued", "running", "failed"]); + assert_eq!(reachable(BuildState::Cancelled), ["queued", "cancelled"]); + assert_eq!(reachable(BuildState::Succeeded), ["succeeded"]); + // The claim that `a_terminal_build_is_history_and_does_not_move` used + // to make here against the table, while its database-level twin stayed + // in tests/state_machine.rs. + for to in [BuildState::Running, BuildState::Queued, BuildState::Failed] { + assert!(!BuildState::Succeeded.can_transition(to), "succeeded must not transition to {to:?}"); + } + } + + #[test] + fn step_states_round_trip_through_their_column_text() { + for s in StepState::ALL { + assert_eq!(StepState::parse(s.as_str()), Some(*s), "{s:?} does not round-trip"); + } + assert_eq!(StepState::parse("exploded"), None); + } +} |
