use crate::error::{DeclarationSite, Error, Result};
use crate::grants::built::built;
use crate::types::{Argument, Ceiling, Command, Node, OutputPath, Program, Want};
pub(crate) fn admit(
expanded: Vec<String>,
node: &Node,
ceiling: &Ceiling,
deps: &[OutputPath],
) -> Result<Command> {
let arguments = expanded
.into_iter()
.map(Argument::parse)
.collect::<Result<Vec<Argument>>>()?;
let Some(program) = arguments.first().and_then(Program::of) else {
return Err(Error::NoProgram {
site: Box::new(DeclarationSite::from(node)),
});
};
if !built(&program, deps) && !ceiling.run.contains(&program) {
return Err(Error::Refused {
want: Want::Program(program),
site: Some(Box::new(DeclarationSite::from(node))),
ceiling: Box::new(ceiling.clone()),
});
}
Command::new(arguments)
}
#[cfg(test)]
mod tests {
#![expect(
clippy::unwrap_used,
reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
)]
use std::collections::BTreeSet;
use super::admit;
use crate::error::{Error, NameKind};
use crate::execute::fixtures::{command, node, output_path, program};
use crate::types::{Ceiling, Want};
fn ceiling(run: &[&str]) -> Ceiling {
Ceiling {
run: run.iter().map(|raw| program(raw)).collect(),
env: BTreeSet::new(),
read: BTreeSet::new(),
write: BTreeSet::new(),
}
}
fn texts(arguments: &[&str]) -> Vec<String> {
arguments
.iter()
.map(|argument| (*argument).to_owned())
.collect()
}
fn names_no_program(expanded: &[&str]) {
let app = node("//:app", |_| {});
let error = admit(texts(expanded), &app, &ceiling(&["cc", "out/app"]), &[]).unwrap_err();
assert_eq!(
error.to_string(),
"build.lua: //:app: the command names no program once expanded"
);
match error {
Error::NoProgram { site } => assert_eq!(site.label, app.label),
other => unreachable!("expected NoProgram, got {other:?}"),
}
}
#[test]
fn a_command_whose_program_may_run_is_admitted_as_it_is() {
let text = node("//lib:text", |_| {});
assert_eq!(
admit(
texts(&["cc", "-c", "lib/a.c"]),
&text,
&ceiling(&["cc"]),
&[]
)
.unwrap(),
command(&["cc", "-c", "lib/a.c"])
);
}
#[test]
fn only_the_first_argument_has_to_name_a_program() {
let app = node("//:app", |_| {});
assert_eq!(
admit(texts(&["cc", "", "$input"]), &app, &ceiling(&["cc"]), &[]).unwrap(),
command(&["cc", "", "$input"])
);
}
#[test]
fn a_built_program_outside_the_ceiling_is_admitted() {
let test = node("//:app_test", |_| {});
assert_eq!(
admit(
texts(&["out/app", "--filter=Login"]),
&test,
&ceiling(&["cc"]),
&[output_path("//:app", "app")]
)
.unwrap(),
command(&["out/app", "--filter=Login"])
);
}
#[test]
fn a_program_outside_the_ceiling_that_is_not_built_is_refused() {
let test = node("//:app_test", |_| {});
let expanded = texts(&["out/app", "--filter=Login"]);
let deps = [output_path("//:lib", "lib")];
assert_eq!(
admit(expanded.clone(), &test, &ceiling(&["cc", "out/app"]), &deps).unwrap(),
command(&["out/app", "--filter=Login"])
);
let allowed = ceiling(&["cc"]);
let error = admit(expanded, &test, &allowed, &deps).unwrap_err();
assert_eq!(
error.to_string(),
"build.lua: //:app_test: program out/app is outside the ceiling (proc.run allows: cc)"
);
match error {
Error::Refused {
want,
site,
ceiling,
} => {
assert_eq!(want, Want::Program(program("out/app")));
assert_eq!(site.unwrap().label, test.label);
assert_eq!(*ceiling, allowed);
}
other => unreachable!("expected Refused, got {other:?}"),
}
}
#[test]
fn a_command_that_expanded_to_no_argument_names_no_program() {
names_no_program(&[]);
}
#[test]
fn an_empty_first_argument_names_no_program() {
names_no_program(&["", "out/app"]);
}
#[test]
fn a_first_argument_still_holding_a_dollar_names_no_program() {
names_no_program(&["$input"]);
names_no_program(&["$opt:missing", "cc"]);
names_no_program(&["out/$"]);
}
#[test]
fn a_command_naming_no_program_is_not_asked_of_the_ceiling() {
let app = node("//:app", |_| {});
assert!(matches!(
admit(Vec::new(), &app, &ceiling(&[]), &[]).unwrap_err(),
Error::NoProgram { .. }
));
}
#[test]
fn text_no_argument_can_hold_is_refused_as_an_invalid_argument() {
let app = node("//:app", |_| {});
match admit(texts(&["cc", "a\0b"]), &app, &ceiling(&["cc"]), &[]).unwrap_err() {
Error::InvalidName { kind, .. } => assert_eq!(kind, NameKind::Argument),
other => unreachable!("expected InvalidName, got {other:?}"),
}
}
}