Allow filesize type as a valid limit value (#11349)

# Description
This pr allow us to use `filesize` type as a valid limit value, which is
benefit for some file size based limits.

# User-Facing Changes
```console
/data/source/nushell> ulimit -f                                                                                                   
╭───┬─────────────────────────────────────────────────────┬───────────┬───────────╮
│ # │                     description                     │   soft    │   hard    │
├───┼─────────────────────────────────────────────────────┼───────────┼───────────┤
│ 0 │ Maximum size of files created by the shell (kB, -f) │ unlimited │ unlimited │
╰───┴─────────────────────────────────────────────────────┴───────────┴───────────╯
/data/source/nushell> ulimit -f 10Mib                                                                                           
/data/source/nushell> ulimit -f                                                                                                    
╭───┬─────────────────────────────────────────────────────┬───────┬───────╮
│ # │                     description                     │ soft  │ hard  │
├───┼─────────────────────────────────────────────────────┼───────┼───────┤
│ 0 │ Maximum size of files created by the shell (kB, -f) │ 10240 │ 10240 │
╰───┴─────────────────────────────────────────────────────┴───────┴───────╯
/data/source/nushell> ulimit -n                                                                                                 
╭───┬──────────────────────────────────────────────┬──────┬────────╮
│ # │                 description                  │ soft │  hard  │
├───┼──────────────────────────────────────────────┼──────┼────────┤
│ 0 │ Maximum number of open file descriptors (-n) │ 1024 │ 524288 │
╰───┴──────────────────────────────────────────────┴──────┴────────╯
/data/source/nushell> ulimit -n 10Mib                                                                                            
Error: nu:🐚:type_mismatch

  × Type mismatch.
   ╭─[entry #5:1:1]
 1 │ ulimit -n 10Mib
   ·             ─┬─
   ·              ╰── filesize is not compatible with resource RLIMIT_NOFILE
   ╰────
```

# Tests + Formatting
Make sure you've run and fixed any issues with these commands:

- [x] add `commands::ulimit::limit_set_filesize1`
- [x] add `commands::ulimit::limit_set_filesize2`
- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used`
to check that you're using the standard code style
- [x] `cargo test --workspace` to check that all tests pass (on Windows
make sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- [x] `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
This commit is contained in:
nibon7
2023-12-16 23:56:03 +08:00
committed by GitHub
parent 020e121391
commit a6da8ce769
2 changed files with 70 additions and 12 deletions

View File

@ -134,7 +134,7 @@ fn limit_set_invalid4() {
"
);
assert!(actual.err.contains("string or int required"));
assert!(actual.err.contains("string, int or filesize required"));
});
}
@ -168,3 +168,50 @@ fn limit_set_invalid5() {
assert!(actual.out.eq("unlimited"));
});
}
#[test]
fn limit_set_filesize1() {
Playground::setup("limit_set_filesize1", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
let hard = (ulimit -c | first | get hard);
match $hard {
\"unlimited\" => {
ulimit -c 1Mib;
ulimit -c
| first
| get soft
},
$x if $x >= 1024 * 1024 => {
ulimit -c 1Mib;
ulimit -c
| first
| get soft
}
_ => {
echo \"hard limit too small\"
}
}
"
));
assert!(actual.out.eq("1024") || actual.out.eq("hard limit too small"));
});
}
#[test]
fn limit_set_filesize2() {
Playground::setup("limit_set_filesize2", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(),
"
ulimit -n 10Kib
"
);
assert!(actual
.err
.contains("filesize is not compatible with resource RLIMIT_NOFILE"));
});
}