From 81e5ec990608f2b2cf34897413dd941bd0e03076 Mon Sep 17 00:00:00 2001 From: Tony Reed <116904476+DI-Tony-Reed@users.noreply.github.com> Date: Fri, 11 Oct 2024 14:59:02 -0500 Subject: [PATCH] Update response (#3) * don't error if no output was returned from snyk scan diff * update coverage --- compare.go | 4 ++++ compare_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/compare.go b/compare.go index 9998013..3c66f2b 100644 --- a/compare.go +++ b/compare.go @@ -28,5 +28,9 @@ func (j JSONDiff) FindDifferences() (string, error) { return "", err } + if len(output) == 0 { + return "No differences found.", nil + } + return output, errors.New("new vulnerability(s) found") } diff --git a/compare_test.go b/compare_test.go index c9adf7f..6ce06d5 100644 --- a/compare_test.go +++ b/compare_test.go @@ -79,7 +79,59 @@ func TestJSONDiff_FindDifferences(t *testing.T) { } output, err := j.FindDifferences() - if err == nil || output != "" { + if err != nil || output != "No differences found." { t.Errorf("JSONDiff.FindDifferences() error = %v", err) } } + +func TestJSONDiff_FindDifferences_Error(t *testing.T) { + j := JSONDiff{ + File1: File{ + Bytes: []byte(`{"runs": "value1"}`), + Map: map[string]interface{}{ + "runs": []interface{}{ + map[string]interface{}{ + "results": []interface{}{}, + }, + }, + }, + }, + File2: File{ + Bytes: []byte(`{"runs": "value2"}`), + Map: map[string]interface{}{ + "runs": []interface{}{ + map[string]interface{}{ + "results": []interface{}{ + map[string]interface{}{ + "fingerprints": map[string]interface{}{ + "identity": "67890", + }, + "level": "error", + "message": map[string]interface{}{ + "text": "New Issue", + }, + "locations": []interface{}{ + map[string]interface{}{ + "physicalLocation": map[string]interface{}{ + "artifactLocation": map[string]interface{}{ + "uri": "file2.go", + }, + "region": map[string]interface{}{ + "startLine": 20.0, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + } + + output, err := j.FindDifferences() + if err == nil || len(output) == 0 { + t.Errorf("JSONDiff.FindDifferences() wanted error, got = %v and output %v", err, output) + } +}