Skip to content

Commit

Permalink
Adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Aug 30, 2023
1 parent 546b8e8 commit 8e1f56f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/pkg/agent/install/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func (pt *ProgressTracker) StepStart(msg string) {
}

func (pt *ProgressTracker) StepSucceeded() {
fmt.Fprintln(pt.writer, "DONE")
fmt.Fprintln(pt.writer, " DONE")
}

func (pt *ProgressTracker) StepFailed() {
fmt.Fprintln(pt.writer, "FAILED")
fmt.Fprintln(pt.writer, " FAILED")
}
50 changes: 50 additions & 0 deletions internal/pkg/agent/install/progress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package install

import (
"testing"

"github.com/stretchr/testify/require"
)

type testWriter struct {
buf []byte
}

func newTestWriter() *testWriter {
return &testWriter{
buf: []byte{},
}
}

func (tw *testWriter) Write(p []byte) (int, error) {
tw.buf = append(tw.buf, p...)
return len(p), nil
}

func TestProgress(t *testing.T) {
t.Run("single_step_immediate_failure", func(t *testing.T) {
w := newTestWriter()
pt := NewProgressTracker(w)

pt.StepStart("step 1 starting")
pt.StepFailed()

require.Equal(t, "step 1 starting... FAILED\n", string(w.buf))
})

t.Run("multi_step_success", func(t *testing.T) {
w := newTestWriter()
pt := NewProgressTracker(w)

pt.StepStart("step 1 starting")
pt.StepSucceeded()
pt.StepStart("step 2 starting")
pt.StepSucceeded()

require.Equal(t, "step 1 starting... DONE\nstep 2 starting... DONE\n", string(w.buf))
})
}

0 comments on commit 8e1f56f

Please sign in to comment.