#[cfg(windows)]
#[allow(dead_code)]
const SYS_CURL: &str = "C:\\Windows\\System32\\curl.exe";
#[cfg(not(windows))]
#[allow(dead_code)]
const SYS_CURL: &str = "curl";
#[allow(dead_code)]
fn _helix_allowed(var: &str, what: &str) -> bool {
let ok = std::env::var(var).map(|v| v == "1").unwrap_or(false);
if !ok {
eprintln!("helix: BLOCKED {} -- permission denied (set {}=1 to allow; `hxc run` allows by default, a `hxc build`/`export-rust` binary does not)", what, var);
}
ok
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_exec(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_exec(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
c
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_net(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_net(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
c
}
use std::{
io::Write,
process::{Command, Stdio},
};
fn run(input: &str) -> (bool, String) {
let mut c = Command::new(env!("CARGO_BIN_EXE_instinct"))
.args(["check", "run", "--input", "-"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
c.stdin.take().unwrap().write_all(input.as_bytes()).unwrap();
let out = c.wait_with_output().unwrap();
(out.status.success(), String::from_utf8(out.stdout).unwrap())
}
#[test]
fn implication_truth_table_preserves_unknown() {
for premise in [Some(true), Some(false), None] {
for conclusion in [Some(true), Some(false), None] {
let mut facts = vec!["\"truth\":true".to_owned()];
if let Some(p) = premise {
facts.push(format!("\"p\":{p}"));
}
if let Some(q) = conclusion {
facts.push(format!("\"q\":{q}"));
}
let input = format!(
r#"{{"check":{{"schema":"instinct.check/v1","id":"implication","revision":1,"inputs":{{"truth":"bool","p":"bool","q":"bool"}},"claim":{{"op":"implies","if":{{"op":"eq","left":{{"fact":"p"}},"right":{{"fact":"truth"}}}},"then":{{"op":"eq","left":{{"fact":"q"}},"right":{{"fact":"truth"}}}}}}}},"facts":{{{}}}}}"#,
facts.join(",")
);
let expected = if premise == Some(false) || conclusion == Some(true) {
"pass"
} else if premise == Some(true) && conclusion == Some(false) {
"fail"
} else {
"abstain"
};
let (ok, out) = run(&input);
assert!(ok, "{out}");
assert!(
out.contains(&format!("\"verdict\":\"{expected}\"")),
"{premise:?} {conclusion:?}: {out}"
);
}
}
}
#[test]
fn false_premise_does_not_hide_invalid_conclusion() {
let input = r#"{"check":{"schema":"instinct.check/v1","id":"bad","revision":1,"inputs":{"p":"bool","q":"bool"},"claim":{"op":"implies","if":{"op":"eq","left":{"fact":"p"},"right":{"fact":"q"}},"then":{"op":"shell","command":"do not execute"}}},"facts":{"p":true,"q":false}}"#;
let (ok, out) = run(input);
assert!(!ok);
assert!(out.contains("unknown_operator"), "{out}");
}