completely rewrote test suite for top level src files and all builtin files
This commit is contained in:
@@ -3,7 +3,7 @@ use nix::sys::resource::{Resource, getrlimit, setrlimit};
|
||||
use yansi::Color;
|
||||
|
||||
use crate::{
|
||||
getopt::{Opt, OptSpec, get_opts_from_tokens}, libsh::error::{ShErr, ShErrKind, ShResult, ShResultExt, next_color}, parse::{NdRule, Node, execute::prepare_argv}, prelude::*, state::{self}
|
||||
getopt::{Opt, OptSpec, get_opts_from_tokens, get_opts_from_tokens_strict}, libsh::error::{ShErr, ShErrKind, ShResult, ShResultExt, next_color}, parse::{NdRule, Node, execute::prepare_argv}, prelude::*, state::{self}
|
||||
};
|
||||
|
||||
fn ulimit_opt_spec() -> [OptSpec;5] {
|
||||
@@ -100,7 +100,7 @@ pub fn ulimit(node: Node) -> ShResult<()> {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
let (_, opts) = get_opts_from_tokens(argv, &ulimit_opt_spec()).promote_err(span.clone())?;
|
||||
let (_, opts) = get_opts_from_tokens_strict(argv, &ulimit_opt_spec()).promote_err(span.clone())?;
|
||||
let ulimit_opts = get_ulimit_opts(&opts).promote_err(span.clone())?;
|
||||
|
||||
if let Some(fds) = ulimit_opts.fds {
|
||||
@@ -167,3 +167,99 @@ pub fn ulimit(node: Node) -> ShResult<()> {
|
||||
state::set_status(0);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::get_ulimit_opts;
|
||||
use crate::getopt::Opt;
|
||||
use crate::state;
|
||||
use crate::testutil::{TestGuard, test_input};
|
||||
use nix::sys::resource::{Resource, getrlimit};
|
||||
|
||||
// ===================== Pure: option parsing =====================
|
||||
|
||||
#[test]
|
||||
fn parse_fds() {
|
||||
let opts = get_ulimit_opts(&[Opt::ShortWithArg('n', "1024".into())]).unwrap();
|
||||
assert_eq!(opts.fds, Some(1024));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_procs() {
|
||||
let opts = get_ulimit_opts(&[Opt::ShortWithArg('u', "512".into())]).unwrap();
|
||||
assert_eq!(opts.procs, Some(512));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_stack() {
|
||||
let opts = get_ulimit_opts(&[Opt::ShortWithArg('s', "8192".into())]).unwrap();
|
||||
assert_eq!(opts.stack, Some(8192));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_core() {
|
||||
let opts = get_ulimit_opts(&[Opt::ShortWithArg('c', "0".into())]).unwrap();
|
||||
assert_eq!(opts.core, Some(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_vmem() {
|
||||
let opts = get_ulimit_opts(&[Opt::ShortWithArg('v', "100000".into())]).unwrap();
|
||||
assert_eq!(opts.vmem, Some(100000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_multiple() {
|
||||
let opts = get_ulimit_opts(&[
|
||||
Opt::ShortWithArg('n', "256".into()),
|
||||
Opt::ShortWithArg('c', "0".into()),
|
||||
]).unwrap();
|
||||
assert_eq!(opts.fds, Some(256));
|
||||
assert_eq!(opts.core, Some(0));
|
||||
assert!(opts.procs.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_non_numeric_fails() {
|
||||
let result = get_ulimit_opts(&[Opt::ShortWithArg('n', "abc".into())]);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_invalid_option() {
|
||||
let result = get_ulimit_opts(&[Opt::Short('z')]);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
// ===================== Integration =====================
|
||||
|
||||
#[test]
|
||||
fn ulimit_set_core_zero() {
|
||||
let _g = TestGuard::new();
|
||||
// Setting core dump size to 0 is always safe
|
||||
test_input("ulimit -c 0").unwrap();
|
||||
let (soft, _) = getrlimit(Resource::RLIMIT_CORE).unwrap();
|
||||
assert_eq!(soft, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ulimit_invalid_flag() {
|
||||
let _g = TestGuard::new();
|
||||
let result = test_input("ulimit -z 100");
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ulimit_non_numeric_value() {
|
||||
let _g = TestGuard::new();
|
||||
let result = test_input("ulimit -n abc");
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ulimit_status_zero() {
|
||||
let _g = TestGuard::new();
|
||||
test_input("ulimit -c 0").unwrap();
|
||||
assert_eq!(state::get_status(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user