Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell code execution #256

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func Execute(code Block) Result {
}
}

defer f.Close()
defer os.Remove(f.Name())

_, err = f.WriteString(code.Code)
Expand All @@ -96,6 +95,9 @@ func Execute(code Block) Result {
}
}

// Close the file now so platforms like Windows can access it.
f.Close()

var (
output strings.Builder
exitCode int
Expand Down
13 changes: 13 additions & 0 deletions internal/code/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ fmt.Println("Hello, world!")
},
},
},
{
markdown: `
~~~powershell
Write-Host "Hello, World!"
~~~
`,
expected: []code.Block{
{
Code: `Write-Host "Hello, World!"`,
Language: "powershell",
},
},
},
}

for _, tc := range tt {
Expand Down
83 changes: 49 additions & 34 deletions internal/code/execute_test.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,79 @@
package code_test

import (
"runtime"
"testing"

"github.com/maaslalani/slides/internal/code"
)

type TestCase struct {
block code.Block
expected code.Result
}

func TestExecute(t *testing.T) {
tt := []struct {
block code.Block
expected code.Result
}{
tt := []TestCase{
{
block: code.Block{
Code: `Invalid Code`,
Language: "invalid",
},
expected: code.Result{
Out: "Error: unsupported language",
ExitCode: code.ExitCodeInternalError,
},
},
}

if runtime.GOOS == "linux" {
tt = append(tt, TestCase{
block: code.Block{
Code: `
package main
package main

import "fmt"
import "fmt"

func main() {
fmt.Print("Hello, go!")
}
`,
func main() {
fmt.Print("Hello, go!")
}
`,
Language: "go",
},
expected: code.Result{
Out: "Hello, go!",
ExitCode: 0,
},
},
{
block: code.Block{
}},
TestCase{block: code.Block{
Code: `echo "Hello, bash!"`,
Language: "bash",
},
expected: code.Result{
Out: "Hello, bash!\n",
ExitCode: 0,
},
},
{
block: code.Block{
Code: `Invalid Code`,
Language: "bash",
},
expected: code.Result{
Out: "exit status 127",
ExitCode: 127,
expected: code.Result{
Out: "Hello, bash!\n",
ExitCode: 0,
}},
TestCase{
block: code.Block{
Code: `Invalid Code`,
Language: "bash",
},
expected: code.Result{
Out: "exit status 127",
ExitCode: 127,
},
},
},
{
)
} else if runtime.GOOS == "windows" {
tt = append(tt, TestCase{
block: code.Block{
Code: `Invalid Code`,
Language: "invalid",
Code: `Write-Host "Hello, powershell!"`,
Language: "powershell",
},
expected: code.Result{
Out: "Error: unsupported language",
ExitCode: code.ExitCodeInternalError,
Out: "Hello, powershell!\n",
ExitCode: 0,
},
},
})
}

for _, tc := range tt {
Expand Down
5 changes: 5 additions & 0 deletions internal/code/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
Bash = "bash"
Zsh = "zsh"
Fish = "fish"
Powershell = "powershell"
Elixir = "elixir"
Go = "go"
Javascript = "javascript"
Expand Down Expand Up @@ -50,6 +51,10 @@ var Languages = map[string]Language{
Extension: "fish",
Commands: cmds{{"fish", "<file>"}},
},
Powershell: {
Extension: "ps1",
Commands: cmds{{"powershell", "-File", "<file>"}},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Commands: cmds{{"powershell", "-File", "<file>"}},
Commands: cmds{{"powershell", "-NoLogo", "-NoProfile", "-File", "<file>"}},

You probably want to add NoLogoand NoProfile to make things faster and safer. I would also recommend using pwsh since that would work cross platform. I suspect the type of user who wants this tool would be using Pwsh.

},
Elixir: {
Extension: "exs",
Commands: cmds{{"elixir", "<file>"}},
Expand Down