Skip to content

Commit

Permalink
add test err
Browse files Browse the repository at this point in the history
  • Loading branch information
wenn committed Nov 29, 2024
1 parent c791d9b commit a08e52a
Show file tree
Hide file tree
Showing 14 changed files with 641 additions and 58 deletions.
4 changes: 3 additions & 1 deletion check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func gitDir() (string, error) {
return dir, nil
}

func fileExists(path string) bool {
var fileExists = _fileExists

func _fileExists(path string) bool {
if _, err := os.Stat(path); !os.IsNotExist(err) {
return true
}
Expand Down
73 changes: 73 additions & 0 deletions check/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package check

import (
"os"
"strings"
"testing"
)

func TestHasGithubToken(t *testing.T) {
original := os.Getenv("GITHUB_TOKEN")

t.Run("when GITHUB_TOKEN is set", func(t *testing.T) {
os.Setenv("GITHUB_TOKEN", "foo")
if !HasGithubToken() {
t.Errorf("expected HasGithubToken() to be true")
}
os.Setenv("GITHUB_TOKEN", original)
})

t.Run("when GITHUB_TOKEN is not set", func(t *testing.T) {
os.Setenv("GITHUB_TOKEN", "")
if HasGithubToken() {
t.Errorf("expected HasGithubToken() to be false")
}
os.Setenv("GITHUB_TOKEN", original)
})
}

func TestOnOrchestra(t *testing.T) {
t.Run("when conductor-on-orchestra.yml exists", func(t *testing.T) {
want := "sbx/.github/workflows/conductor-on-orchestra.yml"

fileExists = func(path string) bool {
if !strings.Contains(path, want) {
t.Errorf("got %v, want %v", path, want)
}

return true
}

on, err := OnOrchestra()

if !on {
t.Errorf("expected to be true")
}

if err != nil {
t.Errorf("got %v, want nil", err)
}
})

t.Run("when conductor-on-orchestra.yml does not exist", func(t *testing.T) {
want := "sbx/.github/workflows/conductor-on-orchestra.yml"

fileExists = func(path string) bool {
if !strings.Contains(path, want) {
t.Errorf("got %v, want %v", path, want)
}

return false
}

on, err := OnOrchestra()

if on {
t.Errorf("expected to be false")
}

if err != nil {
t.Errorf("got %v, want nil", err)
}
})
}
4 changes: 3 additions & 1 deletion commit/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"github.com/reverbdotcom/sbx/cli"
)

var cmdFn = cli.Cmd

func HeadSHA() (string, error) {
out, err := cli.Cmd("git", "rev-parse", "HEAD")
out, err := cmdFn("git", "rev-parse", "HEAD")

if err != nil {
return out, err
Expand Down
48 changes: 48 additions & 0 deletions commit/head_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package commit

import (
"errors"
"testing"

"github.com/reverbdotcom/sbx/cli"
)

func TestHeadSHA(t *testing.T) {
t.Run("it gets the head sha", func(t *testing.T) {
wants := []cli.MockCall{
{
Command: "git rev-parse HEAD",
Out: "123456",
Err: nil,
},
}

cmdFn = cli.MockCmd(t, wants)
sha, err := HeadSHA()

if err != nil {
t.Errorf("got %v", err)
}

if sha != "123456" {
t.Errorf("got %v", sha)
}
})

t.Run("it errs on cmdFn", func(t *testing.T) {
wants := []cli.MockCall{
{
Command: "git rev-parse HEAD",
Out: "123456",
Err: errors.New("cmd error"),
},
}

cmdFn = cli.MockCmd(t, wants)
_, err := HeadSHA()

if err.Error() != "cmd error" {
t.Errorf("got %v", err)
}
})
}
26 changes: 26 additions & 0 deletions debug/debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package debug

import (
"os"
"testing"
)

func TestOn(t *testing.T) {
original := os.Getenv("DEBUG")

t.Run("when DEBUG is true", func(t *testing.T) {
os.Setenv("DEBUG", "true")
if !On() {
t.Errorf("expected On() to be true")
}
os.Setenv("DEBUG", original)
})

t.Run("when DEBUG is false", func(t *testing.T) {
os.Setenv("DEBUG", "false")
if On() {
t.Errorf("expected On() to be false")
}
os.Setenv("DEBUG", original)
})
}
13 changes: 13 additions & 0 deletions errr/errr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package errr

import (
"testing"
)

func TestNew(t *testing.T) {
got := New("test")
want := "🚫 test"
if got.Error() != want {
t.Errorf("got %q want %q", got, want)
}
}
34 changes: 34 additions & 0 deletions retries/backoff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package retries

import (
"errors"
"time"
)

type Done = bool
type BackoffFunc func() (Done, error)

const ErrBackoffExhausted = "backoff retries exhausted"

var sleep = time.Sleep

func Backoff(maxRetries int, step int, f BackoffFunc) error {
for i := 0; i < maxRetries; i++ {
backoff := time.Duration(i*step) * time.Second
done, err := f()

if err != nil {
return err
}

if done {
return nil
}

if i < maxRetries {
sleep(backoff)
}
}

return errors.New(ErrBackoffExhausted)
}
132 changes: 132 additions & 0 deletions retries/backoff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package retries

import (
"errors"
"testing"
"time"
)

func TestBackoff(t *testing.T) {
t.Run("completes on success", func(t *testing.T) {
sleepCalls := []time.Duration{}
sleep = func(d time.Duration) {
sleepCalls = append(sleepCalls, d)
}

var want int
f := func() (Done, error) {
want++
return true, nil
}

err := Backoff(3, 1, f)

if err != nil {
t.Errorf("expected nil, got %v", err)
}

if want != 1 {
t.Errorf("expected 1, got %v", want)
}

if len(sleepCalls) != 0 {
t.Errorf("expected 0, got %v", len(sleepCalls))
}
})

t.Run("returns errs if func errs", func(t *testing.T) {
sleepCalls := []time.Duration{}
sleep = func(d time.Duration) {
sleepCalls = append(sleepCalls, d)
}

f := func() (Done, error) {
return false, errors.New("some error")
}

err := Backoff(3, 1, f)

if err.Error() != "some error" {
t.Errorf("expected some error, got %v", err)
}

if len(sleepCalls) != 0 {
t.Errorf("expected 0, got %v", len(sleepCalls))
}
})

t.Run("retries until completion", func(t *testing.T) {
sleepCalls := []time.Duration{}
sleep = func(d time.Duration) {
sleepCalls = append(sleepCalls, d)
}

var want int
f := func() (Done, error) {
want++
if want < 3 {
return false, nil
}

return true, nil
}

err := Backoff(3, 1, f)

if err != nil {
t.Errorf("expected nil, got %v", err)
}

if want != 3 {
t.Errorf("expected 3, got %v", want)
}

if len(sleepCalls) != 2 {
t.Errorf("expected 2, got %v", len(sleepCalls))
}

wantSleepCalls := []time.Duration{
0 * time.Second,
1 * time.Second,
}

for i, v := range sleepCalls {
if v != wantSleepCalls[i] {
t.Errorf("expected %v, got %v", wantSleepCalls[i], v)
}
}
})

t.Run("returns exhausted error", func(t *testing.T) {
sleepCalls := []time.Duration{}
sleep = func(d time.Duration) {
sleepCalls = append(sleepCalls, d)
}

f := func() (Done, error) {
return false, nil
}

err := Backoff(3, 2, f)

if err.Error() != ErrBackoffExhausted {
t.Errorf("expected %v, got %v", ErrBackoffExhausted, err)
}

if len(sleepCalls) != 3 {
t.Errorf("expected 3, got %v", len(sleepCalls))
}

wantSleepCalls := []time.Duration{
0 * time.Second,
2 * time.Second,
4 * time.Second,
}

for i, v := range sleepCalls {
if v != wantSleepCalls[i] {
t.Errorf("expected %v, got %v", wantSleepCalls[i], v)
}
}
})
}
Loading

0 comments on commit a08e52a

Please sign in to comment.