Skip to content

Commit

Permalink
Merge pull request #4 from ShiftLeftSecurity/chipaca/unified-diff
Browse files Browse the repository at this point in the history
support optional unified diffs
  • Loading branch information
chipaca authored Sep 29, 2023
2 parents d36d50e + b389fcf commit f1a0917
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Setup go
uses: actions/setup-go@v2
with:
go-version: 1.17.5
go-version: 1.21
- name: Install dependencies
run: |
go install golang.org/x/lint/golint@latest
Expand All @@ -27,7 +27,7 @@ jobs:
strategy:
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
go: ['1.17.5']
go: ['1.17.5', '1.19', '1.21']
name: Test using go ${{ matrix.go }} on ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ In some cases, attributes in a JSON response can by dynamic (e.g unique id's, da
When used with `AssertHTTPResponse`, for any response with `Content-Type: application/json`, the key-value pairs in `defaults` will be used to override the JSON response, allowing for consistent snapshot testing. Any HTTP headers will also be override for key matches in `defaults`.


## Unified diffs

In some situations the default diff generated by `diffmatchpatch` is too verbose, or whitespace or other aspects of the text might be making the difference between added and removed fragments hard to spot. In these situations you can ask abide to generate unified diffs (using `gotextdiff`) by setting `unified-diff` to `true` in the `abide.json` of the package or project that needs it:

```json
{
"unified-diff": true
}
```

## Using custom `__snapshot__` directory

To write snapshots to a directory other than the default `__snapshot__`, adjust `abide.SnapshotDir` before any call to an Assert function. See `example/models` package for a working example
Expand Down
18 changes: 16 additions & 2 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"strings"
"testing"

"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"github.com/sergi/go-diff/diffmatchpatch"
)

Expand Down Expand Up @@ -56,7 +59,7 @@ func createOrUpdateSnapshot(t *testing.T, id, data string, format SnapshotType)
case SnapshotHTTPRespJSON:
diff = compareResultsHTTPRequestJSON(t, snapshot.value, strings.TrimSpace(data))
default:
diff = compareResults(t, snapshot.value, strings.TrimSpace(data))
diff = compareResults(t, id, snapshot.value, strings.TrimSpace(data))
}

if diff != "" {
Expand All @@ -74,7 +77,18 @@ func createOrUpdateSnapshot(t *testing.T, id, data string, format SnapshotType)
}
}

func compareResults(t *testing.T, existing, new string) string {
func compareResults(t *testing.T, id, existing, new string) string {
c, err := getConfig()
if err != nil {
t.Fatal(err)
}

if c != nil && c.UnifiedDiff {
edits := myers.ComputeEdits(span.URIFromPath(id), existing, new)
diff := gotextdiff.ToUnified("a.txt", "b.txt", existing, edits)
return fmt.Sprint(diff)
}

dmp := diffmatchpatch.New()
dmp.PatchMargin = 20
allDiffs := dmp.DiffMain(existing, new, false)
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const (
)

type config struct {
Defaults map[string]interface{} `json:"defaults"`
UnifiedDiff bool `json:"unified-diff"`
Defaults map[string]interface{} `json:"defaults"`
}

func getConfig() (*config, error) {
Expand Down
39 changes: 21 additions & 18 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,43 @@
// and enable broader coverage of http APIs. When included in version control
// it can provide a historical log of API and application changes over time.
//
// Snapshot
// # Snapshot
//
// A snapshot is essentially a lockfile representing an http response.
// /* snapshot: api endpoint */
// HTTP/1.1 200 OK
// Connection: close
// Content-Type: application/json
//
// {
// "foo": "bar"
// }
// /* snapshot: api endpoint */
// HTTP/1.1 200 OK
// Connection: close
// Content-Type: application/json
//
// {
// "foo": "bar"
// }
//
// In addition to testing `http.Response`, abide provides methods for testing
// `io.Reader` and any object that implements `Assertable`.
//
// Snapshots are saved in a directory named __snapshots__ at the root of the package.
// These files are intended to be saved and included in version control.
//
// Creating a Snapshot
// # Creating a Snapshot
//
// Snapshots are automatically generated during the initial test run. For example
// this will create a snapshot identified by "example" for this http.Response.
// func TestFunction(t *testing.T) {
// req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
// w := httptest.NewRecorder()
// handler(w, req)
// res := w.Result()
// abide.AssertHTTPResponse(t, "example", res)
// }
//
// Comparing and Updating
// func TestFunction(t *testing.T) {
// req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
// w := httptest.NewRecorder()
// handler(w, req)
// res := w.Result()
// abide.AssertHTTPResponse(t, "example", res)
// }
//
// # Comparing and Updating
//
// In subsequent test runs the existing snapshot is compared to the new results.
// In the event they do not match, the test will fail, and the diff will be printed.
// If the change was intentional, the snapshot can be updated.
// $ go test -- -u
//
// $ go test -- -u
package abide
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ require (
github.com/sergi/go-diff v1.2.0
)

require github.com/hexops/gotextdiff v1.0.3

replace github.com/beme/abide => ./
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand Down

0 comments on commit f1a0917

Please sign in to comment.