Skip to content

Commit

Permalink
don't actually use errgroup
Browse files Browse the repository at this point in the history
don't want cancelation semantics; just want to be able to see the errors
  • Loading branch information
vito committed Jun 13, 2022
1 parent 7df902c commit fd028cd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
31 changes: 29 additions & 2 deletions pkg/bass/runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,39 @@ package bass

import (
"context"
"sync"

"golang.org/x/sync/errgroup"
"github.com/hashicorp/go-multierror"
)

type Runs struct {
errgroup.Group
wg sync.WaitGroup

errs error
errsL sync.Mutex
}

func (runs *Runs) Go(f func() error) {
runs.wg.Add(1)
go func() {
defer runs.wg.Done()
runs.record(f())
}()
}

func (runs *Runs) Wait() error {
runs.wg.Wait()
return runs.errs
}

func (runs *Runs) record(err error) {
runs.errsL.Lock()
if runs.errs != nil {
runs.errs = multierror.Append(runs.errs, err)
} else {
runs.errs = err
}
runs.errsL.Unlock()
}

type runsKey struct{}
Expand Down
30 changes: 20 additions & 10 deletions pkg/bass/runs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bass_test
import (
"context"
"fmt"
"strings"
"testing"

"github.com/vito/bass/pkg/bass"
Expand All @@ -17,28 +18,37 @@ func TestRuns(t *testing.T) {
ctx, runs := bass.TrackRuns(ctx)

errorScpt := bass.NewInMemoryFile("error.bass", `
(error "oh no")
(defn main [msg]
(error msg))
`)

thunk := bass.Thunk{
Cmd: bass.ThunkCmd{
FS: errorScpt,
},
}
thunk1 := bass.MustThunk(errorScpt).AppendArgs(bass.String("oh no"))
thunk2 := bass.MustThunk(errorScpt).AppendArgs(bass.String("let's go"))

comb, err := thunk.Start(ctx, bass.Func("done", "[ok?]", func(ok bool) error {
errCb := bass.Func("err-if-not-ok", "[ok?]", func(ok bool) error {
if !ok {
return fmt.Errorf("it failed!")
}

return nil
}))
})

_, err = basstest.Call(comb, bass.NewEmptyScope(), bass.NewList())
comb1, err := thunk1.Start(ctx, errCb)
is.NoErr(err)

comb2, err := thunk2.Start(ctx, errCb)
is.NoErr(err)

_, err = basstest.Call(comb1, bass.NewEmptyScope(), bass.NewList())
is.True(err != nil)
is.Equal(err.Error(), "it failed!: oh no")

_, err = basstest.Call(comb2, bass.NewEmptyScope(), bass.NewList())
is.True(err != nil)
is.Equal(err.Error(), "it failed!: let's go")

err = runs.Wait()
is.True(err != nil)
is.Equal(err.Error(), "it failed!: oh no")
is.True(strings.Contains(err.Error(), "it failed!: oh no"))
is.True(strings.Contains(err.Error(), "it failed!: let's go"))
}
6 changes: 6 additions & 0 deletions pkg/bass/thunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ func (thunk Thunk) WithArgs(args []Value) Thunk {
return thunk
}

// AppendArgs appends to the thunk's arg values.
func (thunk Thunk) AppendArgs(args ...Value) Thunk {
thunk.Args = append(thunk.Args, args...)
return thunk
}

// WithEnv sets the thunk's env.
func (thunk Thunk) WithEnv(env *Scope) Thunk {
thunk.Env = env
Expand Down

0 comments on commit fd028cd

Please sign in to comment.