use core::fmt;
use core::num::NonZeroUsize;
use std::ffi::OsString;
use buildl::buildl_core::{
Error, RemoteExecution, RemoteFailure, RemoteFlagConflict, RemotePolicy, RemoteSettings,
RemoteUse, RemoteVariable,
};
use crate::cli::RemoteArgs;
#[derive(Debug, Clone, Copy)]
pub(crate) enum RemoteTable<'a> {
Named(&'a RemoteSettings),
Absent,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Switch {
Off,
On,
Invalid(String),
}
impl Switch {
fn of(value: Option<&OsString>) -> Self {
match value.map(|value| value.to_str()) {
None | Some(Some("0")) => Self::Off,
Some(Some("1")) => Self::On,
Some(_) => Self::Invalid(
value
.map(|value| value.to_string_lossy().into_owned())
.unwrap_or_default(),
),
}
}
}
impl fmt::Display for Switch {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Off => f.write_str("off"),
Self::On => f.write_str("on"),
Self::Invalid(value) => write!(f, "invalid ({value:?} is neither 1 nor 0)"),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct RemoteSwitches {
pub upload: Option<OsString>,
pub strict: Option<OsString>,
pub execute: Option<OsString>,
}
impl RemoteSwitches {
pub(crate) fn upload_switch(&self) -> Switch {
Switch::of(self.upload.as_ref())
}
pub(crate) fn strict_switch(&self) -> Switch {
Switch::of(self.strict.as_ref())
}
pub(crate) fn execute_switch(&self) -> Option<Switch> {
self.execute.as_ref().map(|value| Switch::of(Some(value)))
}
pub(crate) fn resolve(
&self,
args: &RemoteArgs,
table: RemoteTable<'_>,
local: NonZeroUsize,
) -> Result<RemotePolicy, Error> {
let refuse = |conflict| Err(Error::RemoteFlags { conflict });
if args.no_remote {
return if args.upload || args.strict {
refuse(RemoteFlagConflict::NoRemoteWithSwitch)
} else if args.remote_execute {
refuse(RemoteFlagConflict::NoRemoteWithExecute)
} else if args.remote_jobs.is_some() {
refuse(RemoteFlagConflict::JobsWithoutExecution)
} else {
Ok(RemotePolicy::OFF)
};
}
if args.remote_execute && args.no_remote_execute {
return refuse(RemoteFlagConflict::ExecuteBothWays);
}
let upload = args.upload || switched_on(RemoteVariable::Upload, self.upload.as_ref())?;
let strict = args.strict || switched_on(RemoteVariable::Strict, self.strict.as_ref())?;
let RemoteTable::Named(settings) = table else {
return if args.upload || args.strict {
refuse(RemoteFlagConflict::SwitchWithoutRemote)
} else if args.remote_execute || args.no_remote_execute || args.remote_jobs.is_some() {
refuse(RemoteFlagConflict::ExecutionWithoutRemote)
} else {
Ok(RemotePolicy::OFF)
};
};
let executing = if args.remote_execute {
true
} else if args.no_remote_execute {
false
} else if self.execute.is_some() {
switched_on(RemoteVariable::Execute, self.execute.as_ref())?
} else {
settings.execute()
};
let execution = if executing {
RemoteExecution::On {
jobs: args.remote_jobs.unwrap_or_else(|| settings.jobs()),
local,
}
} else if args.remote_jobs.is_some() {
return refuse(RemoteFlagConflict::JobsWithoutExecution);
} else {
RemoteExecution::Off
};
Ok(RemotePolicy {
use_: if upload {
RemoteUse::ReadWrite
} else {
RemoteUse::Read
},
failure: if strict {
RemoteFailure::Strict
} else {
RemoteFailure::Lenient
},
execution,
})
}
}
fn switched_on(name: RemoteVariable, value: Option<&OsString>) -> Result<bool, Error> {
match Switch::of(value) {
Switch::Off => Ok(false),
Switch::On => Ok(true),
Switch::Invalid(value) => Err(Error::RemoteSwitch { name, value }),
}
}
#[cfg(test)]
mod tests {
#![expect(
clippy::unwrap_used,
reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
)]
use core::num::NonZeroUsize;
use std::ffi::OsString;
use buildl::buildl_core::{
Error, RemoteExecution, RemoteFailure, RemoteFlagConflict, RemoteJobs, RemotePolicy,
RemoteSettings, RemoteUrl, RemoteUse, RemoteVariable,
};
use super::{RemoteSwitches, RemoteTable, Switch};
use crate::cli::RemoteArgs;
fn machine(upload: Option<&str>, strict: Option<&str>) -> RemoteSwitches {
RemoteSwitches {
upload: upload.map(Into::into),
strict: strict.map(Into::into),
execute: None,
}
}
fn executing(execute: &str) -> RemoteSwitches {
RemoteSwitches {
execute: Some(execute.into()),
..machine(None, None)
}
}
const fn flags(upload: bool, strict: bool, no_remote: bool) -> RemoteArgs {
RemoteArgs {
upload,
strict,
no_remote,
remote_execute: false,
no_remote_execute: false,
remote_jobs: None,
}
}
const fn plain() -> RemoteArgs {
flags(false, false, false)
}
fn jobs(count: i64) -> RemoteJobs {
RemoteJobs::from_integer(count).unwrap()
}
fn local() -> NonZeroUsize {
NonZeroUsize::new(4).unwrap()
}
fn table(execute: bool, jobs: Option<RemoteJobs>) -> RemoteSettings {
let builder =
RemoteSettings::builder(RemoteUrl::parse("grpcs://cache").unwrap()).execute(execute);
match jobs {
Some(jobs) => builder.jobs(jobs),
None => builder,
}
.build()
.unwrap()
}
fn decide(
machine: &RemoteSwitches,
args: &RemoteArgs,
remote: Option<&RemoteSettings>,
) -> Result<RemotePolicy, Error> {
machine.resolve(
args,
remote.map_or(RemoteTable::Absent, RemoteTable::Named),
local(),
)
}
fn in_workspace(machine: &RemoteSwitches, args: &RemoteArgs) -> Result<RemotePolicy, Error> {
decide(machine, args, Some(&table(false, None)))
}
fn policy(use_: RemoteUse, failure: RemoteFailure) -> RemotePolicy {
RemotePolicy {
use_,
failure,
execution: RemoteExecution::Off,
}
}
fn executes(jobs: RemoteJobs) -> RemotePolicy {
RemotePolicy {
execution: RemoteExecution::On {
jobs,
local: local(),
},
..policy(RemoteUse::Read, RemoteFailure::Lenient)
}
}
fn assert_refused(result: Result<RemotePolicy, Error>, expected: RemoteFlagConflict) {
let refused = result.unwrap_err();
assert!(
matches!(&refused, Error::RemoteFlags { conflict } if *conflict == expected),
"{refused}"
);
}
#[test]
fn no_switch_reads_leniently() {
let resolved = in_workspace(&machine(None, None), &plain()).unwrap();
assert_eq!(resolved, policy(RemoteUse::Read, RemoteFailure::Lenient));
}
#[test]
fn the_upload_variable_turns_upload_on() {
let resolved = in_workspace(&machine(Some("1"), None), &plain()).unwrap();
assert_eq!(
resolved,
policy(RemoteUse::ReadWrite, RemoteFailure::Lenient)
);
}
#[test]
fn the_strict_variable_turns_strict_on() {
let resolved = in_workspace(&machine(None, Some("1")), &plain()).unwrap();
assert_eq!(resolved, policy(RemoteUse::Read, RemoteFailure::Strict));
}
#[test]
fn the_upload_flag_overrides_a_zero_variable() {
let resolved = in_workspace(&machine(Some("0"), None), &flags(true, false, false)).unwrap();
assert_eq!(
resolved,
policy(RemoteUse::ReadWrite, RemoteFailure::Lenient)
);
}
#[test]
fn no_remote_overrides_the_variables() {
let resolved = in_workspace(
&RemoteSwitches {
execute: Some("1".into()),
..machine(Some("1"), Some("1"))
},
&flags(false, false, true),
)
.unwrap();
assert_eq!(resolved, RemotePolicy::OFF);
}
#[test]
fn no_remote_with_an_upload_flag_is_refused() {
for args in [flags(true, false, true), flags(false, true, true)] {
assert_refused(
in_workspace(&machine(None, None), &args),
RemoteFlagConflict::NoRemoteWithSwitch,
);
}
}
#[test]
fn no_remote_never_reads_the_variables() {
let resolved = in_workspace(
&RemoteSwitches {
execute: Some("yes".into()),
..machine(Some("yes"), Some("yes"))
},
&flags(false, false, true),
)
.unwrap();
assert_eq!(resolved, RemotePolicy::OFF);
}
#[test]
fn a_flag_settles_its_switch_without_reading_its_variable() {
let upload = in_workspace(&machine(Some("yes"), None), &flags(true, false, false)).unwrap();
let strict = in_workspace(&machine(None, Some("yes")), &flags(false, true, false)).unwrap();
assert_eq!(upload, policy(RemoteUse::ReadWrite, RemoteFailure::Lenient));
assert_eq!(strict, policy(RemoteUse::Read, RemoteFailure::Strict));
}
#[test]
fn the_other_switchs_bad_variable_is_still_refused_beside_a_flag() {
let refused =
in_workspace(&machine(None, Some("yes")), &flags(true, false, false)).unwrap_err();
assert!(
matches!(
refused,
Error::RemoteSwitch {
name: RemoteVariable::Strict,
..
}
),
"{refused}"
);
}
#[test]
fn a_flag_without_a_remote_table_is_refused() {
for args in [flags(true, false, false), flags(false, true, false)] {
assert_refused(
decide(&machine(None, None), &args, None),
RemoteFlagConflict::SwitchWithoutRemote,
);
}
}
#[test]
fn variables_without_a_remote_table_leave_the_remote_off() {
let resolved = decide(&machine(Some("1"), Some("1")), &plain(), None).unwrap();
assert_eq!(resolved, RemotePolicy::OFF);
}
#[test]
fn a_variable_reads_as_on_off_or_invalid() {
let on = machine(Some("1"), None);
let off = machine(Some("0"), None);
let unset = machine(None, None);
let bad = machine(Some("yes"), Some(""));
assert_eq!(on.upload_switch(), Switch::On);
assert_eq!(off.upload_switch(), Switch::Off);
assert_eq!(unset.upload_switch(), Switch::Off);
assert_eq!(unset.strict_switch(), Switch::Off);
assert_eq!(bad.upload_switch(), Switch::Invalid("yes".to_owned()));
assert_eq!(bad.strict_switch(), Switch::Invalid(String::new()));
}
#[test]
fn the_execute_variable_reads_as_unset_off_on_or_invalid() {
let read = |value: Option<&str>| RemoteSwitches {
execute: value.map(OsString::from),
..RemoteSwitches::default()
};
assert_eq!(read(None).execute_switch(), None);
assert_eq!(read(Some("0")).execute_switch(), Some(Switch::Off));
assert_eq!(read(Some("1")).execute_switch(), Some(Switch::On));
assert_eq!(
read(Some("yes")).execute_switch(),
Some(Switch::Invalid("yes".to_owned()))
);
}
#[test]
fn a_switch_is_worded_for_doctor() {
assert_eq!(Switch::On.to_string(), "on");
assert_eq!(Switch::Off.to_string(), "off");
assert_eq!(
Switch::Invalid("yes".to_owned()).to_string(),
"invalid (\"yes\" is neither 1 nor 0)"
);
}
#[test]
fn a_variable_other_than_one_or_zero_is_refused_naming_it() {
let refused = in_workspace(&machine(Some("yes"), None), &plain()).unwrap_err();
assert!(
matches!(
&refused,
Error::RemoteSwitch { name: RemoteVariable::Upload, value }
if value == "yes"
),
"{refused}"
);
let refused = in_workspace(&machine(None, Some("")), &plain()).unwrap_err();
assert!(
matches!(
&refused,
Error::RemoteSwitch {
name: RemoteVariable::Strict,
..
}
),
"{refused}"
);
}
#[test]
fn the_manifest_turns_execution_on() {
let resolved = decide(
&machine(None, None),
&plain(),
Some(&table(true, Some(jobs(32)))),
)
.unwrap();
assert_eq!(resolved, executes(jobs(32)));
}
#[test]
fn the_manifest_default_is_off() {
let resolved = decide(&machine(None, None), &plain(), Some(&table(false, None))).unwrap();
assert_eq!(resolved, policy(RemoteUse::Read, RemoteFailure::Lenient));
}
#[test]
fn the_variable_overrides_the_manifest() {
let off = decide(&executing("0"), &plain(), Some(&table(true, None))).unwrap();
let on = decide(&executing("1"), &plain(), Some(&table(false, None))).unwrap();
assert_eq!(off, policy(RemoteUse::Read, RemoteFailure::Lenient));
assert_eq!(on, executes(RemoteJobs::default()));
}
#[test]
fn the_flag_overrides_the_variable() {
let args = RemoteArgs {
remote_execute: true,
..plain()
};
let resolved = decide(&executing("0"), &args, Some(&table(false, None))).unwrap();
assert_eq!(resolved, executes(RemoteJobs::default()));
let args = RemoteArgs {
no_remote_execute: true,
..plain()
};
let resolved = decide(&executing("1"), &args, Some(&table(true, None))).unwrap();
assert_eq!(resolved, policy(RemoteUse::Read, RemoteFailure::Lenient));
}
#[test]
fn the_execute_variable_is_not_read_when_a_flag_decides() {
let args = RemoteArgs {
remote_execute: true,
..plain()
};
let resolved = decide(&executing("yes"), &args, Some(&table(false, None))).unwrap();
assert_eq!(resolved, executes(RemoteJobs::default()));
let args = RemoteArgs {
no_remote_execute: true,
..plain()
};
let resolved = decide(&executing("yes"), &args, Some(&table(true, None))).unwrap();
assert_eq!(resolved, policy(RemoteUse::Read, RemoteFailure::Lenient));
}
#[test]
fn no_remote_execute_keeps_the_cache() {
let args = RemoteArgs {
no_remote_execute: true,
..flags(true, false, false)
};
let resolved = decide(&machine(None, None), &args, Some(&table(true, None))).unwrap();
assert_eq!(
resolved,
policy(RemoteUse::ReadWrite, RemoteFailure::Lenient)
);
}
#[test]
fn no_remote_turns_execution_off() {
let resolved = decide(
&executing("1"),
&flags(false, false, true),
Some(&table(true, None)),
)
.unwrap();
assert_eq!(resolved, RemotePolicy::OFF);
let args = RemoteArgs {
no_remote_execute: true,
..flags(false, false, true)
};
let resolved = decide_executing(&args);
assert_eq!(resolved, RemotePolicy::OFF);
}
fn decide_executing(args: &RemoteArgs) -> RemotePolicy {
decide(&machine(None, None), args, Some(&table(true, None))).unwrap()
}
#[test]
fn remote_execute_with_no_remote_is_refused() {
let args = RemoteArgs {
remote_execute: true,
..flags(false, false, true)
};
assert_refused(
decide(&machine(None, None), &args, Some(&table(false, None))),
RemoteFlagConflict::NoRemoteWithExecute,
);
}
#[test]
fn remote_execute_with_no_remote_execute_is_refused() {
let args = RemoteArgs {
remote_execute: true,
no_remote_execute: true,
..plain()
};
assert_refused(
decide(&machine(None, None), &args, Some(&table(false, None))),
RemoteFlagConflict::ExecuteBothWays,
);
}
#[test]
fn an_execution_flag_without_a_remote_table_is_refused() {
for args in [
RemoteArgs {
remote_execute: true,
..plain()
},
RemoteArgs {
no_remote_execute: true,
..plain()
},
RemoteArgs {
remote_jobs: Some(jobs(8)),
..plain()
},
] {
assert_refused(
decide(&machine(None, None), &args, None),
RemoteFlagConflict::ExecutionWithoutRemote,
);
}
}
#[test]
fn the_variable_without_a_remote_table_is_not_read() {
let resolved = decide(&executing("yes"), &plain(), None).unwrap();
assert_eq!(resolved, RemotePolicy::OFF);
}
#[test]
fn remote_jobs_without_execution_is_refused() {
let args = RemoteArgs {
remote_jobs: Some(jobs(8)),
..plain()
};
assert_refused(
decide(&machine(None, None), &args, Some(&table(false, None))),
RemoteFlagConflict::JobsWithoutExecution,
);
assert_refused(
decide(&executing("0"), &args, Some(&table(true, None))),
RemoteFlagConflict::JobsWithoutExecution,
);
let no_execute = RemoteArgs {
no_remote_execute: true,
..args
};
assert_refused(
decide(&machine(None, None), &no_execute, Some(&table(true, None))),
RemoteFlagConflict::JobsWithoutExecution,
);
let no_remote = RemoteArgs {
no_remote: true,
..args
};
assert_refused(
decide(&machine(None, None), &no_remote, Some(&table(true, None))),
RemoteFlagConflict::JobsWithoutExecution,
);
}
#[test]
fn remote_jobs_overrides_the_manifest() {
let args = RemoteArgs {
remote_jobs: Some(jobs(8)),
..plain()
};
let resolved = decide(
&machine(None, None),
&args,
Some(&table(true, Some(jobs(64)))),
)
.unwrap();
assert_eq!(resolved, executes(jobs(8)));
}
#[test]
fn jobs_default_to_the_manifest() {
let manifest = decide(
&machine(None, None),
&plain(),
Some(&table(true, Some(jobs(64)))),
)
.unwrap();
assert_eq!(manifest, executes(jobs(64)));
let default = decide_executing(&plain());
assert_eq!(default, executes(RemoteJobs::default()));
}
#[test]
fn a_bad_execute_variable_is_refused_naming_it() {
let refused = decide(&executing("yes"), &plain(), Some(&table(false, None))).unwrap_err();
assert!(
matches!(
&refused,
Error::RemoteSwitch { name: RemoteVariable::Execute, value } if value == "yes"
),
"{refused}"
);
assert!(
refused.to_string().contains("BUILDL_REMOTE_EXECUTE"),
"{refused}"
);
}
}