Skip to content

Commit

Permalink
lint & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanlookpotts committed Jan 27, 2025
1 parent 3b69f0d commit d52ab1b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
4 changes: 3 additions & 1 deletion components/button/button_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"go.viam.com/rdk/components/button"
"go.viam.com/rdk/components/button/fake"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
)

Expand All @@ -19,11 +20,12 @@ const (
)

func TestPush(t *testing.T) {
logger := logging.NewTestLogger(t)
cfg := resource.Config{
Name: "fakeButton",
API: button.API,
}
button, err := fake.NewButton(context.Background(), nil, cfg, nil)
button, err := fake.NewButton(context.Background(), nil, cfg, logger)
test.That(t, err, test.ShouldBeNil)

err = button.Push(context.Background(), nil)
Expand Down
4 changes: 2 additions & 2 deletions components/button/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func TestClient(t *testing.T) {
var buttonPushed string
var extraOptions map[string]interface{}

injectButton := &inject.Button{}
injectButton := inject.NewButton(testButtonName)
injectButton.PushFunc = func(ctx context.Context, extra map[string]interface{}) error {
extraOptions = extra
buttonPushed = testButtonName
return nil
}

injectButton2 := &inject.Button{}
injectButton2 := inject.NewButton(failButtonName)
injectButton2.PushFunc = func(ctx context.Context, extra map[string]interface{}) error {
buttonPushed = failButtonName
return errCantPush
Expand Down
6 changes: 3 additions & 3 deletions components/button/fake/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ func init() {
resource.RegisterComponent(button.API, model, resource.Registration[button.Button, *resource.NoNativeConfig]{Constructor: NewButton})
}

// Button is a fake button that logs when it is pressed
// Button is a fake button that logs when it is pressed.
type Button struct {
resource.Named
resource.TriviallyCloseable
resource.AlwaysRebuild
resource.AlwaysRebuild
logger logging.Logger
}

Expand All @@ -34,7 +34,7 @@ func NewButton(
return b, nil
}

// Push logs the push
// Push logs the push.
func (b *Button) Push(ctx context.Context, extra map[string]interface{}) error {
b.logger.Info("pushed button")
return nil
Expand Down
2 changes: 1 addition & 1 deletion components/button/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewRPCServiceServer(coll resource.APIResourceCollection[Button]) interface{
return &serviceServer{coll: coll}
}

// Pushes a button
// Pushes a button.
func (s *serviceServer) Push(ctx context.Context, req *pb.PushRequest) (*pb.PushResponse, error) {
button, err := s.coll.Resource(req.Name)
if err != nil {
Expand Down
26 changes: 21 additions & 5 deletions testutils/inject/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ import (
// Button implements button.Button for testing.
type Button struct {
button.Button
name resource.Name
PushFunc func(ctx context.Context, extra map[string]interface{}) error
DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error)
CloseFunc func(ctx context.Context) error
}

// NewButton returns a new injected button.
func NewButton(name string) *Button {
return &Button{name: button.Named(name)}
}

resource.Named
resource.TriviallyReconfigurable
resource.TriviallyCloseable
PushFunc func(ctx context.Context, extra map[string]interface{}) error
DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error)
// Name returns the name of the resource.
func (b *Button) Name() resource.Name {
return b.name
}

// Push calls PushFunc.
Expand All @@ -33,3 +41,11 @@ func (b *Button) DoCommand(ctx context.Context, cmd map[string]interface{}) (map
}
return b.DoFunc(ctx, cmd)
}

// Close calls CloseFunc.
func (b *Button) Close(ctx context.Context) error {
if b.CloseFunc == nil {
return b.Button.Close(ctx)
}
return b.CloseFunc(ctx)
}

0 comments on commit d52ab1b

Please sign in to comment.