use std::io::Write;
use crate::parser::ShellCommand;
use crate::state::ShellState;
pub struct ColonBuiltin;
impl super::Builtin for ColonBuiltin {
fn name(&self) -> &'static str {
":"
}
fn names(&self) -> Vec<&'static str> {
vec![self.name()]
}
fn description(&self) -> &'static str {
"Do nothing, successfully (POSIX no-op)"
}
fn run(
&self,
_cmd: &ShellCommand,
_shell_state: &mut ShellState,
_output_writer: &mut dyn Write,
) -> i32 {
0
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::builtins::Builtin;
use crate::parser::Redirection;
#[test]
fn test_colon_builtin_no_args() {
let cmd = ShellCommand {
args: vec![":".to_string()],
redirections: Vec::new(),
compound: None,
};
let mut shell_state = ShellState::new();
let builtin = ColonBuiltin;
let mut output = Vec::new();
let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);
assert_eq!(exit_code, 0);
assert_eq!(output.len(), 0); }
#[test]
fn test_colon_builtin_with_string_args() {
let cmd = ShellCommand {
args: vec![
":".to_string(),
"arg1".to_string(),
"arg2".to_string(),
"arg3".to_string(),
],
redirections: Vec::new(),
compound: None,
};
let mut shell_state = ShellState::new();
let builtin = ColonBuiltin;
let mut output = Vec::new();
let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);
assert_eq!(exit_code, 0);
assert_eq!(output.len(), 0); }
#[test]
fn test_colon_builtin_with_special_chars() {
let cmd = ShellCommand {
args: vec![
":".to_string(),
"!@#$%^&*()".to_string(),
"spaces here".to_string(),
"tabs\there".to_string(),
],
redirections: Vec::new(),
compound: None,
};
let mut shell_state = ShellState::new();
let builtin = ColonBuiltin;
let mut output = Vec::new();
let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);
assert_eq!(exit_code, 0);
assert_eq!(output.len(), 0);
}
#[test]
fn test_colon_builtin_with_numbers() {
let cmd = ShellCommand {
args: vec![
":".to_string(),
"123".to_string(),
"456".to_string(),
"-789".to_string(),
],
redirections: Vec::new(),
compound: None,
};
let mut shell_state = ShellState::new();
let builtin = ColonBuiltin;
let mut output = Vec::new();
let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);
assert_eq!(exit_code, 0);
assert_eq!(output.len(), 0);
}
#[test]
fn test_colon_builtin_always_returns_zero() {
let builtin = ColonBuiltin;
let mut shell_state = ShellState::new();
for _ in 0..10 {
let cmd = ShellCommand {
args: vec![":".to_string()],
redirections: Vec::new(),
compound: None,
};
let mut output = Vec::new();
let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);
assert_eq!(exit_code, 0);
}
}
#[test]
fn test_colon_builtin_with_expanded_variables() {
let mut shell_state = ShellState::new();
shell_state.set_var("TEST_VAR", "test_value".to_string());
let cmd = ShellCommand {
args: vec![
":".to_string(),
"test_value".to_string(), ],
redirections: Vec::new(),
compound: None,
};
let builtin = ColonBuiltin;
let mut output = Vec::new();
let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);
assert_eq!(exit_code, 0);
assert_eq!(output.len(), 0);
}
#[test]
fn test_colon_builtin_with_redirections() {
let cmd = ShellCommand {
args: vec![":".to_string()],
redirections: vec![Redirection::Output("/tmp/test_colon_output.txt".to_string())],
compound: None,
};
let mut shell_state = ShellState::new();
let builtin = ColonBuiltin;
let mut output = Vec::new();
let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);
assert_eq!(exit_code, 0);
assert_eq!(output.len(), 0);
}
#[test]
fn test_colon_builtin_name() {
let builtin = ColonBuiltin;
assert_eq!(builtin.name(), ":");
assert_eq!(builtin.names(), vec![":"]);
}
#[test]
fn test_colon_builtin_description() {
let builtin = ColonBuiltin;
assert!(!builtin.description().is_empty());
assert!(builtin.description().contains("no-op") || builtin.description().contains("nothing"));
}
#[test]
fn test_colon_builtin_with_empty_strings() {
let cmd = ShellCommand {
args: vec![
":".to_string(),
"".to_string(),
"".to_string(),
],
redirections: Vec::new(),
compound: None,
};
let mut shell_state = ShellState::new();
let builtin = ColonBuiltin;
let mut output = Vec::new();
let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);
assert_eq!(exit_code, 0);
assert_eq!(output.len(), 0);
}
#[test]
fn test_colon_builtin_with_very_long_args() {
let long_string = "a".repeat(10000);
let cmd = ShellCommand {
args: vec![
":".to_string(),
long_string.clone(),
long_string.clone(),
],
redirections: Vec::new(),
compound: None,
};
let mut shell_state = ShellState::new();
let builtin = ColonBuiltin;
let mut output = Vec::new();
let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);
assert_eq!(exit_code, 0);
assert_eq!(output.len(), 0);
}
}