use core::sync::atomic::{AtomicBool, Ordering};
use crate::error::{Error, PortName, Result};
use crate::execute::fetch::{Unfetched, fetch, logs, unasked, unusable};
use crate::execute::gate::Gate;
use crate::execute::remote_action::remote_action;
use crate::execute::upload::{Sent, upload, upload_of};
use crate::ports::{Clock, ContentStore, ExecStrategy, RemoteCache};
use crate::types::{
ActionKey, Blob, Channel, Diagnostic, Digest, Executed, Execution, Exit, FileMode, Found,
FoundEntry, FoundKind, InfraKind, LiveSink, NodeId, OutcomeClass, OutputManifest, OutputName,
OutputRefusal, OutputSource, PlatformProperties, RawOutcome, ReadyAction, RemoteAction,
RemoteCall, RemoteExecution, RemoteFailure, RemoteHit, RemoteNotice, RemotePolicy,
RemoteSwitch, RemoteUse, StoredFile, TargetRole, Timestamp, Tree, TreeEntry, TreePath,
};
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Told {
Began(NodeId),
Printed {
node: NodeId,
channel: Channel,
bytes: Vec<u8>,
},
Remote(RemoteNotice),
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Shared<'a> {
pub(crate) stop: &'a AtomicBool,
pub(crate) live: Option<&'a AtomicBool>,
}
#[derive(Debug)]
pub(crate) struct Gates {
remote: Gate,
local: Gate,
}
impl Gates {
pub(crate) fn of(execution: RemoteExecution) -> Option<Self> {
match execution {
RemoteExecution::Off => None,
RemoteExecution::On { jobs, local } => Some(Self {
remote: Gate::new(usize::from(jobs.get())),
local: Gate::new(local.get()),
}),
}
}
}
#[derive(Debug)]
pub(crate) struct RemoteRun<'a, R> {
pub(crate) port: &'a R,
pub(crate) policy: RemotePolicy,
pub(crate) execution: &'a Execution,
pub(crate) properties: &'a PlatformProperties,
pub(crate) down: &'a RemoteSwitch,
pub(crate) gates: Option<Gates>,
}
impl<R> RemoteRun<'_, R> {
fn executing(&self) -> Option<&Gates> {
self.gates.as_ref().filter(|_| !self.down.is_off())
}
fn consulted(&self) -> bool {
matches!(self.policy.use_, RemoteUse::Read | RemoteUse::ReadWrite) && !self.down.is_off()
}
fn uploading(&self) -> bool {
self.policy.use_ == RemoteUse::ReadWrite && !self.down.is_off()
}
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Task {
pub(crate) node: NodeId,
pub(crate) role: TargetRole,
pub(crate) key: ActionKey,
pub(crate) action: ReadyAction,
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Worked {
NotBegun(NodeId),
Performed(Box<Performed>),
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Performed {
pub(crate) node: NodeId,
pub(crate) key: ActionKey,
pub(crate) span: Span,
pub(crate) live: bool,
pub(crate) ended: Ended,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Span {
pub(crate) started: Timestamp,
pub(crate) finished: Timestamp,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Printed {
pub(crate) stdout: Digest,
pub(crate) stderr: Digest,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Kept {
pub(crate) digest: Digest,
pub(crate) manifest: OutputManifest,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Ended {
Produced {
outputs: Vec<Kept>,
printed: Printed,
},
Failed {
class: OutcomeClass,
diagnostic: Option<Diagnostic>,
printed: Option<Printed>,
},
Fetched {
outputs: Vec<Kept>,
printed: Printed,
},
Executed {
outputs: Vec<Kept>,
printed: Printed,
},
RemoteFailed(RemoteNotice),
}
pub(crate) fn failed_to(kind: InfraKind, error: &Error, printed: Option<Printed>) -> Ended {
Ended::Failed {
class: OutcomeClass::InfraFailure { kind },
diagnostic: Some(Diagnostic::new(error.to_string())),
printed,
}
}
pub(crate) const fn exited(exit: Exit, printed: Printed) -> Option<Ended> {
let class = match exit {
Exit::Exited(0) => return None,
Exit::Exited(status) => OutcomeClass::ActionFailure { status },
Exit::Signaled(signal) => OutcomeClass::Killed { signal },
Exit::TimedOut => OutcomeClass::InfraFailure {
kind: InfraKind::TimedOut,
},
};
Some(Ended::Failed {
class,
diagnostic: None,
printed: Some(printed),
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Unkept {
pub(crate) class: OutcomeClass,
pub(crate) diagnostic: Option<Diagnostic>,
}
pub(crate) const NOT_STORED: OutcomeClass = OutcomeClass::InfraFailure {
kind: InfraKind::Store,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum OutputsFrom {
Command,
Stdout(Digest),
}
impl OutputsFrom {
const fn of(task: &Task, printed: &Printed) -> Self {
match task.action.output_source {
OutputSource::Stdout => Self::Stdout(printed.stdout),
OutputSource::Command => Self::Command,
}
}
}
pub(crate) fn collected_from(
role: TargetRole,
from: &OutputsFrom,
names: &[OutputName],
found: Vec<Found>,
) -> core::result::Result<Vec<OutputManifest>, Unkept> {
if found.len() != names.len() {
return Err(Unkept {
class: NOT_STORED,
diagnostic: Some(Diagnostic::new(format!(
"the strategy answered for {} outputs of {}",
found.len(),
names.len()
))),
});
}
names
.iter()
.zip(found)
.map(|(name, found)| match from {
OutputsFrom::Command => examined(role, name, found),
OutputsFrom::Stdout(digest) => printed_as(name, *digest, &found),
})
.collect()
}
fn printed_as(
name: &OutputName,
stdout: Digest,
found: &Found,
) -> core::result::Result<OutputManifest, Unkept> {
let manifest = OutputManifest::File(StoredFile {
digest: stdout,
mode: FileMode::Plain,
});
written_at(name, found).map_or(Ok(manifest), Err)
}
pub(crate) fn written(names: &[OutputName], found: &[Found]) -> Option<Unkept> {
names
.iter()
.zip(found)
.find_map(|(name, found)| written_at(name, found))
}
fn written_at(name: &OutputName, found: &Found) -> Option<Unkept> {
(!matches!(found, Found::Nothing)).then(|| Unkept {
class: OutcomeClass::OutputWritten { name: name.clone() },
diagnostic: None,
})
}
fn examined(
role: TargetRole,
name: &OutputName,
found: Found,
) -> core::result::Result<OutputManifest, Unkept> {
match found {
Found::Nothing => Err(Unkept {
class: OutcomeClass::OutputMissing { name: name.clone() },
diagnostic: None,
}),
Found::File(file) => Ok(OutputManifest::File(file)),
Found::Tree(_) if role == TargetRole::Discovery => {
Err(refused(name, None, OutputRefusal::NotAFile))
}
Found::Tree(entries) => tree(name, entries).map(OutputManifest::Tree),
Found::Symlink => Err(refused(name, None, OutputRefusal::Symlink)),
Found::Special => Err(refused(name, None, OutputRefusal::Special)),
}
}
fn tree(name: &OutputName, mut entries: Vec<FoundEntry>) -> core::result::Result<Tree, Unkept> {
if entries.is_empty() {
return Err(refused(name, None, OutputRefusal::EmptyDirectory));
}
entries.sort();
let files = entries
.into_iter()
.map(|entry| {
let refusal = match entry.kind {
FoundKind::File(file) => {
return TreePath::parse(entry.path)
.map(|path| TreeEntry {
path,
digest: file.digest,
mode: file.mode,
})
.map_err(|error| misreported(name, &error));
}
FoundKind::EmptyDirectory => OutputRefusal::EmptyDirectory,
FoundKind::Symlink => OutputRefusal::Symlink,
FoundKind::Special => OutputRefusal::Special,
FoundKind::NotText => OutputRefusal::NotText,
};
Err(refused(name, Some(&entry.path), refusal))
})
.collect::<core::result::Result<Vec<TreeEntry>, Unkept>>()?;
Tree::new(files).map_err(|error| misreported(name, &error))
}
fn refused(name: &OutputName, path: Option<&str>, refusal: OutputRefusal) -> Unkept {
let what = path.map_or_else(|| name.to_string(), |path| format!("{name}/{path}"));
let why = match refusal {
OutputRefusal::Symlink => "is a symbolic link",
OutputRefusal::Special => "is neither a file nor a directory",
OutputRefusal::EmptyDirectory => "is an empty directory",
OutputRefusal::NotText => "has a name that is not valid Unicode",
OutputRefusal::NotAFile => "is a directory, and a discovery output must be a file",
};
Unkept {
class: OutcomeClass::OutputRefused {
name: name.clone(),
refusal,
},
diagnostic: Some(Diagnostic::new(format!("{what} {why}"))),
}
}
fn misreported(name: &OutputName, error: &Error) -> Unkept {
Unkept {
class: NOT_STORED,
diagnostic: Some(Diagnostic::new(format!(
"the strategy reported {name}: {error}"
))),
}
}
pub(crate) fn perform<S, C, K, R>(
task: Task,
strategy: &S,
cas: &C,
clock: &K,
remote: Option<&RemoteRun<'_, R>>,
shared: Shared<'_>,
tell: &(dyn Fn(Told) + Sync),
) -> Worked
where
S: ExecStrategy,
C: ContentStore,
K: Clock,
R: RemoteCache,
{
if shared.stop.load(Ordering::SeqCst) {
return Worked::NotBegun(task.node);
}
let node = task.node;
let asked = remote
.filter(|remote| remote.consulted())
.map(|remote| (remote, clock.now()));
let mut told_began = Began::NotYet;
if let Some((remote, started)) = asked {
match consult(&task, remote, cas, shared, tell) {
Consulted::Ended(ended) => {
let finished = clock.now();
return Worked::Performed(Box::new(Performed {
node,
key: task.key,
span: Span { started, finished },
live: false,
ended,
}));
}
Consulted::NotBegun => return Worked::NotBegun(node),
Consulted::Local(began) => told_began = began,
}
}
let local = remote
.and_then(|remote| remote.gates.as_ref())
.map(|gates| gates.local.enter());
if local.is_some() && shared.stop.load(Ordering::SeqCst) {
return Worked::NotBegun(node);
}
let live = shared.live.is_some_and(|place| {
place
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
.is_ok()
});
if told_began == Began::NotYet {
tell(Told::Began(node));
}
let printed: LiveSink<'_> = &|channel, bytes| {
tell(Told::Printed {
node,
channel,
bytes: bytes.to_vec(),
});
};
let started = asked.map_or_else(|| clock.now(), |(_, started)| started);
let ended = run(&task, strategy, cas, live.then_some(printed));
drop(local);
let ended = match remote.filter(|remote| remote.uploading()) {
Some(remote) => sent(&task, ended, remote, cas, tell),
None => ended,
};
let finished = clock.now();
Worked::Performed(Box::new(Performed {
node,
key: task.key,
span: Span { started, finished },
live,
ended,
}))
}
#[derive(Debug)]
enum Consulted {
Ended(Ended),
NotBegun,
Local(Began),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Began {
NotYet,
Told,
}
fn resumed(ended: Option<Ended>, began: Began) -> Consulted {
ended.map_or(Consulted::Local(began), Consulted::Ended)
}
fn consult<C: ContentStore, R: RemoteCache>(
task: &Task,
remote: &RemoteRun<'_, R>,
cas: &C,
shared: Shared<'_>,
tell: &(dyn Fn(Told) + Sync),
) -> Consulted {
let described = match remote_action(task, remote.execution, remote.properties, cas) {
Ok(described) => described,
Err(error) => {
tell(Told::Began(task.node));
return Consulted::Ended(failed_to(InfraKind::Store, &error, None));
}
};
let hit = match remote.port.lookup(&described) {
Ok(Some(hit)) => hit,
Ok(None) => return sent_to_be_run(task, remote, &described, cas, shared, tell),
Err(error) => {
return resumed(
failed_call(remote, RemoteCall::Lookup, &error, tell),
Began::NotYet,
);
}
};
if task.action.output_source == OutputSource::Stdout
&& hit.exit_code == 0
&& let Some(unkept) = unusable(&hit, task)
{
tell(Told::Began(task.node));
return refused_with_logs(&hit, unkept, RemoteCall::Download, remote, cas, tell);
}
let checked = match fetch(&hit, task, remote.port) {
Ok(checked) => checked,
Err(error) => {
return resumed(
failed_call(remote, RemoteCall::Download, &error.into_error(), tell),
Began::NotYet,
);
}
};
Consulted::Ended(match checked.keep(cas) {
Ok((outputs, printed)) => Ended::Fetched { outputs, printed },
Err(error) => failed_to(InfraKind::Store, &error, None),
})
}
fn sent_to_be_run<C: ContentStore, R: RemoteCache>(
task: &Task,
remote: &RemoteRun<'_, R>,
described: &RemoteAction,
cas: &C,
shared: Shared<'_>,
tell: &(dyn Fn(Told) + Sync),
) -> Consulted {
let Some(gates) = remote.executing() else {
return Consulted::Local(Began::NotYet);
};
let _place = gates.remote.enter();
sent_when_admitted(task, remote, described, cas, shared, tell)
}
fn sent_when_admitted<C: ContentStore, R: RemoteCache>(
task: &Task,
remote: &RemoteRun<'_, R>,
described: &RemoteAction,
cas: &C,
shared: Shared<'_>,
tell: &(dyn Fn(Told) + Sync),
) -> Consulted {
if shared.stop.load(Ordering::SeqCst) {
return Consulted::NotBegun;
}
if remote.down.is_off() {
return Consulted::Local(Began::NotYet);
}
tell(Told::Began(task.node));
match remote.port.execute(described) {
Err(error) => resumed(
failed_call(remote, RemoteCall::Execute, &error, tell),
Began::Told,
),
Ok(Executed::InputChanged(path)) => {
tell(Told::Remote(RemoteNotice::InputChanged {
label: task.action.label.clone(),
path,
}));
Consulted::Local(Began::Told)
}
Ok(Executed::TimedOut { stdout, stderr }) => {
match printed_by_remote(stdout, stderr, RemoteCall::Execute, remote, cas, tell) {
Ok(printed) => ended_by(Exit::TimedOut, printed),
Err(over) => *over,
}
}
Ok(Executed::Ran(hit)) => judged(task, remote, &hit, Answered::Ran, cas, tell),
Ok(Executed::Cached(hit)) => judged(task, remote, &hit, Answered::Cached, cas, tell),
}
}
#[derive(Clone, Copy)]
enum Answered {
Ran,
Cached,
}
fn judged<C: ContentStore, R: RemoteCache>(
task: &Task,
remote: &RemoteRun<'_, R>,
hit: &RemoteHit,
held: Answered,
cas: &C,
tell: &(dyn Fn(Told) + Sync),
) -> Consulted {
if let Some(path) = unasked(hit, &task.action.outputs) {
let diagnostic = Diagnostic::new(format!(
"the result names {path}, which the command was not asked to leave"
));
let notice = RemoteNotice::Failed {
call: RemoteCall::Execute,
diagnostic,
};
return resumed(failed_with(remote, notice, tell), Began::Told);
}
let mut refusal = if hit.exit_code == 0 {
unusable(hit, task)
} else {
None
};
if hit.exit_code == 0 && refusal.is_none() {
match fetch(hit, task, remote.port) {
Ok(checked) => {
return Consulted::Ended(match (checked.keep(cas), held) {
(Ok((outputs, printed)), Answered::Cached) => {
Ended::Fetched { outputs, printed }
}
(Ok((outputs, printed)), Answered::Ran) => Ended::Executed { outputs, printed },
(Err(error), _) => failed_to(InfraKind::Store, &error, None),
});
}
Err(Unfetched::Failed(error)) => {
return resumed(
failed_call(remote, RemoteCall::Execute, &error, tell),
Began::Told,
);
}
Err(Unfetched::Unkept(unkept)) => refusal = Some(unkept),
}
}
if let Some(unkept) = refusal {
return refused_with_logs(hit, unkept, RemoteCall::Execute, remote, cas, tell);
}
match printed_by_remote(
hit.stdout,
hit.stderr,
RemoteCall::Execute,
remote,
cas,
tell,
) {
Ok(printed) => ended_by(Exit::Exited(hit.exit_code), printed),
Err(over) => *over,
}
}
fn refused_with_logs<C: ContentStore, R: RemoteCache>(
hit: &RemoteHit,
unkept: Unkept,
call: RemoteCall,
remote: &RemoteRun<'_, R>,
cas: &C,
tell: &(dyn Fn(Told) + Sync),
) -> Consulted {
match printed_by_remote(hit.stdout, hit.stderr, call, remote, cas, tell) {
Ok(printed) => Consulted::Ended(Ended::Failed {
class: unkept.class,
diagnostic: unkept.diagnostic,
printed: Some(printed),
}),
Err(over) => *over,
}
}
fn printed_by_remote<C: ContentStore, R: RemoteCache>(
stdout: Option<Blob>,
stderr: Option<Blob>,
call: RemoteCall,
remote: &RemoteRun<'_, R>,
cas: &C,
tell: &(dyn Fn(Told) + Sync),
) -> core::result::Result<Printed, Box<Consulted>> {
let downloaded = logs(stdout, stderr, remote.port).map_err(|error| {
Box::new(resumed(
failed_call(remote, call, &error, tell),
Began::Told,
))
})?;
downloaded
.keep(cas)
.map_err(|error| Box::new(Consulted::Ended(failed_to(InfraKind::Store, &error, None))))
}
fn ended_by(exit: Exit, printed: Printed) -> Consulted {
exited(exit, printed).map_or(Consulted::Local(Began::Told), Consulted::Ended)
}
fn failed_call<R>(
remote: &RemoteRun<'_, R>,
call: RemoteCall,
error: &Error,
tell: &(dyn Fn(Told) + Sync),
) -> Option<Ended> {
let diagnostic = match error {
Error::Adapter { diagnostic, .. } => diagnostic.clone(),
other => Diagnostic::new(other.to_string()),
};
failed_with(remote, RemoteNotice::Failed { call, diagnostic }, tell)
}
fn failed_with<R>(
remote: &RemoteRun<'_, R>,
notice: RemoteNotice,
tell: &(dyn Fn(Told) + Sync),
) -> Option<Ended> {
match remote.policy.failure {
RemoteFailure::Strict => Some(Ended::RemoteFailed(notice)),
RemoteFailure::Lenient => {
if remote.down.turn_off() {
tell(Told::Remote(notice));
}
None
}
}
}
fn sent<C: ContentStore, R: RemoteCache>(
task: &Task,
ended: Ended,
remote: &RemoteRun<'_, R>,
cas: &C,
tell: &(dyn Fn(Told) + Sync),
) -> Ended {
let Ended::Produced { outputs, printed } = &ended else {
return ended;
};
let printed = *printed;
let described = remote_action(task, remote.execution, remote.properties, cas)
.and_then(|action| Ok((action, upload_of(task, outputs, &printed, cas)?)));
let (action, produced) = match described {
Ok(described) => described,
Err(error) => return failed_to(InfraKind::Store, &error, Some(printed)),
};
match upload(remote.port, &task.action.label, &action, &produced, cas) {
Sent::Stored => ended,
Sent::Declined(notice) => {
tell(Told::Remote(notice));
ended
}
Sent::Failed(notice) => failed_with(remote, notice, tell).unwrap_or(ended),
}
}
fn run<S: ExecStrategy, C: ContentStore>(
task: &Task,
strategy: &S,
cas: &C,
live: Option<LiveSink<'_>>,
) -> Ended {
let action = &task.action;
let dir = match strategy.prepare(action) {
Ok(dir) => dir,
Err(error) => return failed_to(InfraKind::Prepare, &error, None),
};
let raw = match strategy.run(&dir, action, live) {
Ok(raw) => raw,
Err(error) => return failed_to(InfraKind::Run, &error, None),
};
let printed = match stored(&raw, cas) {
Ok(printed) => printed,
Err(error) => return failed_to(InfraKind::Store, &error, None),
};
if let Some(ended) = exited(raw.exit, printed) {
return ended;
}
let found = match strategy.collect(&dir, action, cas) {
Ok(found) => found,
Err(error) => return failed_to(InfraKind::Store, &error, Some(printed)),
};
let from = OutputsFrom::of(task, &printed);
let manifests =
match collected_from(task.role, &from, &task.key.components().own.outputs, found) {
Ok(manifests) => manifests,
Err(unkept) => {
return Ended::Failed {
class: unkept.class,
diagnostic: unkept.diagnostic,
printed: Some(printed),
};
}
};
match kept(manifests, cas) {
Ok(outputs) => Ended::Produced { outputs, printed },
Err(error) => failed_to(InfraKind::Store, &error, Some(printed)),
}
}
pub(crate) fn remote_failure(notice: &RemoteNotice) -> Error {
let diagnostic = match notice {
RemoteNotice::Failed { call, diagnostic } => format!("{call}: {diagnostic}"),
RemoteNotice::InputChanged { label, path } => {
format!("{label}: {path} changed after its digest was taken")
}
};
Error::Adapter {
port: PortName::RemoteCache,
diagnostic: Diagnostic::new(diagnostic),
}
}
fn kept<C: ContentStore>(manifests: Vec<OutputManifest>, cas: &C) -> Result<Vec<Kept>> {
manifests
.into_iter()
.map(|manifest| {
let digest = cas.put(&manifest.to_bytes()?)?;
Ok(Kept { digest, manifest })
})
.collect()
}
fn stored<C: ContentStore>(raw: &RawOutcome, cas: &C) -> Result<Printed> {
Ok(Printed {
stdout: cas.put(&raw.stdout)?,
stderr: cas.put(&raw.stderr)?,
})
}
#[cfg(test)]
mod tests {
#![expect(
clippy::unwrap_used,
reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
)]
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Mutex;
use core::num::NonZeroUsize;
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use super::{
Began, Consulted, Ended, Gates, OutputsFrom, Performed, Printed, RemoteRun, Shared, Span,
Task, Told, Unkept, Worked, collected_from, consult, exited, failed_call, failed_to,
perform, remote_failure, sent_when_admitted,
};
use crate::error::{Error, PortName, Result};
use crate::execute::fixtures::{command, file, kept, key, label, output_path, outputs, tree};
use crate::execute::remote_action::remote_action;
use crate::ports::{BlobSource, Clock, ContentStore, ExecStrategy, NoRemote, RemoteCache};
use crate::types::{
Blob, DecodedTree, Diagnostic, Digest, Executed, Execution, Exit, FileMode, Found,
FoundEntry, FoundKind, InfraKind, LiveSink, NetworkAccess, NodeId, OutcomeClass,
OutputManifest, OutputName, OutputRefusal, OutputSource, PlatformProperties, RawOutcome,
ReadyAction, RemoteAction, RemoteCall, RemoteExecution, RemoteFailure, RemoteHit,
RemoteJobs, RemoteNotice, RemotePolicy, RemoteSwitch, RemoteUpload, RemoteUse, StoredFile,
TargetRole, Timestamp, Uploaded,
};
fn collected(
role: TargetRole,
names: &[OutputName],
found: Vec<Found>,
) -> core::result::Result<Vec<OutputManifest>, Unkept> {
collected_from(role, &OutputsFrom::Command, names, found)
}
fn printed() -> Printed {
Printed {
stdout: Digest::of(b"compiling\n"),
stderr: Digest::of(b""),
}
}
fn stored(content: &str, mode: FileMode) -> StoredFile {
StoredFile {
digest: Digest::of(content.as_bytes()),
mode,
}
}
fn found(path: &str, kind: FoundKind) -> FoundEntry {
FoundEntry {
path: path.to_owned(),
kind,
}
}
fn found_file(path: &str) -> FoundEntry {
found(path, FoundKind::File(stored(path, FileMode::Plain)))
}
fn not_stored(text: &str) -> Unkept {
Unkept {
class: OutcomeClass::InfraFailure {
kind: InfraKind::Store,
},
diagnostic: Some(Diagnostic::new(text)),
}
}
fn refused(name: &str, refusal: OutputRefusal, text: &str) -> Unkept {
Unkept {
class: OutcomeClass::OutputRefused {
name: outputs(&[name]).remove(0),
refusal,
},
diagnostic: Some(Diagnostic::new(text)),
}
}
fn strategy_error(text: &str) -> Error {
Error::Adapter {
port: PortName::ExecStrategy,
diagnostic: Diagnostic::new(text),
}
}
fn handed_over<T: Send + 'static>(value: T) -> T {
value
}
const fn reading(failure: RemoteFailure) -> RemotePolicy {
RemotePolicy {
use_: RemoteUse::Read,
failure,
execution: RemoteExecution::Off,
}
}
fn running<'a>(
policy: RemotePolicy,
execution: &'a Execution,
properties: &'a PlatformProperties,
down: &'a RemoteSwitch,
) -> RemoteRun<'a, NoRemote> {
RemoteRun {
port: &NoRemote,
policy,
execution,
properties,
down,
gates: Gates::of(policy.execution),
}
}
#[test]
fn a_remote_is_consulted_only_when_the_build_reads_and_it_is_not_down() {
let execution = Execution::default();
let none = PlatformProperties::default();
let up = RemoteSwitch::new();
let down = RemoteSwitch::new();
down.turn_off();
for (use_, expected) in [
(RemoteUse::Off, false),
(RemoteUse::Read, true),
(RemoteUse::ReadWrite, true),
] {
let policy = RemotePolicy {
use_,
failure: RemoteFailure::Lenient,
execution: RemoteExecution::Off,
};
assert_eq!(
running(policy, &execution, &none, &up).consulted(),
expected
);
assert!(!running(policy, &execution, &none, &down).consulted());
}
}
#[test]
fn a_remote_is_written_to_only_when_the_build_writes_and_it_is_not_down() {
let execution = Execution::default();
let none = PlatformProperties::default();
let up = RemoteSwitch::new();
let down = RemoteSwitch::new();
down.turn_off();
for (use_, expected) in [
(RemoteUse::Off, false),
(RemoteUse::Read, false),
(RemoteUse::ReadWrite, true),
] {
let policy = RemotePolicy {
use_,
failure: RemoteFailure::Lenient,
execution: RemoteExecution::Off,
};
assert_eq!(
running(policy, &execution, &none, &up).uploading(),
expected
);
assert!(!running(policy, &execution, &none, &down).uploading());
}
}
#[test]
fn a_remote_is_executed_to_only_when_the_policy_executes_and_it_is_not_down() {
let execution = Execution::default();
let none = PlatformProperties::default();
let up = RemoteSwitch::new();
let down = RemoteSwitch::new();
down.turn_off();
let executing = RemotePolicy {
execution: RemoteExecution::On {
jobs: RemoteJobs::default(),
local: NonZeroUsize::MIN,
},
..reading(RemoteFailure::Lenient)
};
assert!(
running(executing, &execution, &none, &up)
.executing()
.is_some()
);
assert!(
running(executing, &execution, &none, &down)
.executing()
.is_none()
);
let reading = reading(RemoteFailure::Lenient);
assert!(
running(reading, &execution, &none, &up)
.executing()
.is_none()
);
}
#[test]
fn a_policy_that_does_not_execute_has_no_gates_and_one_that_does_has_two() {
assert!(Gates::of(RemoteExecution::Off).is_none());
let gates = Gates::of(RemoteExecution::On {
jobs: RemoteJobs::from_integer(2).unwrap(),
local: NonZeroUsize::new(3).unwrap(),
})
.unwrap();
assert_eq!(
(gates.remote.free(), gates.local.free()),
(2, 3),
"each gate has the places its own count asks for"
);
}
fn executing() -> RemotePolicy {
RemotePolicy {
use_: RemoteUse::Read,
failure: RemoteFailure::Lenient,
execution: RemoteExecution::On {
jobs: RemoteJobs::default(),
local: NonZeroUsize::MIN,
},
}
}
fn lone_task() -> Task {
Task {
node: NodeId::new(0),
role: TargetRole::Build,
key: key(&["app"]),
action: ReadyAction {
label: label("//:app"),
command: command(&["cc"]),
description: None,
env: BTreeMap::new(),
unkeyed: BTreeSet::new(),
inputs: Vec::new(),
deps: Vec::new(),
outputs: vec![output_path("//:app", "app")],
network: NetworkAccess::Sealed,
output_source: OutputSource::Command,
},
}
}
struct Scripted<'a> {
stops: Option<&'a AtomicBool>,
executed: AtomicUsize,
}
impl<'a> Scripted<'a> {
const fn new(stops: Option<&'a AtomicBool>) -> Self {
Self {
stops,
executed: AtomicUsize::new(0),
}
}
fn executed(&self) -> usize {
self.executed.load(Ordering::SeqCst)
}
}
fn unreachable_remote(what: &str) -> Error {
Error::Adapter {
port: PortName::RemoteCache,
diagnostic: Diagnostic::new(what),
}
}
impl RemoteCache for Scripted<'_> {
fn lookup(&self, _action: &RemoteAction) -> Result<Option<RemoteHit>> {
if let Some(stop) = self.stops {
stop.store(true, Ordering::SeqCst);
}
Err(unreachable_remote("the server cannot be reached"))
}
fn download(&self, _blobs: &[Blob]) -> Result<Vec<Vec<u8>>> {
unreachable!("nothing is downloaded")
}
fn decode_tree(&self, _bytes: &[u8]) -> Result<DecodedTree> {
unreachable!("no tree is decoded")
}
fn upload(
&self,
_action: &RemoteAction,
_produced: &RemoteUpload,
_blobs: &dyn BlobSource,
) -> Result<Uploaded> {
unreachable!("nothing is uploaded")
}
fn execute(&self, _action: &RemoteAction) -> Result<Executed> {
self.executed.fetch_add(1, Ordering::SeqCst);
Err(unreachable_remote("the server cannot run it"))
}
}
#[derive(Default)]
struct Unused {
prepared: AtomicUsize,
}
impl ExecStrategy for Unused {
type Dir = ();
fn prepare(&self, _action: &ReadyAction) -> Result<Self::Dir> {
self.prepared.fetch_add(1, Ordering::SeqCst);
Err(strategy_error("not to be run"))
}
fn run(
&self,
_dir: &Self::Dir,
_action: &ReadyAction,
_live: Option<LiveSink<'_>>,
) -> Result<RawOutcome> {
unreachable!("nothing is run")
}
fn collect<C: ContentStore>(
&self,
_dir: &Self::Dir,
_action: &ReadyAction,
_cas: &C,
) -> Result<Vec<Found>> {
unreachable!("nothing is collected")
}
}
struct Always;
impl Clock for Always {
fn now(&self) -> Timestamp {
Timestamp::from_unix_nanos(1)
}
}
#[test]
fn a_build_that_stops_while_a_task_waits_at_the_local_gate_does_not_begin_it() {
let execution = Execution::default();
let none = PlatformProperties::default();
let down = RemoteSwitch::new();
let stop = AtomicBool::new(false);
let port = Scripted::new(Some(&stop));
let remote = RemoteRun {
port: &port,
policy: executing(),
execution: &execution,
properties: &none,
down: &down,
gates: Gates::of(executing().execution),
};
let strategy = Unused::default();
let tell = |_: Told| {};
let worked = perform(
lone_task(),
&strategy,
&EmptyStore,
&Always,
Some(&remote),
Shared {
stop: &stop,
live: None,
},
&tell,
);
assert!(down.is_off(), "the lookup failed first");
assert!(
matches!(worked, Worked::NotBegun(at) if at == NodeId::new(0)),
"{worked:?}"
);
assert_eq!(strategy.prepared.load(Ordering::SeqCst), 0);
}
fn admitted(stopped: bool, off: bool) -> (Consulted, usize, Vec<Told>) {
let execution = Execution::default();
let none = PlatformProperties::default();
let down = RemoteSwitch::new();
if off {
down.turn_off();
}
let stop = AtomicBool::new(stopped);
let port = Scripted::new(None);
let remote = RemoteRun {
port: &port,
policy: executing(),
execution: &execution,
properties: &none,
down: &down,
gates: Gates::of(executing().execution),
};
let task = lone_task();
let described = remote_action(&task, &execution, &none, &EmptyStore).unwrap();
let told = Mutex::new(Vec::new());
let tell = |message: Told| told.lock().unwrap().push(message);
let consulted = sent_when_admitted(
&task,
&remote,
&described,
&EmptyStore,
Shared {
stop: &stop,
live: None,
},
&tell,
);
(consulted, port.executed(), told.into_inner().unwrap())
}
#[test]
fn a_worker_admitted_to_the_remote_after_the_build_stopped_sends_nothing() {
let (consulted, executed, told) = admitted(true, false);
assert!(matches!(consulted, Consulted::NotBegun), "{consulted:?}");
assert_eq!(executed, 0);
assert_eq!(told, []);
}
#[test]
fn a_worker_admitted_to_the_remote_after_it_was_turned_off_sends_nothing_and_runs_here() {
let (consulted, executed, told) = admitted(false, true);
assert!(
matches!(consulted, Consulted::Local(Began::NotYet)),
"{consulted:?}"
);
assert_eq!(executed, 0);
assert_eq!(told, [], "not told it began: the run here does that");
}
#[test]
fn a_worker_admitted_to_a_remote_that_is_on_tells_the_build_and_sends() {
let (consulted, executed, told) = admitted(false, false);
assert!(
matches!(consulted, Consulted::Local(Began::Told)),
"the call failed, so it runs here: {consulted:?}"
);
assert_eq!(executed, 1);
assert_eq!(told[0], Told::Began(NodeId::new(0)));
}
#[test]
fn a_lenient_failure_is_told_once_by_the_worker_that_turns_the_remote_off() {
let execution = Execution::default();
let none = PlatformProperties::default();
let down = RemoteSwitch::new();
let remote = running(reading(RemoteFailure::Lenient), &execution, &none, &down);
let told = Mutex::new(Vec::new());
let tell = |message: Told| told.lock().unwrap().push(message);
let error = Error::Adapter {
port: PortName::RemoteCache,
diagnostic: Diagnostic::new("the server cannot be reached"),
};
assert_eq!(
failed_call(&remote, RemoteCall::Lookup, &error, &tell),
None
);
assert_eq!(
failed_call(&remote, RemoteCall::Lookup, &error, &tell),
None
);
assert!(down.is_off());
assert_eq!(
*told.lock().unwrap(),
[Told::Remote(RemoteNotice::Failed {
call: RemoteCall::Lookup,
diagnostic: Diagnostic::new("the server cannot be reached"),
})],
"the adapter's own message, once"
);
}
struct EmptyStore;
impl ContentStore for EmptyStore {
fn put(&self, bytes: &[u8]) -> Result<Digest> {
Ok(Digest::of(bytes))
}
fn contains(&self, _digest: &Digest) -> bool {
false
}
fn get(&self, _digest: &Digest) -> Result<Option<Vec<u8>>> {
Ok(None)
}
fn clear(&self, _at: &crate::types::OutputPath) -> Result<()> {
Ok(())
}
fn place(
&self,
_digest: &Digest,
_mode: FileMode,
_at: &crate::types::OutputPath,
) -> Result<()> {
Ok(())
}
}
#[test]
fn a_task_the_remote_cannot_be_asked_about_is_begun_before_it_fails_to_store() {
let execution = Execution::default();
let none = PlatformProperties::default();
let down = RemoteSwitch::new();
let remote = running(reading(RemoteFailure::Lenient), &execution, &none, &down);
let task = Task {
node: NodeId::new(0),
role: TargetRole::Build,
key: key(&["app"]),
action: ReadyAction {
label: label("//:app"),
command: command(&["cc"]),
description: None,
env: BTreeMap::new(),
unkeyed: BTreeSet::new(),
inputs: Vec::new(),
deps: vec![output_path("//:lib", "lib")],
outputs: vec![output_path("//:app", "app")],
network: NetworkAccess::Sealed,
output_source: OutputSource::Command,
},
};
let told = Mutex::new(Vec::new());
let tell = |message: Told| told.lock().unwrap().push(message);
let stop = AtomicBool::new(false);
let shared = Shared {
stop: &stop,
live: None,
};
let ended = consult(&task, &remote, &EmptyStore, shared, &tell);
assert!(matches!(
ended,
Consulted::Ended(Ended::Failed {
class: OutcomeClass::InfraFailure {
kind: InfraKind::Store
},
..
})
));
assert_eq!(
*told.lock().unwrap(),
[Told::Began(NodeId::new(0))],
"a failure to take the action is told after the action began, as a run's is"
);
assert!(!down.is_off(), "the remote is not at fault");
}
#[test]
fn a_strict_failure_ends_the_task_and_says_nothing_to_the_build() {
let execution = Execution::default();
let none = PlatformProperties::default();
let down = RemoteSwitch::new();
let remote = running(reading(RemoteFailure::Strict), &execution, &none, &down);
let told = Mutex::new(Vec::new());
let tell = |message: Told| told.lock().unwrap().push(message);
let ended = failed_call(&remote, RemoteCall::Download, &Error::ForeignPlan, &tell);
assert!(matches!(
ended,
Some(Ended::RemoteFailed(RemoteNotice::Failed {
call: RemoteCall::Download,
..
}))
));
assert!(told.lock().unwrap().is_empty());
assert!(!down.is_off());
}
#[test]
fn a_failure_that_fails_the_build_names_the_remote_cache_and_the_call() {
let failure = remote_failure(&RemoteNotice::Failed {
call: RemoteCall::Lookup,
diagnostic: Diagnostic::new("timed out"),
});
assert_eq!(failure.to_string(), "remote cache: lookup: timed out");
}
#[test]
fn an_exit_with_status_zero_decides_nothing_yet() {
assert_eq!(exited(Exit::Exited(0), printed()), None);
}
#[test]
fn an_exit_with_another_status_is_the_actions_failure() {
for status in [1, 2, -1, 255] {
assert_eq!(
exited(Exit::Exited(status), printed()),
Some(Ended::Failed {
class: OutcomeClass::ActionFailure { status },
diagnostic: None,
printed: Some(printed()),
})
);
}
}
#[test]
fn a_signal_is_the_actions_failure_and_carries_its_number() {
for signal in [9, 11, 15] {
assert_eq!(
exited(Exit::Signaled(signal), printed()),
Some(Ended::Failed {
class: OutcomeClass::Killed { signal },
diagnostic: None,
printed: Some(printed()),
})
);
}
}
#[test]
fn a_timeout_is_the_builders_failure_and_carries_no_message() {
assert_eq!(
exited(Exit::TimedOut, printed()),
Some(Ended::Failed {
class: OutcomeClass::InfraFailure {
kind: InfraKind::TimedOut,
},
diagnostic: None,
printed: Some(printed()),
})
);
}
#[test]
fn a_ports_failure_is_the_builders_and_its_text_is_the_message() {
let error = strategy_error("//:app: no directory to run in");
for (kind, printed) in [
(InfraKind::Prepare, None),
(InfraKind::Run, None),
(InfraKind::Store, None),
(InfraKind::Store, Some(printed())),
] {
assert_eq!(
failed_to(kind, &error, printed),
Ended::Failed {
class: OutcomeClass::InfraFailure { kind },
diagnostic: Some(Diagnostic::new(
"exec strategy: //:app: no directory to run in"
)),
printed,
}
);
}
}
#[test]
fn an_action_that_left_every_output_produced_them() {
let app = stored("app", FileMode::Executable);
let map = stored("map", FileMode::Plain);
assert_eq!(
collected(
TargetRole::Build,
&outputs(&["app", "app.map"]),
vec![Found::File(app), Found::File(map)]
),
Ok(vec![OutputManifest::File(app), OutputManifest::File(map)])
);
assert_eq!(
collected(TargetRole::Build, &[], Vec::new()),
Ok(Vec::new()),
"a target that declares no output"
);
}
#[test]
fn a_stdout_target_keeps_its_stdout_as_the_output() {
let x = Digest::of(b"x");
assert_eq!(
collected_from(
TargetRole::Build,
&OutputsFrom::Stdout(x),
&outputs(&["listing"]),
vec![Found::Nothing]
),
Ok(vec![OutputManifest::File(stored("x", FileMode::Plain))])
);
}
#[test]
fn a_stdout_target_whose_command_wrote_the_path_fails() {
let written = Unkept {
class: OutcomeClass::OutputWritten {
name: outputs(&["listing"]).remove(0),
},
diagnostic: None,
};
for found in [
Found::File(stored("y", FileMode::Plain)),
Found::Tree(vec![found_file("a")]),
Found::Symlink,
Found::Special,
] {
assert_eq!(
collected_from(
TargetRole::Build,
&OutputsFrom::Stdout(Digest::of(b"x")),
&outputs(&["listing"]),
vec![found]
),
Err(written.clone())
);
}
}
#[test]
fn a_command_target_is_unchanged() {
let missing = Unkept {
class: OutcomeClass::OutputMissing {
name: outputs(&["listing"]).remove(0),
},
diagnostic: None,
};
assert_eq!(
collected_from(
TargetRole::Build,
&OutputsFrom::Command,
&outputs(&["listing"]),
vec![Found::Nothing]
),
Err(missing.clone())
);
assert_eq!(
collected(
TargetRole::Build,
&outputs(&["listing"]),
vec![Found::Nothing]
),
Err(missing)
);
}
#[test]
fn a_directory_is_the_same_tree_whatever_order_it_was_listed_in() {
let listed = |paths: &[&str]| {
let entries = paths.iter().map(|path| found_file(path)).collect();
collected(
TargetRole::Build,
&outputs(&["site"]),
vec![Found::Tree(entries)],
)
};
let one = listed(&["index.html", "assets/app.js", "assets/app.css"]);
let other = listed(&["assets/app.css", "index.html", "assets/app.js"]);
assert_eq!(one, other);
assert_eq!(
one,
Ok(vec![tree(&[
("assets/app.css", "assets/app.css"),
("assets/app.js", "assets/app.js"),
("index.html", "index.html"),
])])
);
}
#[test]
fn a_file_and_a_directory_are_kept_side_by_side() {
let app = stored("app", FileMode::Executable);
let share = Found::Tree(vec![found_file("man/app.1")]);
assert_eq!(
collected(
TargetRole::Build,
&outputs(&["app", "share"]),
vec![Found::File(app), share]
),
Ok(vec![
OutputManifest::File(app),
tree(&[("man/app.1", "man/app.1")]),
])
);
}
#[test]
fn a_discovery_output_may_be_a_file_but_not_a_directory() {
let pkgs = stored("{}", FileMode::Plain);
assert_eq!(
collected(
TargetRole::Discovery,
&outputs(&["pkgs.json"]),
vec![Found::File(pkgs)]
),
Ok(vec![OutputManifest::File(pkgs)])
);
for entries in [vec![found_file("a.json")], Vec::new()] {
assert_eq!(
collected(
TargetRole::Discovery,
&outputs(&["pkgs.json"]),
vec![Found::Tree(entries)]
),
Err(refused(
"pkgs.json",
OutputRefusal::NotAFile,
"pkgs.json is a directory, and a discovery output must be a file"
))
);
}
}
#[test]
fn an_answer_of_the_wrong_length_is_the_builders_failure() {
let app = Found::File(stored("app", FileMode::Plain));
for (found, text) in [
(
vec![app.clone()],
"the strategy answered for 1 outputs of 2",
),
(
vec![app.clone(), Found::Nothing, app],
"the strategy answered for 3 outputs of 2",
),
(Vec::new(), "the strategy answered for 0 outputs of 2"),
] {
assert_eq!(
collected(TargetRole::Build, &outputs(&["app", "app.map"]), found),
Err(not_stored(text))
);
}
}
#[test]
fn an_output_the_action_did_not_leave_is_named() {
let names = outputs(&["app", "app.map", "app.sym"]);
let app = Found::File(stored("app", FileMode::Plain));
let missing = |name: &str| {
Err(Unkept {
class: OutcomeClass::OutputMissing {
name: outputs(&[name]).remove(0),
},
diagnostic: None,
})
};
assert_eq!(
collected(
TargetRole::Build,
&names,
vec![app.clone(), Found::Nothing, app.clone()]
),
missing("app.map"),
"one missing"
);
assert_eq!(
collected(
TargetRole::Build,
&names,
vec![app, Found::Nothing, Found::Nothing]
),
missing("app.map"),
"two missing: the first, in the order the outputs were written"
);
assert_eq!(
collected(
TargetRole::Build,
&names,
vec![Found::Nothing, Found::Nothing, Found::Nothing]
),
missing("app"),
"none left"
);
}
#[test]
fn an_output_that_is_neither_a_file_nor_a_directory_is_refused() {
for (found, refusal, text) in [
(
Found::Symlink,
OutputRefusal::Symlink,
"dist is a symbolic link",
),
(
Found::Special,
OutputRefusal::Special,
"dist is neither a file nor a directory",
),
(
Found::Tree(Vec::new()),
OutputRefusal::EmptyDirectory,
"dist is an empty directory",
),
] {
assert_eq!(
collected(TargetRole::Build, &outputs(&["dist"]), vec![found]),
Err(refused("dist", refusal, text))
);
}
}
#[test]
fn a_directory_is_refused_for_anything_in_it_that_is_not_a_regular_file() {
for (kind, refusal, text) in [
(
FoundKind::Symlink,
OutputRefusal::Symlink,
"dist/vendor/x is a symbolic link",
),
(
FoundKind::Special,
OutputRefusal::Special,
"dist/vendor/x is neither a file nor a directory",
),
(
FoundKind::EmptyDirectory,
OutputRefusal::EmptyDirectory,
"dist/vendor/x is an empty directory",
),
(
FoundKind::NotText,
OutputRefusal::NotText,
"dist/vendor/x has a name that is not valid Unicode",
),
] {
let entries = vec![found_file("index.html"), found("vendor/x", kind)];
assert_eq!(
collected(
TargetRole::Build,
&outputs(&["dist"]),
vec![Found::Tree(entries)]
),
Err(refused("dist", refusal, text))
);
}
}
#[test]
fn the_entry_a_refusal_names_does_not_depend_on_the_order_of_the_listing() {
let link = || found("m/link", FoundKind::Symlink);
let pipe = || found("b/pipe", FoundKind::Special);
let expected = Err(refused(
"dist",
OutputRefusal::Special,
"dist/b/pipe is neither a file nor a directory",
));
for entries in [
vec![link(), found_file("a"), pipe()],
vec![pipe(), link(), found_file("a")],
] {
assert_eq!(
collected(
TargetRole::Build,
&outputs(&["dist"]),
vec![Found::Tree(entries)]
),
expected,
"the lowest path that cannot be kept"
);
}
}
#[test]
fn the_first_output_that_cannot_be_kept_decides_how_the_build_ends() {
let names = outputs(&["app", "dist", "docs"]);
let app = Found::File(stored("app", FileMode::Plain));
assert_eq!(
collected(
TargetRole::Build,
&names,
vec![app.clone(), Found::Symlink, Found::Nothing]
),
Err(refused(
"dist",
OutputRefusal::Symlink,
"dist is a symbolic link"
)),
"refused before a later one is missing"
);
assert_eq!(
collected(
TargetRole::Build,
&names,
vec![app, Found::Nothing, Found::Symlink]
),
Err(Unkept {
class: OutcomeClass::OutputMissing {
name: outputs(&["dist"]).remove(0),
},
diagnostic: None,
}),
"missing before a later one is refused"
);
}
#[test]
fn a_report_that_is_not_a_tree_is_the_builders_failure() {
for (paths, text) in [
(
vec!["index.html", "../escaped"],
"the strategy reported dist: invalid tree path: \"../escaped\" — \
must not contain a '.' or '..' segment",
),
(
vec!["/etc/passwd"],
"the strategy reported dist: invalid tree path: \"/etc/passwd\" — \
must not contain an empty path segment",
),
(
vec!["a", "b", "a"],
"the strategy reported dist: not a tree: a is listed twice",
),
(
vec!["a/b", "a"],
"the strategy reported dist: not a tree: a is both a file and a directory",
),
] {
let entries = paths.iter().map(|path| found_file(path)).collect();
assert_eq!(
collected(
TargetRole::Build,
&outputs(&["dist"]),
vec![Found::Tree(entries)]
),
Err(not_stored(text))
);
}
}
#[test]
fn a_task_and_its_result_can_be_handed_to_another_thread() {
let node = NodeId::new(3);
let span = Span {
started: Timestamp::from_unix_nanos(10),
finished: Timestamp::from_unix_nanos(20),
};
let action = ReadyAction {
label: label("//:app"),
command: command(&["cc", "-o", "out/app"]),
description: None,
env: BTreeMap::new(),
unkeyed: BTreeSet::new(),
inputs: Vec::new(),
deps: Vec::new(),
outputs: vec![output_path("//:app", "app")],
network: NetworkAccess::Sealed,
output_source: OutputSource::Command,
};
let task = handed_over(Task {
node,
role: TargetRole::Build,
key: key(&["app"]),
action: action.clone(),
});
assert_eq!(task.node, node);
assert_eq!(task.key, key(&["app"]));
assert_eq!(task.action, action);
match handed_over(Worked::NotBegun(task.node)) {
Worked::NotBegun(not_begun) => assert_eq!(not_begun, node),
Worked::Performed(performed) => unreachable!("expected NotBegun, got {performed:?}"),
}
let ended = Ended::Produced {
outputs: vec![kept(file("app"))],
printed: printed(),
};
let worked = handed_over(Worked::Performed(Box::new(Performed {
node: task.node,
key: task.key,
span,
live: false,
ended: ended.clone(),
})));
match worked {
Worked::Performed(performed) => {
assert_eq!(performed.node, node);
assert_eq!(performed.key, key(&["app"]));
assert_eq!(performed.span.started, Timestamp::from_unix_nanos(10));
assert_eq!(performed.span.finished, Timestamp::from_unix_nanos(20));
assert_eq!(performed.ended, ended);
}
Worked::NotBegun(not_begun) => unreachable!("expected Performed, got {not_begun:?}"),
}
}
}