Skip to content

Commit

Permalink
fix(traverse): create options even when validation error occurs (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Sep 11, 2024
1 parent 5e3d7ff commit 41fa7de
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
27 changes: 27 additions & 0 deletions director-error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tv_test
import (
"context"
"fmt"
"log/slog"
"sync"

"github.com/fortytw2/leaktest"
Expand Down Expand Up @@ -174,5 +175,31 @@ var _ = Describe("director error", Ordered, func() {
wg.Wait()
Expect(err).NotTo(Succeed())
})

It("🧪 should: log error", func(specCtx SpecContext) {
defer leaktest.Check(GinkgoT())()

ctx, cancel := context.WithCancel(specCtx)
defer cancel()

invoked := false
_, _ = tv.Walk().Configure().Extent(tv.Prime(
&tv.Using{
Root: RootPath,
Handler: func(_ *tv.Node) error {
return nil
},
},
tv.WithLogger(
slog.New(slog.NewTextHandler(&TestWriter{
assertFn: func() {
invoked = true
},
}, nil)),
),
)).Navigate(ctx)

Expect(invoked).To(BeTrue(), "validation error not logged")
})
})
})
16 changes: 10 additions & 6 deletions director.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func features(o *pref.Options, using *pref.Using, mediator types.Mediator,

// Prime extent requests that the navigator performs a full
// traversal from the root path specified.
func Prime(using *pref.Using, settings ...pref.Option) *Builders {
func Prime(using *pref.Using, opts ...pref.Option) *Builders {
// TODO: we need to create an aux file system, which is bound
// to a pre-defined location, that will be called upon if
// the navigation session is terminated either by a ctrl-c or
Expand Down Expand Up @@ -101,15 +101,19 @@ func Prime(using *pref.Using, settings ...pref.Option) *Builders {
}
}),
options: optionals(func(ext extent) (*pref.Options, error) {
if err := using.Validate(); err != nil {
return nil, err
}
ve := using.Validate()

if using.O != nil {
return using.O, nil
return using.O, ve
}

return ext.options(settings...)
o, err := ext.options(opts...)

if ve != nil {
return o, ve
}

return o, err
}),
navigator: kernel.Builder(func(o *pref.Options,
resources *types.Resources,
Expand Down
8 changes: 8 additions & 0 deletions pref/options-log.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ type (
MaxAgeInDays int
}
)

func WithLogger(logger *slog.Logger) Option {
return func(o *Options) error {
o.Monitor.Log = logger

return nil
}
}
1 change: 1 addition & 0 deletions traverse-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ var (
WithHookCaseSensitiveSort = pref.WithHookCaseSensitiveSort
WithHookFileSubPath = pref.WithHookFileSubPath
WithHookFolderSubPath = pref.WithHookFolderSubPath
WithLogger = pref.WithLogger
WithNavigationBehaviours = pref.WithNavigationBehaviours
WithOnAscend = pref.WithOnAscend
WithOnBegin = pref.WithOnBegin
Expand Down
9 changes: 9 additions & 0 deletions traverse-suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ const (
var noOpHandler = func(_ *tv.Node) error {
return nil
}

type TestWriter struct {
assertFn func()
}

func (tw *TestWriter) Write([]byte) (int, error) {
tw.assertFn()
return 0, nil
}

0 comments on commit 41fa7de

Please sign in to comment.