added more tests

This commit is contained in:
2025-03-19 02:35:31 -04:00
parent aa2684546e
commit b0dd101740
11 changed files with 162 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ pub mod state;
pub mod builtin;
pub mod jobs;
pub mod signal;
#[cfg(test)]
pub mod tests;
use std::collections::HashSet;

View File

@@ -30,7 +30,6 @@ fn get_hist_path() -> ShResult<PathBuf> {
} else {
let home = env::var("HOME")?;
let path = PathBuf::from(format!("{home}/.fernhist"));
flog!(DEBUG, path);
Ok(path)
}
@@ -44,7 +43,6 @@ pub fn read_line() -> ShResult<String> {
Ok(line) => {
if !line.is_empty() {
let hist_path = get_hist_path()?;
flog!(DEBUG, hist_path);
editor.add_history_entry(&line)?;
editor.save_history(&hist_path)?;
}

97
src/tests/error.rs Normal file
View File

@@ -0,0 +1,97 @@
use libsh::error::{ShErr, ShErrKind};
use super::super::*;
#[test]
fn cmd_not_found() {
let input = "foo";
let token = LexStream::new(Rc::new(input.into()), LexFlags::empty()).next().unwrap().unwrap();
let err = ShErr::full(ShErrKind::CmdNotFound("foo".into()), "", token.span);
let err_fmt = format!("{err}");
insta::assert_snapshot!(err_fmt)
}
#[test]
fn if_no_fi() {
let input = "if foo; then bar;";
let tokens = LexStream::new(Rc::new(input.into()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect::<Vec<_>>();
let node = ParseStream::new(tokens).next().unwrap();
let Err(e) = node else { panic!() };
let err_fmt = format!("{e}");
insta::assert_snapshot!(err_fmt)
}
#[test]
fn if_no_then() {
let input = "if foo; bar; fi";
let tokens = LexStream::new(Rc::new(input.into()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect::<Vec<_>>();
let node = ParseStream::new(tokens).next().unwrap();
let Err(e) = node else { panic!() };
let err_fmt = format!("{e}");
insta::assert_snapshot!(err_fmt)
}
#[test]
fn loop_no_done() {
let input = "while true; do echo foo;";
let tokens = LexStream::new(Rc::new(input.into()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect::<Vec<_>>();
let node = ParseStream::new(tokens).next().unwrap();
let Err(e) = node else { panic!() };
let err_fmt = format!("{e}");
insta::assert_snapshot!(err_fmt)
}
#[test]
fn loop_no_do() {
let input = "while true; echo foo; done";
let tokens = LexStream::new(Rc::new(input.into()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect::<Vec<_>>();
let node = ParseStream::new(tokens).next().unwrap();
let Err(e) = node else { panic!() };
let err_fmt = format!("{e}");
insta::assert_snapshot!(err_fmt)
}
#[test]
fn case_no_esac() {
let input = "case foo in foo) bar;; bar) foo;;";
let tokens = LexStream::new(Rc::new(input.into()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect::<Vec<_>>();
let node = ParseStream::new(tokens).next().unwrap();
let Err(e) = node else { panic!() };
let err_fmt = format!("{e}");
insta::assert_snapshot!(err_fmt)
}
#[test]
fn case_no_in() {
let input = "case foo foo) bar;; bar) foo;; esac";
let tokens = LexStream::new(Rc::new(input.into()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect::<Vec<_>>();
let node = ParseStream::new(tokens).next().unwrap();
let Err(e) = node else { panic!() };
let err_fmt = format!("{e}");
insta::assert_snapshot!(err_fmt)
}

View File

@@ -2,3 +2,4 @@ pub mod lexer;
pub mod parser;
pub mod expand;
pub mod term;
pub mod error;

View File

@@ -0,0 +1,9 @@
---
source: src/tests/error.rs
expression: err_fmt
---
-> [1;1] - Parse Error
 |
1 | case foo in foo) bar;; bar) foo;;
 |
- Expected 'esac' after case block

View File

@@ -0,0 +1,9 @@
---
source: src/tests/error.rs
expression: err_fmt
---
-> [1;1] - Parse Error
 |
1 | case foo foo) bar;; bar) foo;; esac
 |
- Expected 'in' after case variable name

View File

@@ -0,0 +1,9 @@
---
source: src/tests/error.rs
expression: err_fmt
---
-> [1;1] - Command not found: foo
 |
1 | foo
 |
-

View File

@@ -0,0 +1,9 @@
---
source: src/tests/error.rs
expression: err_fmt
---
-> [1;1] - Parse Error
 |
1 | if foo; then bar;
 |
- Expected 'fi' after if statement

View File

@@ -0,0 +1,9 @@
---
source: src/tests/error.rs
expression: err_fmt
---
-> [1;1] - Parse Error
 |
1 | if foo; bar; fi
 |
- Expected 'then' after 'if' condition

View File

@@ -0,0 +1,9 @@
---
source: src/tests/error.rs
expression: err_fmt
---
-> [1;1] - Parse Error
 |
1 | while true; echo foo; done
 |
- Expected 'do' after loop condition

View File

@@ -0,0 +1,9 @@
---
source: src/tests/error.rs
expression: err_fmt
---
-> [1;1] - Parse Error
 |
1 | while true; do echo foo;
 |
- Expected 'done' after loop body