Add just_pid function (#1833)

This commit is contained in:
Swordelf2 2024-01-12 06:22:27 +03:00 committed by GitHub
parent 8fc91b298c
commit 53cea2f823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 2 deletions

View File

@ -1323,6 +1323,23 @@ $ just
The executable is at: /bin/just
```
#### Just Process ID
- `just_pid()` - Process ID of the `just` executable.
For example:
```just
pid:
@echo The process ID is: {{ just_pid() }}
```
```sh
$ just
The process ID is: 420
```
#### String Manipulation
- `quote(s)` - Replace all single quotes with `'\''` and prepend and append

View File

@ -41,6 +41,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"invocation_directory_native" => Nullary(invocation_directory_native),
"join" => BinaryPlus(join),
"just_executable" => Nullary(just_executable),
"just_pid" => Nullary(just_pid),
"justfile" => Nullary(justfile),
"justfile_directory" => Nullary(justfile_directory),
"kebabcase" => Unary(kebabcase),
@ -251,6 +252,10 @@ fn just_executable(_context: &FunctionContext) -> Result<String, String> {
})
}
fn just_pid(_context: &FunctionContext) -> Result<String, String> {
Ok(std::process::id().to_string())
}
fn justfile(context: &FunctionContext) -> Result<String, String> {
context
.search

View File

@ -651,3 +651,14 @@ fn sha256_file() {
.stdout("177b3d79aaafb53a7a4d7aaba99a82f27c73370e8cb0295571aade1e4fea1cd2")
.run();
}
#[test]
fn just_pid() {
let Output { stdout, pid, .. } = Test::new()
.args(["--evaluate", "x"])
.justfile("x := just_pid()")
.stdout_regex(r"\d+")
.run();
assert_eq!(stdout.parse::<u32>().unwrap(), pid);
}

View File

@ -85,7 +85,9 @@ fn test_invocation_directory() {
#[test]
fn invocation_directory_native() {
let Output { stdout, tempdir } = Test::new()
let Output {
stdout, tempdir, ..
} = Test::new()
.justfile("x := invocation_directory_native()")
.args(["--evaluate", "x"])
.stdout_regex(".*")

View File

@ -35,6 +35,7 @@ macro_rules! test {
}
pub(crate) struct Output {
pub(crate) pid: u32,
pub(crate) stdout: String,
pub(crate) tempdir: TempDir,
}
@ -210,6 +211,8 @@ impl Test {
.spawn()
.expect("just invocation failed");
let pid = child.id();
{
let mut stdin_handle = child.stdin.take().expect("failed to unwrap stdin handle");
@ -257,8 +260,9 @@ impl Test {
}
Output {
tempdir: self.tempdir,
pid,
stdout: output_stdout.into(),
tempdir: self.tempdir,
}
}
}