From bbf3bb2d12ec8ddcf7c3fffcc6cb640ceeebd98a Mon Sep 17 00:00:00 2001 From: tonyreed Date: Thu, 10 Oct 2024 14:43:24 -0500 Subject: [PATCH] add new flag to skip check if file 2 is fewer bytes than file 1 --- README.md | 8 +++++++- compare.go | 9 +++++++-- compare_test.go | 23 +++++++++++++++++++++-- runner.go | 9 ++++++--- runner_test.go | 16 ++++++++++++++++ 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f07ba7b..7ae3d62 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,15 @@ Locally, you can clone this repository, build it via the Makefile, and run it by make build ./bin/jsonDiff-darwin json1.json json2.json ``` - Make sure to use the appropriate binary for your OS. The above example is assuming you are on a Mac. +## Additional CLI flags +You can also use the `--byteskip` to skip the comparison if the second file has fewer bytes than the first. +```bash +./bin/jsonDiff-darwin json1.json json2.json --byteskip +``` + + # Viewing test coverage ```bash make tests-coverage && open coverage.html diff --git a/compare.go b/compare.go index 826a6b8..284c37b 100644 --- a/compare.go +++ b/compare.go @@ -7,8 +7,9 @@ import ( ) type JSONDiff struct { - File1 File - File2 File + File1 File + File2 File + ByteSkip bool } func (j JSONDiff) FindDifferences() string { @@ -24,6 +25,10 @@ func (j JSONDiff) FindDifferences() string { return "No differences found." } + if j.ByteSkip && len(j.File2.Bytes) < len(j.File1.Bytes) { + return "Second file smaller than first and byteskip enabled" + } + if diff := deep.Equal(j.File1.Map, j.File2.Map); diff != nil { differences := "Differences found:" for _, d := range diff { diff --git a/compare_test.go b/compare_test.go index e857a16..f04c55c 100644 --- a/compare_test.go +++ b/compare_test.go @@ -391,7 +391,7 @@ func TestJSONDiff_FindDifferencesWithSnykShape_Vulnerabilities(t *testing.T) { } } -func TestJSONDiff_FindDifference_NoBytes(t *testing.T) { +func TestJSONDiff_FindDifferences_NoBytes(t *testing.T) { j := JSONDiff{ File1: File{ Bytes: nil, @@ -408,7 +408,7 @@ func TestJSONDiff_FindDifference_NoBytes(t *testing.T) { } } -func TestJSONDiff_FindDifference_NoMap(t *testing.T) { +func TestJSONDiff_FindDifferences_NoMap(t *testing.T) { j := JSONDiff{ File1: File{ Bytes: []byte(`{"key1": "value1"}`), @@ -424,3 +424,22 @@ func TestJSONDiff_FindDifference_NoMap(t *testing.T) { t.Errorf("JSONDiff.FindDifferences() = %v, want %v", got, "No map defined for File1 and/or File2.") } } + +func TestJSONDiff_FindDIfferences_ByteSkip(t *testing.T) { + j := JSONDiff{ + File1: File{ + Bytes: []byte(`{"key1": "value1"}, {"key2": "value2"}`), + Map: map[string]interface{}{"key1": "value1", "key2": "value2"}, + }, + File2: File{ + Bytes: []byte(`{"key1": "value1"}`), + Map: map[string]interface{}{"key1": "value1"}, + }, + ByteSkip: true, + } + + expected := "Second file smaller than first and byteskip enabled" + if got := j.FindDifferences(); got != expected { + t.Errorf("JSONDiff.FindDifferences() = %v, want %v", got, expected) + } +} diff --git a/runner.go b/runner.go index 63284f4..7569467 100644 --- a/runner.go +++ b/runner.go @@ -14,12 +14,11 @@ type Runner struct { func (r Runner) Run(reader FileReader) (string, error) { if len(r.Arguments) < 2 { return "", errors.New("please provide two JSON files to compare") - } - args := r.Arguments[0:2] + fileArgs := r.Arguments[0:2] var files []File - for _, arg := range args { + for _, arg := range fileArgs { file := File{ Reader: reader, } @@ -35,5 +34,9 @@ func (r Runner) Run(reader FileReader) (string, error) { File2: files[1], } + if (len(r.Arguments) == 3) && (r.Arguments[2] == "--byteskip") { + comparator.ByteSkip = true + } + return comparator.FindDifferences(), nil } diff --git a/runner_test.go b/runner_test.go index ec94fec..38a2dc9 100644 --- a/runner_test.go +++ b/runner_test.go @@ -66,3 +66,19 @@ func TestRunner_Run_InvalidFiles(t *testing.T) { t.Errorf("Expected error, but got did not get one") } } + +func TestRunner_Run_ByteSkip(t *testing.T) { + mockFileReader := MockFileReader{ + Content: []byte(`{"key1": "value1"}`), + Err: nil, + } + + runner := Runner{ + Arguments: []string{"file1.json", "file2.json", "--byteskip"}, + } + + _, err := runner.Run(mockFileReader) + if err != nil { + t.Fatalf("Expected no error, but got %v", err) + } +}