Skip to content

Commit

Permalink
Different diff package (#2)
Browse files Browse the repository at this point in the history
* remove byteskip & add vendor folder for non-go.mod package

* update json diff tool to be snyk-specific

* remove out of date comment

* add snyk check file & add full test coverage for it

* add return type test

* update error response to exit code 1 when new vulnerabilities are found by default like Snyk CLI would do

* update test case

* update workflow name
  • Loading branch information
DI-Tony-Reed authored Oct 11, 2024
1 parent c59b1bc commit 1d02b83
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 450 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
jobs:

build:
name: Go tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
[![Tests](https://github.com/DI-Tony-Reed/JSONDiff/actions/workflows/tests.yaml/badge.svg)](https://github.com/DI-Tony-Reed/JSONDiff/actions/workflows/tests.yaml)

# What is this
This tool simply accepts two JSON files and returns the difference between them. It utilizes https://github.com/go-test/deep for the comparison.
This tool accepts two JSON Snyk scans and returns the difference between them. It utilizes a slightly modified version of https://github.com/hezro/snyk-code-pr-diff for the comparison.

# How do I use this?
Locally, you can clone this repository, build it via the Makefile, and run it by feeding it two JSON files:
Locally, you can clone this repository, build it via the Makefile, and run it by feeding it two JSON Snyk scan files:
```bash
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
32 changes: 11 additions & 21 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,31 @@ package main

import (
"bytes"
"fmt"
"github.com/go-test/deep"
"errors"
)

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

func (j JSONDiff) FindDifferences() string {
func (j JSONDiff) FindDifferences() (string, error) {
if j.File1.Bytes == nil || j.File2.Bytes == nil {
return "No bytes defined for File1 and/or File2."
return "No bytes defined for File1 and/or File2.", nil
}

if j.File1.Map == nil || j.File2.Map == nil {
return "No map defined for File1 and/or File2."
return "No map defined for File1 and/or File2.", nil
}

if bytes.Equal(j.File1.Bytes, j.File2.Bytes) {
return "No differences found."
return "No differences found.", nil
}

if j.ByteSkip && len(j.File2.Bytes) < len(j.File1.Bytes) {
return "Second file smaller than first and byteskip enabled"
output, err := check(j.File1.Map, j.File2.Map)
if err != nil {
return "", err
}

if diff := deep.Equal(j.File1.Map, j.File2.Map); diff != nil {
differences := "Differences found:"
for _, d := range diff {
differences += fmt.Sprintf("\n%v", d)
}

return differences
}

return "No differences found."
return output, errors.New("new vulnerability(s) found")
}
Loading

0 comments on commit 1d02b83

Please sign in to comment.