Skip to content

Commit

Permalink
fix: properly parse exec (#40)
Browse files Browse the repository at this point in the history
* fix: properly parse exec

Split by space is not always correct, for instance:

```
gum format "foo bar"
```

would be split in:

```
["gum" "format" "\"foo" "bar\""]
```

Using the shellwords parse it would correctly split it to:

```
["gum" "format" "foo bar"]
```

* fix: tests

---------

Co-authored-by: Maas Lalani <maas@lalani.dev>
  • Loading branch information
caarlos0 and maaslalani committed Mar 26, 2024
1 parent 2427fce commit d4b1c86
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
23 changes: 17 additions & 6 deletions freeze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
Expand All @@ -13,7 +14,10 @@ import (

const binary = "./test/freeze-test"

var update = flag.Bool("update", false, "update golden files")

func TestMain(m *testing.M) {
flag.Parse()
cmd := exec.Command("go", "build", "-o", binary)
err := cmd.Run()
if err != nil {
Expand Down Expand Up @@ -56,7 +60,6 @@ func TestFreezeHelp(t *testing.T) {
cmd := exec.Command(binary)
cmd.Stdout = &out
err := cmd.Run()

if err != nil {
t.Fatal("unexpected error")
}
Expand Down Expand Up @@ -260,17 +263,25 @@ func TestFreezeConfigurations(t *testing.T) {
t.Log(out.String())
t.Fatal("unexpected error")
}
want, err := os.ReadFile("test/golden/" + tc.output + ".svg")
gotfile := "test/output/" + tc.output + ".svg"
got, err := os.ReadFile(gotfile)
if err != nil {
t.Fatal("no golden file for:", "test/golden/"+tc.output+".svg")
t.Fatal("no output file for:", gotfile)
}
goldenfile := "test/golden/" + tc.output + ".svg"
if *update {
if err := os.WriteFile(goldenfile, got, 0644); err != nil {
t.Log(err)
t.Fatal("unexpected error")
}
}
got, err := os.ReadFile("test/output/" + tc.output + ".svg")
want, err := os.ReadFile(goldenfile)
if err != nil {
t.Fatal("no output file for:", "test/output/"+tc.output+".svg")
t.Fatal("no golden file for:", goldenfile)
}
if string(want) != string(got) {
t.Log(udiff.Unified("want", "got", string(want), string(got)))
t.Fatalf("golden/%s.svg != output/%s.svg", tc.output, tc.output)
t.Fatalf("%s != %s", goldenfile, gotfile)
}

// output PNG
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/alecthomas/kong v0.9.0
github.com/aymanbagabas/go-udiff v0.2.0
github.com/beevik/etree v1.3.0
github.com/caarlos0/go-shellwords v1.0.12
github.com/charmbracelet/huh v0.3.1-0.20240321161533-278656239a9d
github.com/charmbracelet/lipgloss v0.10.1-0.20240322201939-b71dd1266410
github.com/charmbracelet/log v0.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWp
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
github.com/beevik/etree v1.3.0 h1:hQTc+pylzIKDb23yYprodCWWTt+ojFfUZyzU09a/hmU=
github.com/beevik/etree v1.3.0/go.mod h1:aiPf89g/1k3AShMVAzriilpcE4R/Vuor90y83zVZWFc=
github.com/caarlos0/go-shellwords v1.0.12 h1:HWrUnu6lGbWfrDcFiHcZiwOLzHWjjrPVehULaTFgPp8=
github.com/caarlos0/go-shellwords v1.0.12/go.mod h1:bYeeX1GrTLPl5cAMYEzdm272qdsQAZiaHgeF0KTk1Gw=
github.com/catppuccin/go v0.2.0 h1:ktBeIrIP42b/8FGiScP9sgrWOss3lw0Z5SktRoithGA=
github.com/catppuccin/go v0.2.0/go.mod h1:8IHJuMGaUUjQM82qBrGNBv7LFq6JI3NnQCF6MOlZjpc=
github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0=
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/alecthomas/chroma/v2/styles"
"github.com/alecthomas/kong"
"github.com/beevik/etree"
"github.com/caarlos0/go-shellwords"
in "github.com/charmbracelet/freeze/input"
"github.com/charmbracelet/freeze/svg"
"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -395,7 +396,10 @@ func main() {
}

func executeCommand(config Config) string {
args := strings.Split(config.Execute, " ")
args, err := shellwords.Parse(config.Execute)
if err != nil {
printErrorFatal("Something went wrong", err)
}
ctx, _ := context.WithTimeout(context.Background(), config.ExecuteTimeout)
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
pty, err := config.runInPty(cmd)
Expand Down
6 changes: 3 additions & 3 deletions test/golden/execute.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/output/execute.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d4b1c86

Please sign in to comment.