Skip to content

Instantly share code, notes, and snippets.

@Phate6660
Created September 26, 2021 01:58
Show Gist options
  • Save Phate6660/172a32169edcf5bb13559e4766468f96 to your computer and use it in GitHub Desktop.
Save Phate6660/172a32169edcf5bb13559e4766468f96 to your computer and use it in GitHub Desktop.
A WIP for incorporating testing into crusty, as well as an example echo check function
#[test]
fn echo_check() {
use crate::{ShellState, process_input};
let mut shell_state = ShellState::init();
let success = process_input(&mut shell_state, "echo test".to_string());
assert!(success, "echo test failed!");
}
diff --git a/src/main.rs b/src/main.rs
index a698608..f816aac 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,6 @@
mod builtins;
mod shared_functions;
+mod test;
#[cfg(feature = "readline")]
use rustyline::{error::ReadlineError, Editor};
@@ -10,12 +11,12 @@ use std::process::exit;
use shared_functions::parse_input;
// Process the input to run the appropriate builtin or external command.
-fn process_input(shell_state: &mut ShellState, input: String) {
+fn process_input(shell_state: &mut ShellState, input: String) -> bool {
if input.is_empty() {
- return;
+ return false;
}
let command = ShellCommand::new(input);
- ShellCommand::run(shell_state, command);
+ ShellCommand::run(shell_state, command)
}
#[cfg(feature = "readline")]
diff --git a/src/shared_functions.rs b/src/shared_functions.rs
index a79b280..ea1c6e4 100644
--- a/src/shared_functions.rs
+++ b/src/shared_functions.rs
@@ -63,19 +63,24 @@ impl ShellCommand {
args: split_input_string[1..].to_vec(),
}
}
- pub fn run(shell_state: &mut ShellState, command: ShellCommand) {
+ pub fn run(shell_state: &mut ShellState, command: ShellCommand) -> bool {
match command.name.as_str() {
- "calc" => println!("{}", calc(command.args)),
- "cd" => cd(shell_state, command),
- "echo" => println!("{}", echo(command.args)),
- "help" => help(),
- "ls" => print!("{}", ls(command.args)),
- "pwd" => println!("{}", std::env::current_dir().unwrap().display()),
+ "calc" => { println!("{}", calc(command.args)); return true; },
+ "cd" => { cd(shell_state, command); return true; },
+ "echo" => { println!("{}", echo(command.args)); return true; },
+ "help" => { help(); return true; },
+ "ls" => { print!("{}", ls(command.args)); return true; },
+ "pwd" => {
+ println!("{}", std::env::current_dir().unwrap().display());
+ return true;
+ },
_ => {
if command.args.contains(&String::from("|")) {
piped_cmd(PipedShellCommand::from(command));
+ return true;
} else {
cmd(command);
+ return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment