Skip to content

Commit

Permalink
Run shellscript as such (#609)
Browse files Browse the repository at this point in the history
In the extension when the `languageID` set to `shellscript`, it's being
rewritten to `sh`. However, only after serialization following the
initial deserialization.
  • Loading branch information
sourishkrout authored Jun 6, 2024
1 parent 69ef32a commit 53cf215
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 15 deletions.
17 changes: 17 additions & 0 deletions internal/command/command_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ func TestFileCommand(t *testing.T) {
testExecuteCommand(t, cfg, nil, "test", "")
})

t.Run("Shellscript", func(t *testing.T) {
t.Parallel()

cfg := &ProgramConfig{
ProgramName: "",
LanguageId: "shellscript",
Source: &runnerv2alpha1.ProgramConfig_Commands{
Commands: &runnerv2alpha1.ProgramConfig_CommandList{
Items: []string{`echo -n "execute shellscript as shell script"`},
},
},
Mode: runnerv2alpha1.CommandMode_COMMAND_MODE_FILE,
}

testExecuteCommand(t, cfg, nil, "execute shellscript as shell script", "")
})

t.Run("Python", func(t *testing.T) {
t.Parallel()

Expand Down
31 changes: 16 additions & 15 deletions internal/runner/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,21 +618,22 @@ var programByLanguageID = map[string][]string{
"tsx": {"ts-node", "deno run", "bun run"},
"typescriptreact": {"ts-node", "deno run", "bun run"},

"sh": {"bash", "sh"},
"bash": {"bash", "sh"},
"ksh": {"ksh"},
"zsh": {"zsh"},
"fish": {"fish"},
"powershell": {"powershell"},
"cmd": {"cmd"},
"dos": {"cmd"},
"lua": {"lua"},
"perl": {"perl"},
"php": {"php"},
"python": {"python3", "python"},
"py": {"python3", "python"},
"ruby": {"ruby"},
"rb": {"ruby"},
"sh": {"bash", "sh"},
"bash": {"bash", "sh"},
"ksh": {"ksh"},
"zsh": {"zsh"},
"fish": {"fish"},
"powershell": {"powershell"},
"cmd": {"cmd"},
"dos": {"cmd"},
"shellscript": {"bash", "sh"},
"lua": {"lua"},
"perl": {"perl"},
"php": {"php"},
"python": {"python3", "python"},
"py": {"python3", "python"},
"ruby": {"ruby"},
"rb": {"ruby"},
}

type ErrInvalidLanguage struct {
Expand Down
28 changes: 28 additions & 0 deletions internal/runner/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ func Test_command(t *testing.T) {
assert.Equal(t, "", string(data))
})

t.Run("Shellscript", func(t *testing.T) {
t.Parallel()

stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)

cmd, err := newCommand(
&commandConfig{
ProgramName: "",
LanguageID: "shellscript",
Stdout: stdout,
Stderr: stderr,
CommandMode: CommandModeTempFile,
Script: `echo "run this as shell script"`,
Logger: testCreateLogger(t),
},
)
require.NoError(t, err)
require.NoError(t, cmd.Start(context.Background()))
require.NoError(t, cmd.Wait())
data, err := io.ReadAll(stdout)
assert.NoError(t, err)
assert.Equal(t, "run this as shell script\n", string(data))
data, err = io.ReadAll(stderr)
assert.NoError(t, err)
assert.Equal(t, "", string(data))
})

t.Run("JavaScript", func(t *testing.T) {
t.Parallel()

Expand Down
24 changes: 24 additions & 0 deletions testdata/script/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ exec runme run hello-python
stdout 'Hello from Python'
! stderr .

env PATH=/opt/homebrew/bin:$PATH
exec runme run run-shellscript
stdout 'Runs as shell script'
! stderr .

env PATH=/opt/homebrew/bin:$PATH
env RUNME_SERVER_ADDR="123.0.0.9:12345"
exec runme run hello-python
Expand Down Expand Up @@ -169,12 +174,23 @@ def say_hi():
say_hi()
```

-- shellscript.md --
# Examples

## Shell

This is a basic snippet with shell command:

```shellscript {"name":"run-shellscript"}
$ echo "Runs as shell script"
```
-- golden-list.txt --
NAME FILE FIRST COMMAND DESCRIPTION NAMED
echo README.md echo "Hello, runme!" With {"name":"hello"} you can annotate it and give it a nice name. Yes
hello-js README.md console.log("Hello, runme, from javascript!") It can even run scripting languages. Yes
hello-js-cat README.md console.log("Hello, runme, from javascript!") And it can even run a cell with a custom interpreter. Yes
hello-python README.md def say_hi(): Yes
run-shellscript shellscript.md echo "Runs as shell script" This is a basic snippet with shell command. Yes
-- golden-list-allow-unnamed.txt --
NAME FILE FIRST COMMAND DESCRIPTION NAMED
echo-hello README.md echo "Hello, runme!" This is a basic snippet with shell command. No
Expand All @@ -188,6 +204,7 @@ hello-js-cat README.md console.log("Hello, runme, from javascript!") And it can
tempdir README.md temp_dir=$(mktemp -d -t "runme-XXXXXXX") It works with cd, pushd, and similar because all lines are executed as a single script. No
package-main README.md package main It can also execute a snippet of Go code. No
hello-python README.md def say_hi(): Yes
run-shellscript shellscript.md echo "Runs as shell script" This is a basic snippet with shell command. Yes
-- golden-list-json.txt --
[
{
Expand Down Expand Up @@ -217,5 +234,12 @@ hello-python README.md def say_hi(): Yes
"first_command": "def say_hi():",
"description": "",
"named": true
},
{
"name": "run-shellscript",
"file": "shellscript.md",
"first_command": "echo \"Runs as shell script\"",
"description": "This is a basic snippet with shell command.",
"named": true
}
]

0 comments on commit 53cf215

Please sign in to comment.