Skip to content

Commit

Permalink
adds support to handle skipPrompts from MD files in the cli (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
pastuxso authored Oct 27, 2023
1 parent 19b53d2 commit d471012
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 7 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}"
```
21 changes: 17 additions & 4 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,18 @@ func runCmd() *cobra.Command {
for _, fileBlock := range blocks {
block := fileBlock.Block

if category == "" || block.ExcludeFromRunAll() {
if runAll && block.ExcludeFromRunAll() {
continue
}

containsCategory := slices.Contains(strings.Split(block.Category(), ","), category)
if !containsCategory {
continue
if category != "" {
if block.ExcludeFromRunAll() {
continue
}

if !slices.Contains(strings.Split(block.Category(), ","), category) {
continue
}
}

runBlocks = append(runBlocks, fileBlock)
Expand Down Expand Up @@ -183,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
8 changes: 5 additions & 3 deletions internal/cmd/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ func tuiCmd() *cobra.Command {

runBlock := result.block.Clone()

err = promptEnvVars(cmd, sessionEnvs, runBlock)
if err != nil {
return err
if !runBlock.GetFrontmatter().SkipPrompts {
err = promptEnvVars(cmd, sessionEnvs, runBlock)
if err != nil {
return err
}
}

err = inRawMode(func() error {
Expand Down
57 changes: 57 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 All @@ -24,3 +33,51 @@ func TestRunme(t *testing.T) {
Dir: "testdata/script",
})
}

func TestRunmeCategories(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/categories",
})
}

func TestRunmeRunAll(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/runall",
})
}

func TestSkipPromptsWithinAPty(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = os.Setenv("RUNME_VERBOSE", "false")
defer os.Unsetenv("RUNME_VERBOSE")

cmd := exec.Command("go", "run", ".", "run", "skip-prompts-sample", "--chdir", "./examples/frontmatter/skipPrompts")
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 := buf.String()
current = RemoveAnsiCodes(current)
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, "")
}
42 changes: 42 additions & 0 deletions testdata/categories/basic.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
env SHELL=/bin/bash
exec runme run --all --category=foo --filename=CATEGORIES.md
cmp stdout foo-bar-list.txt
! stderr .

env SHELL=/bin/bash
exec runme run --all --category=bar --filename=CATEGORIES.md
cmp stdout bar-list.txt
! stderr .

-- CATEGORIES.md --

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

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

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

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

-- foo-bar-list.txt --
► Running task set-env...
► ✓ Task set-env exited with code 0
► Running task print-foo...
foo!
► ✓ Task print-foo exited with code 0
► Running task print-bar...
bar!
► ✓ Task print-bar exited with code 0
-- bar-list.txt --
bar!
54 changes: 54 additions & 0 deletions testdata/runall/basic.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
env SHELL=/bin/bash
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
► Running task print-foo...
foo!
► ✓ Task print-foo exited with code 0
► 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 interactive=true }
$ export ENV="foo!"
```

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

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

```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 d471012

Please sign in to comment.