Skip to content

Commit

Permalink
chore: consolidate on the WantFns
Browse files Browse the repository at this point in the history
I'm digging the dev experienc with it :-)
  • Loading branch information
jsteenb2 committed Nov 15, 2024
1 parent 5ca134d commit 03a9a31
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 154 deletions.
145 changes: 92 additions & 53 deletions fdktest/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,108 @@ package fdktest

import (
"encoding/json"
"fmt"
"io"
"runtime"
"strings"
"testing"

fdk "github.com/CrowdStrike/foundry-fn-go"
)

// WantErrs compares verifies the errors received from teh response match the desired.
func WantErrs(t *testing.T, got []fdk.APIError, wants ...fdk.APIError) {
t.Helper()
// WantFn defines the assertions for a response.
type WantFn func(t *testing.T, got fdk.Response)

if len(got) != len(wants) {
t.Fatalf("number of errors mismatched\n\t\twant:\t%+v\n\t\tgot:\t%+v", wants, got)
}
// WantErrs compares verifies the errors received from teh response match the desired.
func WantErrs(wants ...fdk.APIError) WantFn {
line := src(1)
return func(t *testing.T, got fdk.Response) {
t.Helper()
if len(got.Errors) != len(wants) {
t.Fatalf("number of errors mismatched\n\t\twant:\t%+v\n\t\tgot:\t%+v\n\t\tsource: %s", wants, got, line)
}

for i, want := range wants {
err := got[i]
if err != want {
t.Errorf("err[%d] does not match:\n\t\twant:\t%+v\n\t\tgot:\t%+v", i, want, err)
for i, want := range wants {
err := got.Errors[i]
if err != want {
t.Errorf("err[%d] does not match:\n\t\twant:\t%+v\n\t\tgot:\t%+v\n\t\tsource: %s", i, want, err, line)
}
}
}
}

// WantNoErrs verifies there are no errors returned.
func WantNoErrs(t *testing.T, got []fdk.APIError) {
t.Helper()
func WantNoErrs() WantFn {
line := src(1)
return func(t *testing.T, got fdk.Response) {
t.Helper()

if len(got) == 0 {
return
}
if len(got.Errors) == 0 {
return
}

b, _ := json.MarshalIndent(got, "", " ")
t.Errorf("recieved unexpected errors:\n\t\tgot:\t%s", string(b))
b, _ := json.MarshalIndent(got.Errors, "", " ")
t.Errorf("recieved unexpected errors:\n\t\tgot:\t%s\n\t\tsource: %s", string(b), line)
}
}

// WantStatus verifies the status matches desired.
func WantStatus(t *testing.T, want, got int) {
t.Helper()
// WantCode verifies the status matches desired.
func WantCode(want int) WantFn {
line := src(1)
return func(t *testing.T, got fdk.Response) {
t.Helper()

if want == got {
return
}
if want == got.Code {
return
}

t.Errorf("status does not match:\n\t\twant:\t%d\n\t\tgot:\t%d", want, got)
eq(t, line, want, got.Code, "Code")
}
}

// WantFile returns the file from the fdk.Response.Body field. If
// it is not a fdk.File it will fail. Additionally, this will close
// the file in t.Cleanup. Safe to ignore closing the contents in tests.
func WantFile(t *testing.T, body json.Marshaler) fdk.File {
t.Helper()
// WantFileMatch verifies the contents of the response body are
// a fdk.File type and have matching contents.
func WantFileMatch(want fdk.File) WantFn {
line := src(1)
return func(t *testing.T, got fdk.Response) {
t.Helper()

f, ok := body.(fdk.File)
if !ok {
t.Fatalf("did not receive correct type back; got:\t%t", body)
}
t.Cleanup(func() { _ = want.Contents.Close })

t.Cleanup(func() {
// ignore the error here as this may cause issues when user
// utilizes it, we just want to make sure this is closing
// at least once
_ = f.Contents.Close()
})
return f
}
f := wantFile(t, got.Body)

func WantFileMatch(t *testing.T, body json.Marshaler, want fdk.File) {
t.Helper()
eq(t, line, want.ContentType, f.ContentType, "ContentType")
eq(t, line, want.Encoding, f.Encoding, "Encoding")
eq(t, line, want.Filename, f.Filename, "Filename")

t.Cleanup(func() { _ = want.Contents.Close })
wantContents, err := io.ReadAll(want.Contents)
mustNoErr(t, err)

f := WantFile(t, body)
gotContents, err := io.ReadAll(f.Contents)
mustNoErr(t, err)

eq(t, "", want.ContentType, f.ContentType, "ContentType")
eq(t, "", want.Encoding, f.Encoding, "Encoding")
eq(t, "", want.Filename, f.Filename, "Filename")
eq(t, line, string(wantContents), string(gotContents), "Contents")
}
}

wantContents, err := io.ReadAll(want.Contents)
mustNoErr(t, err)
func src(deltas ...int) string {
skip := 1
for _, v := range deltas {
skip += v
}
// Source identifies the file:line of the callsite.
pc, file, line, _ := runtime.Caller(skip)
if skip != 1 {
pc, _, _, _ = runtime.Caller(1)
}

gotContents, err := io.ReadAll(f.Contents)
mustNoErr(t, err)
out := fmt.Sprintf("%s:%d", file, line)
if fn := runtime.FuncForPC(pc); fn != nil {
fnName := strings.TrimPrefix(fn.Name(), "github.com/CrowdStrike/foundry-fn-go/")
out += "[" + fnName + "]"
}

eq(t, "", string(wantContents), string(gotContents), "Contents")
return out
}

func eq[T comparable](t *testing.T, line string, want, got T, label string) {
Expand All @@ -106,3 +125,23 @@ func mustNoErr(t *testing.T, err error) {

t.Fatalf("recieved unexpected error:\n\t\tgot:\t%s", err)
}

// wantFile returns the file from the fdk.Response.Body field. If
// it is not a fdk.File it will fail. Additionally, this will close
// the file in t.Cleanup. Safe to ignore closing the contents in tests.
func wantFile(t *testing.T, body json.Marshaler) fdk.File {
t.Helper()

f, ok := body.(fdk.File)
if !ok {
t.Fatalf("did not receive correct type back; got:\t%t", body)
}

t.Cleanup(func() {
// ignore the error here as this may cause issues when user
// utilizes it, we just want to make sure this is closing
// at least once
_ = f.Contents.Close()
})
return f
}
101 changes: 0 additions & 101 deletions fdktest/matchers_2.go

This file was deleted.

0 comments on commit 03a9a31

Please sign in to comment.