-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Zsh microkernel): Add Zsh microkernel
Zsh is now the default shell on MacOS (since Catalina).
- Loading branch information
Showing
7 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "kernel-zsh" | ||
description = "A microkernel for Zsh" | ||
version = "0.0.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
kernel = { path = "../kernel", version = "0.0.0" } | ||
kernel-micro = { path = "../kernel-micro", version = "0.0.0" } | ||
|
||
[dev-dependencies] | ||
tokio = { version = "1.13.0", features = ["macros"] } | ||
test-utils = { path = "../test-utils", version = "0.0.0" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use kernel_micro::{include_file, MicroKernel}; | ||
|
||
/// A microkernel for Zsh | ||
/// | ||
/// The `kernel-zsh.sh` script relies on `/dev/stdin`, `/dev/stdout`, | ||
/// and `/dev/stderr` so this kernel is not available on Windows. | ||
pub fn new() -> MicroKernel { | ||
MicroKernel::new( | ||
"zsh-micro", | ||
&["zsh"], | ||
&["linux", "macos", "windows"], | ||
("zsh", "*"), | ||
&["{{script}}"], | ||
include_file!("zsh-kernel.sh"), | ||
&[], | ||
"{{name}}=\"{{json}}\"", | ||
"echo ${{name}}", | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use kernel::{eyre::Result, stencila_schema::Node, KernelTrait}; | ||
use test_utils::{assert_json_eq, print_logs, serde_json::json}; | ||
|
||
/// Tests of basic functionality | ||
/// This test is replicated in all the microkernels. | ||
/// Other test should be written for language specific quirks and regressions. | ||
#[tokio::test] | ||
async fn basics() -> Result<()> { | ||
print_logs(); | ||
|
||
let mut kernel = new(); | ||
if !kernel.available().await { | ||
return Ok(()); | ||
} else { | ||
kernel.start().await?; | ||
} | ||
|
||
// Assign a variable and output it | ||
let (outputs, messages) = kernel.exec("a=2\necho $a").await?; | ||
assert_json_eq!(messages, json!([])); | ||
assert_json_eq!(outputs, [2]); | ||
|
||
// Syntax error | ||
let (outputs, messages) = kernel.exec("if").await?; | ||
messages[0] | ||
.error_message | ||
.ends_with("syntax error: unexpected end of file\n"); | ||
|
||
assert_json_eq!(outputs, json!([])); | ||
|
||
// Runtime error | ||
let (outputs, messages) = kernel.exec("foo").await?; | ||
messages[0] | ||
.error_message | ||
.ends_with("foo: command not found\n"); | ||
assert_json_eq!(outputs, json!([])); | ||
|
||
// Set and get another variable | ||
kernel.set("b", Node::Integer(3)).await?; | ||
let b = kernel.get("b").await?; | ||
assert_json_eq!(b, 3); | ||
|
||
// Use both variables | ||
let (outputs, messages) = kernel.exec("echo $a$b").await?; | ||
assert_json_eq!(messages, json!([])); | ||
assert_json_eq!(outputs, [23]); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env zsh | ||
|
||
while read -r code | ||
do | ||
print -v unescaped "$code" | ||
eval "$unescaped" | ||
print "\U10ACDC" | tee /dev/stderr | ||
done < "${1:-/dev/stdin}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters