fish-shell/src/tests/mod.rs

117 lines
3.2 KiB
Rust
Raw Normal View History

2023-12-22 19:27:01 +08:00
mod abbrs;
mod common;
mod complete;
2023-12-09 23:48:02 +08:00
mod debounce;
2023-12-03 01:20:40 +08:00
mod editable_line;
2023-12-10 04:20:38 +08:00
mod encoding;
mod env;
mod env_universal_common;
mod expand;
mod fd_monitor;
mod highlight;
mod history;
mod input;
mod input_common;
mod key;
2023-12-02 23:26:45 +08:00
mod pager;
2023-12-10 03:47:43 +08:00
mod parse_util;
mod parser;
2023-12-22 19:27:01 +08:00
mod reader;
mod redirection;
2023-11-26 02:40:31 +08:00
mod screen;
mod std;
mod string_escape;
2024-03-24 00:22:52 +08:00
mod termsize;
2023-12-10 17:38:06 +08:00
mod threads;
2023-09-17 19:42:24 +08:00
mod tokenizer;
mod topic_monitor;
2023-12-10 00:10:36 +08:00
mod wgetopt;
pub mod prelude {
use crate::common::ScopeGuarding;
use crate::env::{env_init, misc_init};
use crate::parser::{CancelBehavior, Parser};
use crate::reader::reader_init;
use crate::signal::signal_reset_handlers;
pub use crate::tests::env::{PwdEnvironment, TestEnvironment};
use crate::topic_monitor::topic_monitor_init;
use crate::wutil::wgetcwd;
use crate::{env::EnvStack, proc::proc_init};
use once_cell::sync::OnceCell;
use std::cell::RefCell;
use std::env::set_current_dir;
use std::ffi::CString;
use std::rc::Rc;
/// A wrapper around a Parser with some test helpers.
pub struct TestParser {
parser: Parser,
pushed_dirs: RefCell<Vec<String>>,
}
impl TestParser {
pub fn new() -> TestParser {
TestParser {
parser: Parser::new(Rc::new(EnvStack::new()), CancelBehavior::default()),
pushed_dirs: RefCell::new(Vec::new()),
}
}
/// Helper to chdir and then update $PWD.
pub fn pushd(&self, path: &str) {
let cwd = wgetcwd();
self.pushed_dirs.borrow_mut().push(cwd.to_string());
// We might need to create the directory. We don't care if this fails due to the directory
// already being present.
std::fs::create_dir_all(path).unwrap();
std::env::set_current_dir(path).unwrap();
self.parser.vars().set_pwd_from_getcwd();
}
pub fn popd(&self) {
let old_cwd = self.pushed_dirs.borrow_mut().pop().unwrap();
std::env::set_current_dir(old_cwd).unwrap();
self.parser.vars().set_pwd_from_getcwd();
}
}
impl std::ops::Deref for TestParser {
type Target = Parser;
fn deref(&self) -> &Self::Target {
&self.parser
}
}
pub fn test_init() -> impl ScopeGuarding<Target = ()> {
static DONE: OnceCell<()> = OnceCell::new();
DONE.get_or_init(|| {
// If we are building with `cargo build` and have build w/ `cmake`, FISH_BUILD_DIR might
// not yet exist.
std::fs::create_dir_all(env!("FISH_BUILD_DIR")).unwrap();
set_current_dir(env!("FISH_BUILD_DIR")).unwrap();
{
let s = CString::new("").unwrap();
unsafe {
libc::setlocale(libc::LC_ALL, s.as_ptr());
}
}
topic_monitor_init();
crate::threads::init();
proc_init();
env_init(None, true, false);
misc_init();
// Set default signal handlers, so we can ctrl-C out of this.
signal_reset_handlers();
// Set PWD from getcwd - fixes #5599
EnvStack::globals().set_pwd_from_getcwd();
});
reader_init()
}
pub use serial_test::serial;
}