Skip to content

Commit 074c4f2

Browse files
Build test (#34)
* workflow updation * workflow update * IND-2585 workflow updated with unit test coverage & linting * fix for handler test * errcheck corrected as done in previous code * resolved comment of using t.Fatalf * break added to newly added errors * disables * reordering of workflow * resolved comments * test fix --------- Co-authored-by: Sonam Tenzin <sonam.tenzin@hashicorp.com>
1 parent 79abdab commit 074c4f2

File tree

8 files changed

+97
-40
lines changed

8 files changed

+97
-40
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,29 @@ jobs:
1212
matrix:
1313
go: [ '1.21', '1.20', '1.19', '1.18']
1414
steps:
15-
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
15+
- name: Checkout Code
16+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
1617

17-
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
18+
- name: Setup Go
19+
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
1820
with:
1921
go-version: ${{ matrix.go }}
22+
23+
- name: Build Go
24+
run: go build ./...
2025

21-
- name: test
22-
run: go test -race . -v
26+
- name: Run golangci-lint
27+
uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5
28+
29+
- name: run test and generate coverage report
30+
run: go test -race ./... -v -coverprofile=coverage.out
31+
32+
- name: Upload coverage report
33+
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808
34+
with:
35+
path: coverage.out
36+
name: Coverage-report-${{matrix.go}}
37+
38+
- name: Display coverage report
39+
run: go tool cover -func=coverage.out
40+

errors_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
4747
var writer io.Writer = buffer
4848

4949
_ = MarshalErrors(writer, testRow.In)
50-
json.Unmarshal(buffer.Bytes(), &output)
50+
if err := json.Unmarshal(buffer.Bytes(), &output); err != nil {
51+
t.Fatalf("failed to unmarshal: %v", err)
52+
}
5153

5254
if !reflect.DeepEqual(output, testRow.Out) {
5355
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)

examples/handler_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
func TestExampleHandler_post(t *testing.T) {
1313
blog := fixtureBlogCreate(1)
1414
requestBody := bytes.NewBuffer(nil)
15-
jsonapi.MarshalOnePayloadEmbedded(requestBody, blog)
15+
if err := jsonapi.MarshalOnePayloadEmbedded(requestBody, blog); err != nil {
16+
t.Fatal(err)
17+
}
1618

1719
r, err := http.NewRequest(http.MethodPost, "/blogs?id=1", requestBody)
1820
if err != nil {
@@ -36,7 +38,9 @@ func TestExampleHandler_put(t *testing.T) {
3638
fixtureBlogCreate(3),
3739
}
3840
requestBody := bytes.NewBuffer(nil)
39-
jsonapi.MarshalPayload(requestBody, blogs)
41+
if err := jsonapi.MarshalPayload(requestBody, blogs); err != nil {
42+
t.Fatal(err)
43+
}
4044

4145
r, err := http.NewRequest(http.MethodPut, "/blogs", requestBody)
4246
if err != nil {
@@ -102,7 +106,7 @@ func TestHttpErrorWhenHeaderDoesNotMatch(t *testing.T) {
102106
}
103107

104108
func TestHttpErrorWhenMethodDoesNotMatch(t *testing.T) {
105-
r, err := http.NewRequest(http.MethodPatch, "/blogs", nil)
109+
r, err := http.NewRequest(http.MethodOptions, "/blogs", nil)
106110
if err != nil {
107111
t.Fatal(err)
108112
}

models_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ type Video struct {
246246

247247
type OneOfMedia struct {
248248
Image *Image
249-
random int
250249
Video *Video
251250
RandomStuff *string
252251
}

request.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
453453

454454
buf := bytes.NewBuffer(nil)
455455

456-
json.NewEncoder(buf).Encode(data.Relationships[args[1]])
457-
json.NewDecoder(buf).Decode(relationship)
456+
json.NewEncoder(buf).Encode(data.Relationships[args[1]]) //nolint:errcheck
457+
json.NewDecoder(buf).Decode(relationship) //nolint:errcheck
458458

459459
data := relationship.Data
460460

@@ -483,7 +483,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
483483

484484
buf := bytes.NewBuffer(nil)
485485
relDataStr := data.Relationships[args[1]]
486-
json.NewEncoder(buf).Encode(relDataStr)
486+
json.NewEncoder(buf).Encode(relDataStr) //nolint:errcheck
487487

488488
isExplicitNull := false
489489
relationshipDecodeErr := json.NewDecoder(buf).Decode(relationship)

request_test.go

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ func TestUnmarshalToStructWithPointerAttr_BadType_Struct(t *testing.T) {
249249

250250
func TestUnmarshalToStructWithPointerAttr_BadType_IntSlice(t *testing.T) {
251251
out := new(WithPointer)
252-
type FooStruct struct{ A, B int }
253252
in := map[string]interface{}{
254253
"name": []int{4, 5}, // This is the wrong type.
255254
}
@@ -405,7 +404,9 @@ func TestUnmarshalNullableRelationshipsNonNullValue(t *testing.T) {
405404
}
406405

407406
outBuf := bytes.NewBuffer(nil)
408-
json.NewEncoder(outBuf).Encode(payload)
407+
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
408+
t.Fatal(err)
409+
}
409410

410411
out := new(WithNullableAttrs)
411412

@@ -442,7 +443,9 @@ func TestUnmarshalNullableRelationshipsExplicitNullValue(t *testing.T) {
442443
}
443444

444445
outBuf := bytes.NewBuffer(nil)
445-
json.NewEncoder(outBuf).Encode(payload)
446+
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
447+
t.Fatal(err)
448+
}
446449

447450
out := new(WithNullableAttrs)
448451

@@ -467,7 +470,9 @@ func TestUnmarshalNullableRelationshipsNonExistentValue(t *testing.T) {
467470
}
468471

469472
outBuf := bytes.NewBuffer(nil)
470-
json.NewEncoder(outBuf).Encode(payload)
473+
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
474+
t.Fatal(err)
475+
}
471476

472477
out := new(WithNullableAttrs)
473478

@@ -490,7 +495,9 @@ func TestUnmarshalNullableRelationshipsNoRelationships(t *testing.T) {
490495
}
491496

492497
outBuf := bytes.NewBuffer(nil)
493-
json.NewEncoder(outBuf).Encode(payload)
498+
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
499+
t.Fatal(err)
500+
}
494501

495502
out := new(WithNullableAttrs)
496503

@@ -1028,7 +1035,7 @@ func Test_choiceStructMapping(t *testing.T) {
10281035
t.Errorf("expected \"images\" to be the first field, but got %d", imageField.FieldNum)
10291036
}
10301037
videoField, ok := result["videos"]
1031-
if !ok || videoField.FieldNum != 2 {
1038+
if !ok || videoField.FieldNum != 1 {
10321039
t.Errorf("expected \"videos\" to be the third field, but got %d", videoField.FieldNum)
10331040
}
10341041
}
@@ -1120,7 +1127,10 @@ func TestUnmarshalNestedRelationships(t *testing.T) {
11201127
}
11211128

11221129
func TestUnmarshalRelationshipsSerializedEmbedded(t *testing.T) {
1123-
out := sampleSerializedEmbeddedTestModel()
1130+
out, err := sampleSerializedEmbeddedTestModel()
1131+
if err != nil {
1132+
t.Fatal(err)
1133+
}
11241134

11251135
if out.CurrentPost == nil {
11261136
t.Fatalf("Current post was not materialized")
@@ -1169,7 +1179,10 @@ func TestUnmarshalNestedRelationshipsEmbedded(t *testing.T) {
11691179
}
11701180

11711181
func TestUnmarshalRelationshipsSideloaded(t *testing.T) {
1172-
payload := samplePayloadWithSideloaded()
1182+
payload, err := samplePayloadWithSideloaded()
1183+
if err != nil {
1184+
t.Fatal(err)
1185+
}
11731186
out := new(Blog)
11741187

11751188
if err := UnmarshalPayload(payload, out); err != nil {
@@ -1190,7 +1203,10 @@ func TestUnmarshalRelationshipsSideloaded(t *testing.T) {
11901203
}
11911204

11921205
func TestUnmarshalNestedRelationshipsSideloaded(t *testing.T) {
1193-
payload := samplePayloadWithSideloaded()
1206+
payload, err := samplePayloadWithSideloaded()
1207+
if err != nil {
1208+
t.Fatal(err)
1209+
}
11941210
out := new(Blog)
11951211

11961212
if err := UnmarshalPayload(payload, out); err != nil {
@@ -1621,7 +1637,7 @@ func samplePayload() io.Reader {
16211637
}
16221638

16231639
out := bytes.NewBuffer(nil)
1624-
json.NewEncoder(out).Encode(payload)
1640+
json.NewEncoder(out).Encode(payload) //nolint:errcheck
16251641

16261642
return out
16271643
}
@@ -1639,7 +1655,7 @@ func samplePayloadWithID() io.Reader {
16391655
}
16401656

16411657
out := bytes.NewBuffer(nil)
1642-
json.NewEncoder(out).Encode(payload)
1658+
json.NewEncoder(out).Encode(payload) //nolint:errcheck
16431659

16441660
return out
16451661
}
@@ -1654,7 +1670,7 @@ func samplePayloadWithBadTypes(m map[string]interface{}) io.Reader {
16541670
}
16551671

16561672
out := bytes.NewBuffer(nil)
1657-
json.NewEncoder(out).Encode(payload)
1673+
json.NewEncoder(out).Encode(payload) //nolint:errcheck
16581674

16591675
return out
16601676
}
@@ -1669,7 +1685,7 @@ func sampleWithPointerPayload(m map[string]interface{}) io.Reader {
16691685
}
16701686

16711687
out := bytes.NewBuffer(nil)
1672-
json.NewEncoder(out).Encode(payload)
1688+
json.NewEncoder(out).Encode(payload) //nolint:errcheck
16731689

16741690
return out
16751691
}
@@ -1684,7 +1700,7 @@ func samplePayloadWithNullableAttrs(m map[string]interface{}) io.Reader {
16841700
}
16851701

16861702
out := bytes.NewBuffer(nil)
1687-
json.NewEncoder(out).Encode(payload)
1703+
json.NewEncoder(out).Encode(payload) //nolint:errcheck
16881704

16891705
return out
16901706
}
@@ -1757,23 +1773,29 @@ func testModel() *Blog {
17571773
}
17581774
}
17591775

1760-
func samplePayloadWithSideloaded() io.Reader {
1776+
func samplePayloadWithSideloaded() (io.Reader, error) {
17611777
testModel := testModel()
17621778

17631779
out := bytes.NewBuffer(nil)
1764-
MarshalPayload(out, testModel)
1780+
if err := MarshalPayload(out, testModel); err != nil {
1781+
return nil, err
1782+
}
17651783

1766-
return out
1784+
return out, nil
17671785
}
17681786

1769-
func sampleSerializedEmbeddedTestModel() *Blog {
1787+
func sampleSerializedEmbeddedTestModel() (*Blog, error) {
17701788
out := bytes.NewBuffer(nil)
1771-
MarshalOnePayloadEmbedded(out, testModel())
1789+
if err := MarshalOnePayloadEmbedded(out, testModel()); err != nil {
1790+
return nil, err
1791+
}
17721792

17731793
blog := new(Blog)
1774-
UnmarshalPayload(out, blog)
1794+
if err := UnmarshalPayload(out, blog); err != nil {
1795+
return nil, err
1796+
}
17751797

1776-
return blog
1798+
return blog, nil
17771799
}
17781800

17791801
func TestUnmarshalNestedStructPtr(t *testing.T) {

response_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ func TestMarshalPayload(t *testing.T) {
1818

1919
// One
2020
out1 := bytes.NewBuffer(nil)
21-
MarshalPayload(out1, book)
21+
if err := MarshalPayload(out1, book); err != nil {
22+
t.Fatal(err)
23+
}
2224

2325
if err := json.Unmarshal(out1.Bytes(), &jsonData); err != nil {
2426
t.Fatal(err)
@@ -29,7 +31,9 @@ func TestMarshalPayload(t *testing.T) {
2931

3032
// Many
3133
out2 := bytes.NewBuffer(nil)
32-
MarshalPayload(out2, books)
34+
if err := MarshalPayload(out2, books); err != nil {
35+
t.Fatal(err)
36+
}
3337

3438
if err := json.Unmarshal(out2.Bytes(), &jsonData); err != nil {
3539
t.Fatal(err)
@@ -936,7 +940,9 @@ func TestMarshal_Times(t *testing.T) {
936940
}
937941
// Use the standard JSON library to traverse the genereated JSON payload.
938942
data := map[string]interface{}{}
939-
json.Unmarshal(out.Bytes(), &data)
943+
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
944+
t.Fatal(err)
945+
}
940946
if tc.verification != nil {
941947
if err := tc.verification(data); err != nil {
942948
t.Fatal(err)
@@ -1017,7 +1023,9 @@ func TestNullableRelationship(t *testing.T) {
10171023

10181024
// Use the standard JSON library to traverse the genereated JSON payload.
10191025
data := map[string]interface{}{}
1020-
json.Unmarshal(out.Bytes(), &data)
1026+
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
1027+
t.Fatal(err)
1028+
}
10211029
if tc.verification != nil {
10221030
if err := tc.verification(data); err != nil {
10231031
t.Fatal(err)
@@ -1114,7 +1122,9 @@ func TestNullableAttr_Time(t *testing.T) {
11141122
}
11151123
// Use the standard JSON library to traverse the genereated JSON payload.
11161124
data := map[string]interface{}{}
1117-
json.Unmarshal(out.Bytes(), &data)
1125+
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
1126+
t.Fatal(err)
1127+
}
11181128
if tc.verification != nil {
11191129
if err := tc.verification(data); err != nil {
11201130
t.Fatal(err)
@@ -1184,7 +1194,9 @@ func TestNullableAttr_Bool(t *testing.T) {
11841194
}
11851195
// Use the standard JSON library to traverse the genereated JSON payload.
11861196
data := map[string]interface{}{}
1187-
json.Unmarshal(out.Bytes(), &data)
1197+
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
1198+
t.Fatal(err)
1199+
}
11881200
if tc.verification != nil {
11891201
if err := tc.verification(data); err != nil {
11901202
t.Fatal(err)

runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error {
7777

7878
// UnmarshalManyPayload has docs in request.go for UnmarshalManyPayload.
7979
func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elems []interface{}, err error) {
80-
r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
80+
r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error { //nolint:errcheck
8181
elems, err = UnmarshalManyPayload(reader, kind)
8282
return err
8383
})

0 commit comments

Comments
 (0)