Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 56 additions & 21 deletions src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ impl HookScripts {
}

#[derive(Debug, Clone, serde::Deserialize)]
#[serde(untagged, deny_unknown_fields)]
#[serde(untagged)]
pub enum HookDef {
One(HookDefItem),
/// Array of hook definitions: `enter = ["echo hello", { task = "setup" }]`
Array(Vec<HookDefItem>),
}

#[derive(Debug, Clone, serde::Deserialize)]
#[serde(untagged, deny_unknown_fields)]
pub enum HookDefItem {
/// Simple run string: `enter = "echo hello"`
RunString(String),
/// Table with run: `enter = { run = "echo hello" }`
Expand All @@ -86,8 +94,6 @@ pub enum HookDef {
},
/// Task reference: `enter = { task = "setup" }`
TaskRef { task: String },
/// Array of hook definitions: `enter = ["echo hello", { task = "setup" }]`
Array(Vec<HookDef>),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -128,7 +134,19 @@ impl HookDef {
/// Convert to a list of Hook structs with the given hook type
pub fn into_hooks(self, hook_type: Hooks) -> Vec<Hook> {
match self {
HookDef::RunString(script) => vec![Hook {
HookDef::One(def) => vec![def.into_hook(hook_type)],
HookDef::Array(arr) => arr
.into_iter()
.map(|def| def.into_hook(hook_type))
.collect(),
}
}
}

impl HookDefItem {
fn into_hook(self, hook_type: Hooks) -> Hook {
match self {
HookDefItem::RunString(script) => Hook {
hook: hook_type,
action: HookAction::Run {
run: Some(script),
Expand All @@ -138,8 +156,8 @@ impl HookDef {
ignored_shell: None,
},
global: false,
}],
HookDef::Run(table) => vec![Hook {
},
HookDefItem::Run(table) => Hook {
hook: hook_type,
action: HookAction::Run {
run: table.run,
Expand All @@ -149,26 +167,22 @@ impl HookDef {
ignored_shell: None,
},
global: false,
}],
HookDef::ScriptTable { script, shell } => vec![Hook {
},
HookDefItem::ScriptTable { script, shell } => Hook {
hook: hook_type,
action: script_hook_action(hook_type, script.into_script(), shell, true),
global: false,
}],
HookDef::ScriptsTable { scripts, shell } => vec![Hook {
},
HookDefItem::ScriptsTable { scripts, shell } => Hook {
hook: hook_type,
action: script_hook_action(hook_type, scripts.join("\n"), shell, true),
global: false,
}],
HookDef::TaskRef { task } => vec![Hook {
},
HookDefItem::TaskRef { task } => Hook {
hook: hook_type,
action: HookAction::Task { task_name: task },
global: false,
}],
HookDef::Array(arr) => arr
.into_iter()
.flat_map(|d| d.into_hooks(hook_type))
.collect(),
},
}
}
}
Expand Down Expand Up @@ -808,12 +822,33 @@ mod tests {
}
}

#[test]
fn hook_arrays_reject_nested_arrays() {
assert!(
toml::from_str::<TestHook>(
r#"hook = ["echo first", { task = "build" }, { run = "echo last" }]"#
)
.is_ok(),
"flat mixed array should be accepted"
);
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

for input in [
r#"hook = [["echo nested"]]"#,
r#"hook = ["echo first", [{ task = "build" }]]"#,
] {
assert!(
toml::from_str::<TestHook>(input).is_err(),
"unexpectedly accepted {input}"
);
}
}

#[test]
fn scripts_tables_are_legacy_only_when_spawned() {
let spawned = HookDef::ScriptsTable {
let spawned = HookDef::One(HookDefItem::ScriptsTable {
scripts: vec!["echo one".into(), "echo two".into()],
shell: None,
}
})
.into_hooks(Hooks::Postinstall);
assert!(matches!(
&spawned[0].action,
Expand All @@ -823,10 +858,10 @@ mod tests {
}
));

let current_shell = HookDef::ScriptsTable {
let current_shell = HookDef::One(HookDefItem::ScriptsTable {
scripts: vec!["echo one".into(), "echo two".into()],
shell: Some("bash".into()),
}
})
.into_hooks(Hooks::Enter);
assert!(matches!(
&current_shell[0].action,
Expand Down
Loading