Skip to content

Commit

Permalink
chore: use testify instead of testing.Fatal or testing.Error in appli…
Browse files Browse the repository at this point in the history
…cationset

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 committed Nov 9, 2024
1 parent 5d0a3e6 commit 6fc27fa
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 36 deletions.
21 changes: 6 additions & 15 deletions applicationset/services/internal/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

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

func TestClient(t *testing.T) {
Expand All @@ -24,9 +25,7 @@ func TestClient(t *testing.T) {

var clientOptionFns []ClientOptionFunc
_, err := NewClient(server.URL, clientOptionFns...)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
require.NoError(t, err, "Failed to create client")
}

func TestClientDo(t *testing.T) {
Expand Down Expand Up @@ -118,14 +117,10 @@ func TestClientDo(t *testing.T) {
defer cc.fakeServer.Close()

client, err := NewClient(cc.fakeServer.URL, cc.clientOptionFns...)
if err != nil {
t.Fatalf("NewClient returned unexpected error: %v", err)
}
require.NoError(t, err, "NewClient returned unexpected error")

req, err := client.NewRequest("POST", "", cc.params, nil)
if err != nil {
t.Fatalf("NewRequest returned unexpected error: %v", err)
}
require.NoError(t, err, "NewRequest returned unexpected error")

var data []map[string]interface{}

Expand All @@ -149,12 +144,8 @@ func TestCheckResponse(t *testing.T) {
}

err := CheckResponse(resp)
if err == nil {
t.Error("Expected an error, got nil")
}
require.Error(t, err)

expected := "API error with status code 400: invalid_request"
if err.Error() != expected {
t.Errorf("Expected error '%s', got '%s'", expected, err.Error())
}
assert.EqualErrorf(t, err, expected, "Expected error '%s', got '%s'", expected, err.Error())
}
13 changes: 4 additions & 9 deletions applicationset/services/plugin/plugin_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

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

func TestPlugin(t *testing.T) {
Expand All @@ -31,19 +32,13 @@ func TestPlugin(t *testing.T) {
defer ts.Close()

client, err := NewPluginService(context.Background(), "plugin-test", ts.URL, token, 0)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
require.NoError(t, err)

data, err := client.List(context.Background(), nil)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
require.NoError(t, err)

var expectedData ServiceResponse
err = json.Unmarshal([]byte(expectedJSON), &expectedData)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
assert.Equal(t, &expectedData, data)
}
5 changes: 2 additions & 3 deletions applicationset/services/pull_request/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ func TestContainLabels(t *testing.T) {

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
if got := containLabels(c.Labels, c.PullLabels); got != c.Expect {
t.Errorf("expect: %v, got: %v", c.Expect, got)
}
got := containLabels(c.Labels, c.PullLabels)
assert.Equalf(t, got, c.Expect, "expect: %v, got: %v", c.Expect, got)
})
}
}
Expand Down
9 changes: 3 additions & 6 deletions applicationset/services/pull_request/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ import (
func writeMRListResponse(t *testing.T, w io.Writer) {
t.Helper()
f, err := os.Open("fixtures/gitlab_mr_list_response.json")
if err != nil {
t.Fatalf("error opening fixture file: %v", err)
}
require.NoErrorf(t, err, "error opening fixture file: %v", err)

if _, err = io.Copy(w, f); err != nil {
t.Fatalf("error writing response: %v", err)
}
_, err = io.Copy(w, f)
require.NoErrorf(t, err, "error writing response: %v", err)
}

func TestGitLabServiceCustomBaseURL(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions applicationset/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,8 @@ func TestWebhookHandler(t *testing.T) {
for i := range list.Items {
gotAppSet := &list.Items[i]
if _, isEffected := effectedAppSetsAsExpected[gotAppSet.Name]; isEffected {
if expected, got := test.expectedRefresh, gotAppSet.RefreshRequired(); expected != got {
t.Errorf("unexpected RefreshRequired() for appset '%s' expect: %v got: %v", gotAppSet.Name, expected, got)
}
expected, got := test.expectedRefresh, gotAppSet.RefreshRequired()
require.Equalf(t, expected, got, "unexpected RefreshRequired() for appset '%s' expect: %v got: %v", gotAppSet.Name, expected, got)
effectedAppSetsAsExpected[gotAppSet.Name] = true
} else {
assert.False(t, gotAppSet.RefreshRequired())
Expand Down

0 comments on commit 6fc27fa

Please sign in to comment.