Skip to content

Commit

Permalink
Add FailNotEqualMarshal, update deps and CI (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed May 16, 2022
1 parent 0b84650 commit 9535626
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 60 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/cloc.yml
Expand Up @@ -2,6 +2,12 @@
name: cloc
on:
pull_request:

# Cancel the workflow in progress in newer build is about to start.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
cloc:
runs-on: ubuntu-latest
Expand All @@ -18,14 +24,14 @@ jobs:
- name: Count Lines Of Code
id: loc
run: |
curl -OL https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
OUTPUT="${OUTPUT//'%'/'%25'}"
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
echo "${OUTPUT}"
OUTPUT="${OUTPUT//$'\n'/%0A}"
echo "::set-output name=diff::$OUTPUT"
- name: Comment Code Lines
continue-on-error: true
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
13 changes: 11 additions & 2 deletions .github/workflows/golangci-lint.yml
Expand Up @@ -8,17 +8,26 @@ on:
- master
- main
pull_request:

# Cancel the workflow in progress in newer build is about to start.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
golangci:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.18.x
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2.5.2
uses: golangci/golangci-lint-action@v3.1.0
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.41.1
version: v1.45.2

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
36 changes: 24 additions & 12 deletions .github/workflows/gorelease.yml
Expand Up @@ -2,19 +2,32 @@
name: gorelease
on:
pull_request:

# Cancel the workflow in progress in newer build is about to start.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

env:
GO111MODULE: "on"
GO_VERSION: 1.18.x
jobs:
gorelease:
strategy:
matrix:
go-version: [ 1.17.x ]
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
- name: Install Go stable
if: env.GO_VERSION != 'tip'
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
go-version: ${{ env.GO_VERSION }}
- name: Install Go tip
if: env.GO_VERSION == 'tip'
run: |
curl -sL https://storage.googleapis.com/go-build-snap/go/linux-amd64/$(git ls-remote https://github.com/golang/go.git HEAD | awk '{print $1;}').tar.gz -o gotip.tar.gz
ls -lah gotip.tar.gz
mkdir -p ~/sdk/gotip
tar -C ~/sdk/gotip -xzf gotip.tar.gz
~/sdk/gotip/bin/go version
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v2
- name: Gorelease cache
Expand All @@ -28,17 +41,16 @@ jobs:
run: |
test -e ~/go/bin/gorelease || go install golang.org/x/exp/cmd/gorelease@latest
OUTPUT=$(gorelease || exit 0)
OUTPUT="${OUTPUT//'%'/'%25'}"
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
echo "${OUTPUT}"
OUTPUT="${OUTPUT//$'\n'/%0A}"
echo "::set-output name=report::$OUTPUT"
- name: Comment Report
continue-on-error: true
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
header: gorelease
message: |
### API Changes
### Go API Changes
<pre>
${{ steps.gorelease.outputs.report }}
Expand Down
21 changes: 14 additions & 7 deletions .github/workflows/release-assets.yml
Expand Up @@ -8,19 +8,26 @@ on:
- created
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

GO_VERSION: 1.18.x
jobs:
build:
name: Upload Release Assets
strategy:
matrix:
go-version: [ 1.17.x ]
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
- name: Install Go stable
if: env.GO_VERSION != 'tip'
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
go-version: ${{ env.GO_VERSION }}
- name: Install Go tip
if: env.GO_VERSION == 'tip'
run: |
curl -sL https://storage.googleapis.com/go-build-snap/go/linux-amd64/$(git ls-remote https://github.com/golang/go.git HEAD | awk '{print $1;}').tar.gz -o gotip.tar.gz
ls -lah gotip.tar.gz
mkdir -p ~/sdk/gotip
tar -C ~/sdk/gotip -xzf gotip.tar.gz
~/sdk/gotip/bin/go version
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v2
- name: Build artifacts
Expand Down
41 changes: 30 additions & 11 deletions .github/workflows/test-unit.yml
Expand Up @@ -6,20 +6,37 @@ on:
- master
- main
pull_request:

# Cancel the workflow in progress in newer build is about to start.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

env:
GO111MODULE: "on"
RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing.
COV_GO_VERSION: 1.18.x # Version of Go to collect coverage
jobs:
test:
strategy:
matrix:
go-version: [ 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x ]
go-version: [ 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x ]
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
- name: Install Go stable
if: matrix.go-version != 'tip'
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Install Go tip
if: matrix.go-version == 'tip'
run: |
curl -sL https://storage.googleapis.com/go-build-snap/go/linux-amd64/$(git ls-remote https://github.com/golang/go.git HEAD | awk '{print $1;}').tar.gz -o gotip.tar.gz
ls -lah gotip.tar.gz
mkdir -p ~/sdk/gotip
tar -C ~/sdk/gotip -xzf gotip.tar.gz
~/sdk/gotip/bin/go version
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v2
- name: Go cache
Expand All @@ -35,21 +52,22 @@ jobs:
restore-keys: |
${{ runner.os }}-go-cache
- name: Restore base test coverage
if: matrix.go-version == '1.17.x'
id: base-coverage
if: matrix.go-version == env.COV_GO_VERSION
uses: actions/cache@v2
with:
path: |
unit-base.txt
# Use base sha for PR or new commit hash for master/main push in test result key.
key: ${{ runner.os }}-unit-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
- name: Checkout base code
if: matrix.go-version == '1.17.x' && env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
if: matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.base.sha }}
path: __base
- name: Run test for base code
if: matrix.go-version == '1.17.x' && env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
if: matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
run: |
cd __base
make | grep test-unit && (make test-unit && go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt) || echo "No test-unit in base"
Expand All @@ -59,17 +77,18 @@ jobs:
make test-unit
go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > unit.txt
OUTPUT=$(test -e unit-base.txt && (diff unit-base.txt unit.txt || exit 0) || cat unit.txt)
OUTPUT="${OUTPUT//'%'/'%25'}"
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
echo "${OUTPUT}"
OUTPUT="${OUTPUT//$'\n'/%0A}"
TOTAL=$(grep 'total:' unit.txt)
echo "${TOTAL}"
echo "::set-output name=diff::$OUTPUT"
echo "::set-output name=total::$TOTAL"
- name: Store base coverage
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
run: cp unit.txt unit-base.txt
- name: Comment Test Coverage
if: matrix.go-version == '1.17.x'
continue-on-error: true
if: matrix.go-version == env.COV_GO_VERSION
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -85,7 +104,7 @@ jobs:
</details>
- name: Upload code coverage
if: matrix.go-version == '1.17.x'
if: matrix.go-version == env.COV_GO_VERSION
uses: codecov/codecov-action@v1
with:
file: ./unit.coverprofile
Expand Down
4 changes: 4 additions & 0 deletions .golangci.yml
Expand Up @@ -38,6 +38,10 @@ linters:
- scopelint # deprecated
- ifshort # too many false positives
- golint # deprecated
- varnamelen
- tagliatelle
- errname
- ireturn

issues:
exclude-use-default: false
Expand Down
9 changes: 5 additions & 4 deletions Makefile
@@ -1,7 +1,4 @@
# Override in app Makefile to control build target, example BUILD_PKG=./cmd/my-app
BUILD_PKG ?= ./cmd/jsoncompact

#GOLANGCI_LINT_VERSION := "v1.41.1" # Optional configuration to pinpoint golangci-lint version.
#GOLANGCI_LINT_VERSION := "v1.45.2" # Optional configuration to pinpoint golangci-lint version.

# The head of Makefile determines location of dev-go to include standard targets.
GO ?= go
Expand Down Expand Up @@ -30,6 +27,10 @@ ifeq ($(DEVGO_PATH),)
endif
endif

# Override in app Makefile to control build target, example BUILD_PKG=./cmd/my-app
BUILD_PKG ?= ./cmd/jsoncompact


-include $(DEVGO_PATH)/makefiles/main.mk
-include $(DEVGO_PATH)/makefiles/lint.mk
-include $(DEVGO_PATH)/makefiles/test-unit.mk
Expand Down
15 changes: 15 additions & 0 deletions equal.go
Expand Up @@ -180,6 +180,11 @@ func FailNotEqual(expected, actual []byte) error {
return defaultComparer.FailNotEqual(expected, actual)
}

// FailNotEqualMarshal returns error if expected JSON payload is not equal to marshaled actual value.
func FailNotEqualMarshal(expected []byte, actualValue interface{}) error {
return defaultComparer.FailNotEqualMarshal(expected, actualValue)
}

func (c Comparer) filterExpected(expected []byte) ([]byte, error) {
if c.Vars != nil {
for k, v := range c.Vars.GetAll() {
Expand Down Expand Up @@ -220,6 +225,16 @@ func (c Comparer) compare(expDecoded, actDecoded interface{}) (gojsondiff.Diff,
return nil, nil
}

// FailNotEqualMarshal returns error if expected JSON payload is not equal to marshaled actual value.
func (c Comparer) FailNotEqualMarshal(expected []byte, actualValue interface{}) error {
actual, err := MarshalIndentCompact(actualValue, "", " ", 80)
if err != nil {
return err
}

return c.FailNotEqual(expected, actual)
}

// FailNotEqual returns error if JSON payloads are different, nil otherwise.
func (c Comparer) FailNotEqual(expected, actual []byte) error {
var expDecoded, actDecoded interface{}
Expand Down
13 changes: 13 additions & 0 deletions equal_test.go
Expand Up @@ -161,6 +161,19 @@ func TestEqualMarshal(t *testing.T) {
assertjson.EqualMarshal(t, []byte(`{"a":123,"b":"abc"}`), v)
}

func TestFailNotEqualMarshal(t *testing.T) {
v := struct {
A int `json:"a"`
B string `json:"b"`
}{
A: 123,
B: "abc",
}

err := assertjson.FailNotEqualMarshal([]byte(`{"a":123,"b":"abc"}`), v)
assert.NoError(t, err)
}

func TestComparer_Equal_vars(t *testing.T) {
v := &shared.Vars{}
v.Set("$varB", []int{1, 2, 3})
Expand Down
19 changes: 12 additions & 7 deletions go.mod
@@ -1,19 +1,24 @@
module github.com/swaggest/assertjson

go 1.11
go 1.17

require (
github.com/bool64/dev v0.1.41
github.com/bool64/shared v0.1.3
github.com/bool64/dev v0.2.10
github.com/bool64/shared v0.1.4
github.com/iancoleman/orderedmap v0.2.0
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/stretchr/testify v1.4.0
github.com/yosuke-furukawa/json5 v0.1.2-0.20201207051438-cf7bb3f354ff
github.com/yudai/gojsondiff v1.0.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/onsi/ginkgo v1.15.2 // indirect
github.com/onsi/gomega v1.11.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/stretchr/testify v1.4.0
github.com/yosuke-furukawa/json5 v0.1.2-0.20201207051438-cf7bb3f354ff
github.com/yudai/gojsondiff v1.0.0
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/yudai/pp v2.0.1+incompatible // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

0 comments on commit 9535626

Please sign in to comment.