-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
641 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}) | ||
} |
Oops, something went wrong.