summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSwordelf2 <evgeny1998oo@gmail.com>2024-01-12 06:22:27 +0300
committerGitHub <noreply@github.com>2024-01-12 03:22:27 +0000
commit53cea2f82351012f9c6708dd109ad2922ea0487c (patch)
treec58495fc9892bb370cc751491c247d640ed11ac6
parent8fc91b298c6b436b4dd54da34d365399f13fa172 (diff)
Add `just_pid` function (#1833)
-rw-r--r--README.md17
-rw-r--r--src/function.rs5
-rw-r--r--tests/functions.rs11
-rw-r--r--tests/invocation_directory.rs4
-rw-r--r--tests/test.rs6
5 files changed, 41 insertions, 2 deletions
diff --git a/README.md b/README.md
index b5d622de..c854c565 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/src/function.rs b/src/function.rs
index c4923e59..464e64a1 100644
--- a/src/function.rs
+++ b/src/function.rs
@@ -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
diff --git a/tests/functions.rs b/tests/functions.rs
index 53566158..e1da40ad 100644
--- a/tests/functions.rs
+++ b/tests/functions.rs
@@ -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);
+}
diff --git a/tests/invocation_directory.rs b/tests/invocation_directory.rs
index b62790e7..bbef3857 100644
--- a/tests/invocation_directory.rs
+++ b/tests/invocation_directory.rs
@@ -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(".*")
diff --git a/tests/test.rs b/tests/test.rs
index 4aad92b7..a615fb65 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -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,
}
}
}