type Cmd = Vec<String>;
pub(crate) fn commands(script: &str) -> Vec<Cmd> {
let mut out = Vec::new();
collect(script, 0, &mut out);
out
}
fn collect(script: &str, depth: usize, out: &mut Vec<Cmd>) {
for cmd in split(script) {
if depth < 4 {
if let Some(inner) = shell_c(&cmd) {
collect(&inner, depth + 1, out);
}
}
out.push(cmd);
}
}
fn shell_c(cmd: &Cmd) -> Option<String> {
let (_, rest) = strip(cmd)?;
let (head, args) = rest.split_first()?;
if !matches!(base(head), "bash" | "sh" | "zsh" | "dash") {
return None;
}
let i = args
.iter()
.position(|a| a.starts_with('-') && !a.starts_with("--") && a.ends_with('c'))?;
args.get(i + 1).cloned()
}
struct Words {
words: Vec<(String, bool)>,
cur: String,
in_word: bool,
quoted: bool,
out: Vec<Cmd>,
}
impl Words {
fn end_word(&mut self) {
if self.in_word {
self.words
.push((std::mem::take(&mut self.cur), self.quoted));
}
self.in_word = false;
self.quoted = false;
}
fn end_command(&mut self) {
self.end_word();
let words = std::mem::take(&mut self.words);
let mut cmd = Vec::new();
let mut it = words.into_iter();
while let Some((w, quoted)) = it.next() {
if !quoted && is_redirect(&w) {
if w.trim_start_matches(|c: char| c.is_ascii_digit()).len() <= 2
&& !w.contains('&')
&& w.trim_start_matches(|c: char| c.is_ascii_digit())
.chars()
.all(|c| c == '<' || c == '>')
{
it.next();
}
continue;
}
cmd.push(w);
}
if !cmd.is_empty() {
self.out.push(cmd);
}
}
}
fn is_redirect(w: &str) -> bool {
let t = w.trim_start_matches(|c: char| c.is_ascii_digit());
t.starts_with('<') || t.starts_with('>')
}
fn split(script: &str) -> Vec<Cmd> {
let chars: Vec<char> = script.chars().collect();
let mut w = Words {
words: vec![],
cur: String::new(),
in_word: false,
quoted: false,
out: vec![],
};
let mut heredocs: Vec<(String, bool)> = Vec::new();
let mut i = 0;
while i < chars.len() {
let c = chars[i];
match c {
'\\' => {
match chars.get(i + 1) {
Some('\n') => {}
Some(&n) => {
w.cur.push(n);
w.in_word = true;
}
None => {}
}
i += 2;
continue;
}
'\'' => {
w.in_word = true;
w.quoted = true;
i += 1;
while i < chars.len() && chars[i] != '\'' {
w.cur.push(chars[i]);
i += 1;
}
i += 1;
continue;
}
'"' => {
w.in_word = true;
w.quoted = true;
i += 1;
while i < chars.len() && chars[i] != '"' {
if chars[i] == '\\' && i + 1 < chars.len() {
i += 1;
}
w.cur.push(chars[i]);
i += 1;
}
i += 1;
continue;
}
'#' if !w.in_word => {
while i < chars.len() && chars[i] != '\n' {
i += 1;
}
continue;
}
'\n' => {
w.end_command();
i += 1;
for (delim, strip_tabs) in std::mem::take(&mut heredocs) {
while i < chars.len() {
let end = chars[i..]
.iter()
.position(|&c| c == '\n')
.map_or(chars.len(), |p| i + p);
let line: String = chars[i..end].iter().collect();
i = (end + 1).min(chars.len());
let line = if strip_tabs {
line.trim_start_matches('\t')
} else {
line.as_str()
};
if line.trim_end() == delim {
break;
}
}
}
continue;
}
'<' if chars.get(i + 1) == Some(&'<') && chars.get(i + 2) != Some(&'<') => {
let mut j = i + 2;
let strip_tabs = chars.get(j) == Some(&'-');
if strip_tabs {
j += 1;
}
while chars.get(j).is_some_and(|c| *c == ' ' || *c == '\t') {
j += 1;
}
let mut delim = String::new();
while let Some(&d) = chars.get(j) {
if d.is_whitespace() || matches!(d, ';' | '&' | '|' | '(' | ')') {
break;
}
if !matches!(d, '\'' | '"' | '\\') {
delim.push(d);
}
j += 1;
}
if !delim.is_empty() {
heredocs.push((delim, strip_tabs));
}
w.end_word();
i = j;
continue;
}
';' | '(' | ')' | '`' => {
w.end_command();
}
'|' => {
w.end_command();
if matches!(chars.get(i + 1), Some('|') | Some('&')) {
i += 1;
}
}
'&' => {
let redirect =
(w.in_word && w.cur.ends_with(['>', '<'])) || chars.get(i + 1) == Some(&'>');
if redirect {
w.cur.push('&');
w.in_word = true;
} else {
w.end_command();
if chars.get(i + 1) == Some(&'&') {
i += 1;
}
}
}
c if c.is_whitespace() => w.end_word(),
c => {
w.cur.push(c);
w.in_word = true;
}
}
i += 1;
}
w.end_command();
w.out
}
fn base(word: &str) -> &str {
word.rsplit('/').next().unwrap_or(word)
}
fn is_assignment(w: &str) -> bool {
match w.split_once('=') {
Some((k, _)) => {
!k.is_empty()
&& !k.starts_with(|c: char| c.is_ascii_digit())
&& k.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}
None => false,
}
}
const KEYWORDS: &[&str] = &[
"if", "then", "else", "elif", "do", "while", "until", "!", "{", "}", "time",
];
fn strip(cmd: &[String]) -> Option<(bool, &[String])> {
let mut sudo = false;
let mut i = 0;
loop {
let w = cmd.get(i)?.as_str();
if KEYWORDS.contains(&w) || is_assignment(w) {
i += 1;
continue;
}
match base(w) {
"sudo" => {
sudo = true;
i += 1;
while let Some(a) = cmd.get(i) {
if matches!(
a.as_str(),
"-u" | "-g" | "-C" | "-p" | "-h" | "-D" | "-r" | "-t" | "-U"
) {
i += 2;
} else if a.starts_with('-') || is_assignment(a) {
i += 1;
} else {
break;
}
}
}
"env" => {
i += 1;
while let Some(a) = cmd.get(i) {
if matches!(a.as_str(), "-u" | "-C" | "-S") {
i += 2;
} else if a.starts_with('-') || is_assignment(a) {
i += 1;
} else {
break;
}
}
}
"command" => {
i += 1;
while let Some(a) = cmd.get(i) {
match a.as_str() {
"-v" | "-V" => return None,
"-p" | "--" => i += 1,
_ => break,
}
}
}
"exec" | "nohup" | "builtin" | "setsid" | "xargs" | "stdbuf" => {
i += 1;
while cmd.get(i).is_some_and(|a| a.starts_with('-')) {
i += 1;
}
}
"nice" => {
i += 1;
while let Some(a) = cmd.get(i) {
if a == "-n" {
i += 2;
} else if a.starts_with('-') {
i += 1;
} else {
break;
}
}
}
"timeout" => {
i += 1;
while cmd.get(i).is_some_and(|a| a.starts_with('-')) {
i += 1;
}
i += 1; }
"npx" | "bunx" => i = skip_runner_options(cmd, i + 1),
tool @ ("npm" | "pnpm" | "yarn" | "bun") if runner_sub(cmd, i, tool).is_some() => {
i = skip_runner_options(cmd, runner_sub(cmd, i, tool).unwrap_or(i + 1));
}
_ => return Some((sudo, &cmd[i..])),
}
}
}
fn runner_sub(cmd: &[String], i: usize, tool: &str) -> Option<usize> {
let subs: &[&str] = match tool {
"npm" => &["exec", "x"],
"pnpm" | "yarn" => &["dlx", "exec"],
"bun" => &["x"],
_ => return None,
};
let mut j = i + 1;
loop {
let a = cmd.get(j)?.as_str();
if a.starts_with('-') {
j += if matches!(
a,
"--filter" | "-F" | "--prefix" | "-C" | "--cwd" | "--dir" | "--workspace" | "-w"
) {
2
} else {
1
};
} else {
return subs.contains(&a).then_some(j + 1);
}
}
}
fn skip_runner_options(cmd: &[String], mut j: usize) -> usize {
while let Some(a) = cmd.get(j).map(String::as_str) {
if !a.starts_with('-') {
break;
}
j += if matches!(
a,
"-p" | "--package" | "--registry" | "--cache" | "--node-options" | "-c"
) {
2
} else {
1
};
if a == "--" {
break;
}
}
j
}
fn positionals<'a>(args: &'a [String], valued: &[&str]) -> Vec<&'a str> {
let mut out = Vec::new();
let mut i = 0;
while i < args.len() {
let a = args[i].as_str();
if valued.contains(&a) {
i += 2;
continue;
}
if !a.starts_with('-') || a == "-" {
out.push(a);
}
i += 1;
}
out
}
fn has_flag(args: &[String], names: &[&str]) -> bool {
args.iter().any(|a| {
names
.iter()
.any(|n| a == n || a.starts_with(&format!("{n}=")))
})
}
fn short_n(args: &[String]) -> bool {
args.iter()
.any(|a| a.starts_with('-') && !a.starts_with("--") && a.contains('n'))
}
const AWS_VALUED: &[&str] = &[
"--profile",
"--region",
"--endpoint-url",
"--output",
"--query",
"--cli-read-timeout",
"--cli-connect-timeout",
"--ca-bundle",
"--color",
];
fn aws_remote(args: &[String]) -> bool {
let pos = positionals(args, AWS_VALUED);
let (Some(service), op) = (pos.first().copied(), pos.get(1).copied().unwrap_or("")) else {
return false;
};
if has_flag(args, &["--dryrun", "--dry-run"]) {
return false;
}
match service {
"s3" => match op {
"cp" | "sync" => {
pos.get(3).is_some_and(|d| d.starts_with("s3://"))
}
"mv" | "rm" | "mb" | "rb" => true,
_ => false,
},
"deploy" => op != "help",
"configure" | "help" | "sts" => false,
_ => {
const READ: &[&str] = &[
"describe-",
"get-",
"list-",
"head-",
"batch-get-",
"lookup-",
"search-",
"scan",
"query",
"filter-",
"validate-",
"estimate-",
"check-",
"preview-",
"select-",
];
if op.is_empty()
|| op == "help"
|| READ.iter().any(|p| op.starts_with(p))
|| args.iter().any(|a| a == "--version")
{
return false;
}
const WRITE: &[&str] = &[
"create",
"delete",
"update",
"put",
"start",
"stop",
"run",
"invoke",
"terminate",
"modify",
"attach",
"detach",
"publish",
"send",
"tag",
"untag",
"register",
"deregister",
"upload",
"deploy",
"remove",
"add",
"set",
"reboot",
"restore",
"release",
"associate",
"disassociate",
"enable",
"disable",
"execute",
"import",
"copy",
"purge",
"cancel",
"reset",
"rotate",
"revoke",
"authorize",
"grant",
"apply",
"replace",
"change",
"write",
"post",
"commit",
"promote",
"rollback",
];
WRITE.iter().any(|p| op.starts_with(p))
}
}
}
fn gh_remote(args: &[String]) -> bool {
let pos = positionals(args, &["-R", "--repo", "--hostname"]);
let (Some(group), sub) = (pos.first().copied(), pos.get(1).copied().unwrap_or("")) else {
return false;
};
match group {
"api" => {
let method = args
.iter()
.enumerate()
.find_map(|(i, a)| {
if a == "-X" || a == "--method" {
args.get(i + 1).cloned()
} else if let Some(m) = a.strip_prefix("--method=") {
Some(m.to_string())
} else {
a.strip_prefix("-X")
.filter(|m| !m.is_empty())
.map(String::from)
}
})
.map(|m| m.to_ascii_uppercase());
match method {
Some(m) => matches!(m.as_str(), "POST" | "PATCH" | "PUT" | "DELETE"),
None => has_flag(args, &["-f", "-F", "--field", "--raw-field", "--input"]),
}
}
"pr" => matches!(
sub,
"create" | "merge" | "comment" | "edit" | "close" | "reopen" | "review" | "ready"
),
"issue" => matches!(
sub,
"create" | "comment" | "edit" | "close" | "reopen" | "delete" | "transfer" | "lock"
),
"release" => matches!(
sub,
"create" | "upload" | "edit" | "delete" | "delete-asset"
),
"workflow" => matches!(sub, "run" | "enable" | "disable"),
"run" => matches!(sub, "rerun" | "cancel" | "delete"),
"repo" => matches!(
sub,
"create" | "delete" | "edit" | "fork" | "rename" | "archive" | "unarchive"
),
"secret" | "variable" => matches!(sub, "set" | "delete" | "remove"),
"label" => matches!(sub, "create" | "edit" | "delete" | "clone"),
"gist" => matches!(sub, "create" | "edit" | "delete" | "rename"),
_ => false,
}
}
fn kubectl_remote(args: &[String]) -> bool {
if has_flag(args, &["--dry-run"]) {
return false;
}
let pos = positionals(
args,
&[
"-n",
"--namespace",
"--context",
"--kubeconfig",
"-s",
"--server",
],
);
match pos.first().copied() {
Some("rollout") => !matches!(pos.get(1).copied(), Some("status" | "history")),
Some(
"apply" | "delete" | "create" | "replace" | "patch" | "scale" | "set" | "annotate"
| "label" | "edit" | "drain" | "cordon" | "uncordon" | "taint" | "run" | "expose"
| "autoscale" | "cp" | "exec",
) => true,
_ => false,
}
}
fn git_remote(args: &[String]) -> bool {
let pos = positionals(args, &["-C", "-c", "--git-dir", "--work-tree"]);
if pos.first().copied() != Some("push") {
return false;
}
let after: &[String] = match args.iter().position(|a| a == "push") {
Some(i) => &args[i + 1..],
None => &[],
};
!(has_flag(after, &["--dry-run"]) || short_n(after))
}
const DOCKER_VALUED: &[&str] = &[
"--context",
"-c",
"-H",
"--host",
"--config",
"-l",
"--log-level",
"-f",
"--file",
"-p",
"--project-name",
"--profile",
"--env-file",
"--project-directory",
];
fn docker_remote(head: &str, args: &[String]) -> bool {
let pos = positionals(args, DOCKER_VALUED);
let (first, second) = (
pos.first().copied().unwrap_or(""),
pos.get(1).copied().unwrap_or(""),
);
let pushes = has_flag(args, &["--push"])
|| args
.iter()
.any(|a| a.contains("type=registry") || a.contains("push=true"));
let (first, second) = if base(head) == "docker-compose" {
("compose", first)
} else {
(first, second)
};
match first {
"push" | "login" => true,
"image" | "manifest" => second == "push",
"compose" => second == "push",
"buildx" => {
matches!(second, "build" | "bake" | "b") && pushes
|| (second == "imagetools" && pos.get(2).copied() == Some("create"))
}
"build" | "bake" => pushes,
_ => false,
}
}
const CURL_SHORT_VALUED: &str = "oHuAebcDEKwxyYzUrCmQ";
fn write_method(m: &str) -> bool {
matches!(
m.to_ascii_uppercase().as_str(),
"POST" | "PUT" | "PATCH" | "DELETE"
)
}
fn curl_remote(args: &[String]) -> bool {
let get = has_flag(args, &["-G", "--get"]);
let mut body = false;
let mut i = 0;
while i < args.len() {
let a = args[i].as_str();
if let Some(long) = a.strip_prefix("--") {
let (name, inline) = match long.split_once('=') {
Some((n, v)) => (n, Some(v)),
None => (long, None),
};
match name {
"request" => {
let v = inline.or_else(|| args.get(i + 1).map(String::as_str));
if v.is_some_and(write_method) {
return true;
}
}
"data" | "data-raw" | "data-binary" | "data-urlencode" | "data-ascii" | "form"
| "form-string" | "upload-file" | "json" => body = true,
_ => {}
}
} else if a.starts_with('-') && a.len() > 1 {
let cluster: Vec<char> = a[1..].chars().collect();
let mut k = 0;
while k < cluster.len() {
match cluster[k] {
'X' => {
let rest: String = cluster[k + 1..].iter().collect();
let v = if rest.is_empty() {
args.get(i + 1).cloned().unwrap_or_default()
} else {
rest
};
if write_method(&v) {
return true;
}
break;
}
'd' | 'F' | 'T' => {
body = true;
break;
}
c if CURL_SHORT_VALUED.contains(c) => break,
_ => {}
}
k += 1;
}
}
i += 1;
}
body && !get
}
fn wget_remote(args: &[String]) -> bool {
has_flag(
args,
&["--post-data", "--post-file", "--body-data", "--body-file"],
) || args.iter().enumerate().any(|(i, a)| {
let v = if a == "--method" {
args.get(i + 1).map(String::as_str)
} else {
a.strip_prefix("--method=")
};
v.is_some_and(write_method)
})
}
fn is_remote(sudo_less: &[String]) -> bool {
let Some((head, args)) = sudo_less.split_first() else {
return false;
};
let pos = positionals(args, &[]);
let first = pos.first().copied().unwrap_or("");
let second = pos.get(1).copied().unwrap_or("");
let dry = has_flag(args, &["--dry-run", "--dryrun", "-n"]);
match base(head) {
"git" => git_remote(args),
"gh" => gh_remote(args),
"aws" => aws_remote(args),
"kubectl" | "oc" => kubectl_remote(args),
"npm" | "pnpm" | "yarn" | "bun" => {
args.iter()
.any(|a| matches!(a.as_str(), "publish" | "unpublish" | "deprecate"))
&& !dry
}
"cargo" => first == "publish" && !dry,
"dart" | "flutter" => first == "pub" && second == "publish" && !dry,
"twine" => first == "upload",
"docker" | "docker-compose" | "podman" | "buildah" => docker_remote(head, args),
"curl" => curl_remote(args),
"wget" => wget_remote(args),
"helm" => matches!(first, "install" | "upgrade" | "uninstall" | "rollback") && !dry,
"terraform" | "tofu" => matches!(first, "apply" | "destroy"),
"serverless" | "sls" => matches!(first, "deploy" | "remove"),
"firebase" => first == "deploy",
"wrangler" => {
(matches!(
first,
"deploy" | "publish" | "secret" | "delete" | "rollback"
) || (first == "pages" && matches!(second, "deploy" | "publish")))
&& !has_flag(args, &["--dry-run"])
}
"vercel" => {
if has_flag(args, &["--version", "-v", "--help", "-h"]) {
return false;
}
first.is_empty()
|| first.starts_with(['.', '/', '~'])
|| matches!(
first,
"deploy" | "promote" | "rollback" | "remove" | "rm" | "alias"
)
}
"netlify" => first == "deploy",
"gcloud" => pos.iter().any(|w| {
matches!(
*w,
"deploy"
| "create"
| "delete"
| "update"
| "submit"
| "set"
| "add"
| "remove"
| "cp"
| "rsync"
| "mv"
| "rm"
| "import"
| "export"
)
}),
_ => false,
}
}
pub(crate) fn remote_effect(script: &str) -> bool {
commands(script)
.iter()
.any(|c| strip(c).is_some_and(|(_, rest)| is_remote(rest)))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Install {
No,
Present,
Needed,
}
const SUDO_MUTATES: &[&str] = &[
"apt",
"apt-get",
"aptitude",
"yum",
"dnf",
"apk",
"snap",
"brew",
"choco",
"pacman",
"zypper",
"dpkg",
"rpm",
"pip",
"pip3",
"npm",
"gem",
"cargo",
"make",
"cp",
"mv",
"install",
"ln",
"tee",
"rm",
"chmod",
"chown",
"mkdir",
"systemctl",
"service",
"sysctl",
"ufw",
"tar",
"curl",
"wget",
"sh",
"bash",
"useradd",
"usermod",
"mount",
"umount",
"update-alternatives",
];
fn pkg_installs(head: &str, args: &[String]) -> bool {
let first = positionals(args, &[]).first().copied().unwrap_or("");
match head {
"apt" | "apt-get" | "aptitude" | "yum" | "dnf" | "zypper" => matches!(
first,
"install"
| "update"
| "upgrade"
| "dist-upgrade"
| "full-upgrade"
| "remove"
| "purge"
| "autoremove"
| "reinstall"
| "localinstall"
| "groupinstall"
| "downgrade"
),
"apk" => matches!(first, "add" | "del" | "upgrade" | "update"),
"snap" | "choco" => matches!(first, "install" | "upgrade" | "remove" | "refresh"),
"pacman" => args
.iter()
.any(|a| a.starts_with("-S") || a.starts_with("-R")),
"dpkg" | "rpm" => args
.iter()
.any(|a| matches!(a.as_str(), "-i" | "-U" | "-e" | "-ivh")),
_ => false,
}
}
pub(crate) fn global_install(script: &str, have: &dyn Fn(&str) -> bool) -> Install {
let mut found = Install::No;
for c in commands(script) {
let Some((sudo, rest)) = strip(&c) else {
continue;
};
let Some((head, args)) = rest.split_first() else {
continue;
};
let head = base(head);
if head == "brew" {
let first = positionals(args, &[]).first().copied().unwrap_or("");
match first {
"install" | "reinstall" => {
let names: Vec<&str> = positionals(args, &[]).into_iter().skip(1).collect();
let cask = has_flag(args, &["--cask", "--casks"]);
let present = !cask
&& !names.is_empty()
&& names
.iter()
.all(|n| have(n.rsplit('/').next().unwrap_or(n)))
&& first == "install";
if present {
found = Install::Present;
} else {
return Install::Needed;
}
}
"upgrade" | "tap" | "untap" | "uninstall" | "update" => return Install::Needed,
_ => {}
}
} else if pkg_installs(head, args) || (sudo && SUDO_MUTATES.contains(&head)) {
return Install::Needed;
}
}
found
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn false_positives_are_not_remote_effects() {
for s in [
"git push --dry-run origin HEAD",
"git push -n origin HEAD",
"echo using aws sdk",
"grep -q x vercel.json",
"kubectl kustomize .",
"echo gh pr list",
"sudo -n true",
"# git push origin main\necho hi",
"cat <<EOF\ngit push origin main\nEOF\necho done",
"git diff --exit-code",
"npm run lint",
"echo pushing",
"npm publish --dry-run",
"gh pr list --state open",
"gh pr view 1",
"gh api repos/x/y",
"gh api -X GET repos/x/y",
"aws --version",
"aws sts get-caller-identity",
"aws ec2 describe-instances --region us-east-1",
"aws s3 ls s3://b",
"aws s3 cp s3://b/f .",
"aws s3 sync s3://b/dir out",
"kubectl get pods",
"kubectl rollout status deploy/x",
"kubectl apply -f x.yaml --dry-run=client",
"vercel --version",
"vercel build",
"command -v kubectl",
"FOO=1 echo vercel",
"git status && git log -1",
"git push -n",
"aws s3 ls",
"curl https://example.test",
"curl -I https://example.test",
"curl -sSL -o out.tgz https://example.test/x.tgz",
"curl -G -d q=1 https://example.test",
"curl -X GET https://example.test",
"wget https://example.test/x",
"wget -q -O - https://example.test",
"npx --yes prettier --check .",
"pnpm exec eslint .",
"pnpm dlx create-thing",
"yarn dlx tsc",
"npm exec -- tsc -p .",
"npm --prefix a run build",
"pnpm --filter pkg test",
"yarn workspace x build",
"docker build -t x .",
"docker image ls",
"docker compose up -d",
"docker buildx build -t x .",
"wrangler deploy --dry-run",
"vercel pull --yes",
"vercel ls",
"npx publish-check",
] {
assert!(!remote_effect(s), "{s:?}");
}
}
#[test]
fn remote_effects_are_found() {
for s in [
"git push origin main",
"git -C sub push",
"npm publish",
"pnpm publish --access public",
"gh pr merge 3 --squash",
"gh pr create --fill",
"gh pr comment 3 -b hi",
"gh release create v1",
"gh api -X POST repos/x/y/issues",
"gh api --method=DELETE repos/x/y",
"gh api repos/x/y -f a=b",
"aws s3 sync dist s3://bucket",
"aws s3 cp a.txt s3://bucket/a.txt",
"aws s3 rm s3://bucket/a --recursive",
"aws deploy create-deployment --application-name a",
"aws cloudformation deploy --template-file t.yml --stack-name s",
"aws lambda update-function-code --function-name f",
"kubectl apply -f x.yaml",
"kubectl delete pod x",
"kubectl rollout restart deploy/x",
"vercel --prod",
"vercel deploy",
"netlify deploy --prod",
"echo hi\ncd out && git push origin HEAD:main",
"if true; then git push; fi",
"sudo git push",
"env GIT_TERMINAL_PROMPT=0 git push origin x",
"bash -c 'git push origin main'",
"terraform apply -auto-approve",
"cargo publish",
"docker push img:1",
"true | gh pr merge 1",
"npx wrangler deploy",
"npx --yes vercel --prod",
"bunx netlify deploy --prod",
"pnpm dlx wrangler pages deploy dist",
"pnpm exec npm publish",
"yarn dlx vercel deploy",
"yarn exec -- npm publish",
"npm exec -y -- wrangler deploy",
"npm exec --package=x -- git push",
"yarn npm publish",
"yarn workspace x publish",
"pnpm --filter pkg publish",
"pnpm -r publish --no-git-checks",
"npm --prefix a publish",
"bun publish",
"docker image push img:1",
"docker buildx build --push -t x .",
"docker buildx bake --push",
"docker compose push",
"docker compose -f ci.yml push",
"docker-compose push",
"docker manifest push img:1",
"docker buildx imagetools create -t a b",
"wrangler deploy",
"wrangler pages deploy dist",
"vercel",
"vercel ./app",
"netlify deploy",
"curl -X POST https://example.test",
"curl -XPUT https://example.test",
"curl --request DELETE https://example.test",
"curl --request=patch https://example.test",
"curl -d a=b https://example.test",
"curl --data-binary @f https://example.test",
"curl -sSd a=b https://example.test",
"curl -F f=@x https://example.test",
"curl --form f=@x https://example.test",
"curl -T f https://example.test/f",
"curl --upload-file f https://example.test/f",
"curl --json '{}' https://example.test",
"wget --post-data=a=b https://example.test",
"wget --post-file f https://example.test",
"wget --method=PUT https://example.test",
"wget --method DELETE https://example.test",
] {
assert!(remote_effect(s), "{s:?}");
}
}
fn none(_: &str) -> bool {
false
}
#[test]
fn global_installs() {
for s in [
"sudo apt-get install -y jq",
"sudo apt-get update && echo x",
"apt-get install jq",
"apt install jq",
"brew install jq",
"brew upgrade",
"yum install -y x",
"sudo make install",
"sudo cp a /usr/local/bin/a",
"sudo pip install x",
"apk add curl",
] {
assert_eq!(global_install(s, &none), Install::Needed, "{s:?}");
}
for s in [
"npm ci",
"echo sudo",
"echo brew install x",
"sudo -n true",
"sudo -v",
"apt list --installed",
"apt-cache policy jq",
"command -v brew",
"# sudo apt-get install x",
] {
assert_eq!(global_install(s, &none), Install::No, "{s:?}");
}
}
#[test]
fn brew_install_of_what_is_there_runs() {
let have = |c: &str| c == "jq" || c == "yq";
assert_eq!(global_install("brew install jq", &have), Install::Present);
assert_eq!(
global_install("brew install jq yq", &have),
Install::Present
);
assert_eq!(
global_install("brew install --quiet mikefarah/yq/yq", &have),
Install::Present
);
assert_eq!(
global_install("brew install jq ripgrep", &have),
Install::Needed
);
assert_eq!(
global_install("brew install --cask jq", &have),
Install::Needed
);
assert_eq!(global_install("brew install", &have), Install::Needed);
assert_eq!(
global_install("brew install jq\nsudo apt-get install x", &have),
Install::Needed
);
}
#[test]
fn heredoc_bodies_and_continuations() {
let s = "cat > f <<-'EOF'\n\tgit push\n\tEOF\ngit \\\n push origin x";
assert!(remote_effect(s));
let s = "cat > f <<EOF\ngit push\naws s3 rm x\nEOF\n";
assert!(!remote_effect(s));
}
}