Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
)

type JSONDiff struct {
File1 File
File2 File
File1 File
File2 File
ByteSkip bool
}

func (j JSONDiff) FindDifferences() string {
Expand All @@ -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 {
Expand Down
23 changes: 21 additions & 2 deletions compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"}`),
Expand All @@ -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)
}
}
9 changes: 6 additions & 3 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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
}
16 changes: 16 additions & 0 deletions runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}