use async_trait::async_trait;
use clap::{CommandFactory, Parser};
use crate::ast::Value;
use crate::interpreter::ExecResult;
use crate::tools::{schema_from_clap, validate_against_schema, ExecContext, ToolCtx, GlobalFlags, Tool, ToolArgs, ToolSchema};
use crate::validator::ValidationIssue;
pub struct Unset;
#[derive(Parser, Debug)]
#[command(name = "unset", about = "Remove variables from scope")]
struct UnsetArgs {
#[command(flatten)]
global: GlobalFlags,
names: Vec<String>,
}
#[async_trait]
impl Tool for Unset {
fn name(&self) -> &str {
"unset"
}
fn schema(&self) -> ToolSchema {
schema_from_clap(
&UnsetArgs::command(),
"unset",
"Remove variables from scope",
[
("Remove a variable", "unset MY_VAR"),
("Remove multiple", "unset A B C"),
],
)
}
fn validate(&self, args: &ToolArgs) -> Vec<ValidationIssue> {
let mut issues = validate_against_schema(args, &self.schema());
issues.extend(
args.positional
.iter()
.filter_map(|v| match v {
Value::String(s) => Some(s.as_str()),
_ => None,
})
.filter_map(super::mixed_script_issue),
);
issues
}
async fn execute(&self, args: ToolArgs, ctx: &mut dyn ToolCtx) -> ExecResult {
let Some(ctx) = ctx.as_any_mut().downcast_mut::<ExecContext>() else {
return ExecResult::failure(1, "internal error: kernel builtin requires ExecContext");
};
let argv = match args.to_argv() {
Ok(v) => v,
Err(e) => return ExecResult::failure(2, format!("unset: {e}")),
};
let parsed = match UnsetArgs::try_parse_from(
std::iter::once("unset".to_string()).chain(argv),
) {
Ok(p) => p,
Err(e) => return ExecResult::failure(2, format!("unset: {e}")),
};
parsed.global.apply(ctx);
if args.positional.is_empty() {
return ExecResult::failure(1, "unset: missing variable name");
}
for arg in &args.positional {
let name = match arg {
Value::String(s) => s.clone(),
other => match crate::interpreter::value_to_text_sink_named(other, "a variable name") {
Ok(s) => s,
Err(e) => return ExecResult::failure(1, format!("unset: {e}")),
},
};
if let Err(bad) = crate::name::validate(&name) {
return ExecResult::failure(1, format!("unset: `{name}': {bad}"));
}
ctx.scope.remove(&name);
}
ExecResult::success("")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::vfs::{MemoryFs, VfsRouter};
use std::sync::Arc;
fn make_ctx() -> ExecContext {
let mut vfs = VfsRouter::new();
vfs.mount("/", MemoryFs::new());
ExecContext::new(Arc::new(vfs))
}
#[tokio::test]
async fn test_unset_existing_variable() {
let mut ctx = make_ctx();
ctx.scope.set("X", Value::Int(42));
assert!(ctx.scope.get("X").is_some());
let mut args = ToolArgs::new();
args.positional.push(Value::String("X".into()));
let result = Unset.execute(args, &mut ctx).await;
assert!(result.ok());
assert_eq!(&*result.text_out(), "", "POSIX unset is silent");
assert!(ctx.scope.get("X").is_none());
}
#[tokio::test]
async fn test_unset_nonexistent_variable() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::String("MISSING".into()));
let result = Unset.execute(args, &mut ctx).await;
assert!(result.ok(), "unsetting a missing variable is not an error");
assert_eq!(&*result.text_out(), "", "POSIX unset is silent");
}
#[tokio::test]
async fn test_unset_multiple_variables() {
let mut ctx = make_ctx();
ctx.scope.set("A", Value::Int(1));
ctx.scope.set("B", Value::Int(2));
ctx.scope.set("C", Value::Int(3));
let mut args = ToolArgs::new();
args.positional.push(Value::String("A".into()));
args.positional.push(Value::String("B".into()));
args.positional.push(Value::String("D".into()));
let result = Unset.execute(args, &mut ctx).await;
assert!(result.ok());
assert_eq!(&*result.text_out(), "", "POSIX unset is silent");
assert!(ctx.scope.get("A").is_none());
assert!(ctx.scope.get("B").is_none());
assert!(ctx.scope.get("C").is_some());
}
#[tokio::test]
async fn test_unset_no_args() {
let mut ctx = make_ctx();
let args = ToolArgs::new();
let result = Unset.execute(args, &mut ctx).await;
assert!(!result.ok());
}
}