Skip to content

Commit

Permalink
add e2e test case to validate command execution within a tty
Browse files Browse the repository at this point in the history
  • Loading branch information
pastuxso committed Oct 26, 2023
1 parent c53448c commit 3e049a4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
8 changes: 8 additions & 0 deletions examples/frontmatter/skipPrompts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
skipPrompts: true
---

```sh { name=skip-prompts-sample }
$ export ENV="<insert-env-here>"
$ echo "The content of ENV is ${ENV}"
```
12 changes: 8 additions & 4 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ func runCmd() *cobra.Command {
return err
}

if len(blocks) > 0 && blocks[0].Frontmatter.SkipPrompts {
skipPrompts = true
}

if runWithIndex {
if runIndex >= len(blocks) {
return fmt.Errorf("command index %v out of range", runIndex)
Expand Down Expand Up @@ -192,6 +188,14 @@ func runCmd() *cobra.Command {
if err != nil {
return err
}

for _, block := range runBlocks {
if block.GetFrontmatter().SkipPrompts {
skipPrompts = true
break
}
}

if (skipPromptsExplicitly || isTerminal(os.Stdout.Fd())) && !skipPrompts {
err = promptEnvVars(cmd, sessionEnvs, runBlocks...)
if err != nil {
Expand Down
43 changes: 43 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
package main

import (
"bytes"
"context"
"os"
"os/exec"
"regexp"
"strings"
"syscall"
"testing"
"time"

"github.com/creack/pty"
"github.com/rogpeppe/go-internal/testscript"
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -36,3 +45,37 @@ func TestRunmeRunAll(t *testing.T) {
Dir: "testdata/runall",
})
}

func TestSkipPromptsWithinAPty(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

cmd := exec.Command("go", "run", ".", "run", "skip-prompts-sample")
ptmx, err := pty.StartWithAttrs(cmd, &pty.Winsize{Rows: 25, Cols: 80}, &syscall.SysProcAttr{})

if err != nil {
t.Fatalf("Could not start command with pty: %s", err)
}
defer ptmx.Close()

buf := new(bytes.Buffer)
buf.ReadFrom(ptmx)

if ctx.Err() == context.DeadlineExceeded {
t.Fatalf("Command timed out")
return
}

assert.Nil(t, err, "Command execution failed")

expected := "The content of ENV is <insert-env-here>"
current := RemoveAnsiCodes(buf.String())
current = strings.TrimSpace(current)

assert.Equal(t, expected, current, "Output does not match")
}

func RemoveAnsiCodes(str string) string {
re := regexp.MustCompile(`\x1b\[.*?[a-zA-Z]|\x1b\].*?\x1b\\`)
return re.ReplaceAllString(str, "")
}
25 changes: 21 additions & 4 deletions testdata/runall/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ exec runme run --all --filename=README.md
cmp stdout all.txt
! stderr .

env SHELL=/bin/bash
exec runme run foo-command
cmp stdout skip.txt
! stderr .

-- all.txt --
► Running task set-env...
► ✓ Task set-env exited with code 0
Expand All @@ -12,26 +17,38 @@ foo!
► Running task print-bar...
bar!
► ✓ Task print-bar exited with code 0
-- skip.txt --
foo-command
-- README.md --
---
skipPrompts: true
---

```bash { name=set-env category=foo }
```bash { name=set-env category=foo interactive=true }
$ export ENV="foo!"
```

```bash { name=print-foo category=foo }
```bash { name=print-foo category=foo interactive=true }
$ stty -opost
$ echo "$ENV"
```

```bash { name=print-bar category=foo,bar }
```bash { name=print-bar category=foo,bar interactive=true }
$ stty -opost
$ echo "bar!"
```

```bash { name=excluded category=foo,bar excludeFromRunAll=true }
```bash { name=excluded category=foo,bar interactive=true excludeFromRunAll=true }
$ stty -opost
$ echo "excluded!"
```
-- SKIP.md --
---
skipPrompts: true
---

```sh { name=foo-command category=c1 interactive=true}
$ stty -opost
export BAR="foo-command"
echo $BAR
```

0 comments on commit 3e049a4

Please sign in to comment.