Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests] Remove testify dependency #5762

Merged
merged 2 commits into from
Mar 16, 2023
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
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.2
github.com/tchap/go-patricia/v2 v2.3.1
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415
github.com/yashtewari/glob-intersection v0.1.0
Expand Down Expand Up @@ -73,7 +72,6 @@ require (
github.com/miekg/dns v1.1.43 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -331,18 +331,13 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tchap/go-patricia/v2 v2.3.1 h1:6rQp39lgIYZ+MHmdEq4xzuk1t7OdC35z/xm0BGhTkes=
github.com/tchap/go-patricia/v2 v2.3.1/go.mod h1:VZRHKAb53DLaG+nA9EaYYiaEx6YztwDlLElMsnSHD4k=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
Expand Down
3 changes: 2 additions & 1 deletion internal/gojsonschema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ go get github.com/xeipuuv/gojsonschema
Dependencies :
* [github.com/xeipuuv/gojsonpointer](https://github.com/xeipuuv/gojsonpointer)
* [github.com/xeipuuv/gojsonreference](https://github.com/xeipuuv/gojsonreference)
* [github.com/stretchr/testify/assert](https://github.com/stretchr/testify#assert-package)
* [github.com/stretchr/testify/assert](https://github.com/stretchr/testify#assert-package) - (Note, this dependency has
been removed to reduce dependencies in OPA)

## Usage

Expand Down
41 changes: 30 additions & 11 deletions internal/gojsonschema/format_checkers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,44 @@ package gojsonschema
import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUUIDFormatCheckerIsFormat(t *testing.T) {
checker := UUIDFormatChecker{}

assert.True(t, checker.IsFormat("01234567-89ab-cdef-0123-456789abcdef"))
assert.True(t, checker.IsFormat("f1234567-89ab-cdef-0123-456789abcdef"))
assert.True(t, checker.IsFormat("01234567-89AB-CDEF-0123-456789ABCDEF"))
assert.True(t, checker.IsFormat("F1234567-89AB-CDEF-0123-456789ABCDEF"))
if !checker.IsFormat("01234567-89ab-cdef-0123-456789abcdef") {
t.Error("Expected true, got false")
}

if !checker.IsFormat("f1234567-89ab-cdef-0123-456789abcdef") {
t.Error("Expected true, got false")
}

if !checker.IsFormat("01234567-89AB-CDEF-0123-456789ABCDEF") {
t.Error("Expected true, got false")
}
if !checker.IsFormat("F1234567-89AB-CDEF-0123-456789ABCDEF") {
t.Error("Expected true, got false")
}

assert.False(t, checker.IsFormat("not-a-uuid"))
assert.False(t, checker.IsFormat("g1234567-89ab-cdef-0123-456789abcdef"))
if checker.IsFormat("not-a-uuid") {
t.Error("Expected false, got true")
}

if checker.IsFormat("g1234567-89ab-cdef-0123-456789abcdef") {
t.Error("Expected false, got true")
}
}

func TestURIReferenceFormatCheckerIsFormat(t *testing.T) {
checker := URIReferenceFormatChecker{}

assert.True(t, checker.IsFormat("relative"))
assert.True(t, checker.IsFormat("https://dummyhost.com/dummy-path?dummy-qp-name=dummy-qp-value"))
if !checker.IsFormat("relative") {
t.Error("Expected true, got false")
}
if !checker.IsFormat("https://dummyhost.com/dummy-path?dummy-qp-name=dummy-qp-value") {
t.Error("Expected true, got false")
}
}

const formatSchema = `{
Expand Down Expand Up @@ -132,7 +149,9 @@ func TestCustomFormat(t *testing.T) {
t.Error(err)
}

assert.Len(t, invalidResult.Errors(), 5)
if len(invalidResult.Errors()) != 5 {
t.Error("Expected 5 errors, got", len(invalidResult.Errors()))
}

FormatCheckers.
Remove("ArrayChecker").
Expand Down
92 changes: 69 additions & 23 deletions internal/gojsonschema/schemaLoader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ package gojsonschema

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
)

func TestSchemaLoaderWithReferenceToAddedSchema(t *testing.T) {
Expand All @@ -28,12 +24,20 @@ func TestSchemaLoaderWithReferenceToAddedSchema(t *testing.T) {
"$id" : "http://localhost:1234/test1.json",
"type" : "integer"
}`))
if err != nil {
t.Errorf("Error adding schema: %v", err)
}

assert.Nil(t, err)
schema, err := sl.Compile(NewReferenceLoader("http://localhost:1234/test1.json"))
assert.Nil(t, err)
if err != nil {
t.Errorf("Error compiling schema: %v", err)
}

result, err := schema.Validate(NewStringLoader(`"hello"`))
assert.Nil(t, err)
if err != nil {
t.Errorf("Error validating schema: %v", err)
}

if len(result.Errors()) != 1 || result.Errors()[0].Type() != "invalid_type" {
t.Errorf("Expected invalid type erorr, instead got %v", result.Errors())
}
Expand All @@ -54,13 +58,25 @@ func TestCrossReference(t *testing.T) {

sl := NewSchemaLoader()
err := sl.AddSchema("http://localhost:1234/test2.json", schema1)
assert.Nil(t, err)
if err != nil {
t.Errorf("Error adding schema: %v", err)
}

err = sl.AddSchema("http://localhost:1234/test3.json", schema2)
assert.Nil(t, err)
if err != nil {
t.Errorf("Error adding schema: %v", err)
}

schema, err := sl.Compile(NewStringLoader(`{"$ref" : "http://localhost:1234/test2.json"}`))
assert.Nil(t, err)
if err != nil {
t.Errorf("Error compiling schema: %v", err)
}

result, err := schema.Validate(NewStringLoader(`"hello"`))
assert.Nil(t, err)
if err != nil {
t.Errorf("Error validating schema: %v", err)
}

if len(result.Errors()) != 1 || result.Errors()[0].Type() != "invalid_type" {
t.Errorf("Expected invalid type erorr, instead got %v", result.Errors())
}
Expand All @@ -70,9 +86,14 @@ func TestCrossReference(t *testing.T) {
func TestDoubleIDReference(t *testing.T) {
sl := NewSchemaLoader()
err := sl.AddSchema("http://localhost:1234/test4.json", NewStringLoader("{}"))
assert.Nil(t, err)
if err != nil {
t.Errorf("Error adding schema: %v", err)
}

err = sl.AddSchemas(NewStringLoader(`{ "$id" : "http://localhost:1234/test4.json"}`))
assert.NotNil(t, err)
if err == nil {
t.Errorf("Expected error adding schema, got none")
}
}

func TestCustomMetaSchema(t *testing.T) {
Expand All @@ -89,24 +110,34 @@ func TestCustomMetaSchema(t *testing.T) {
sl.Validate = true

err := sl.AddSchemas(loader)
assert.Nil(t, err)
if err != nil {
t.Errorf("Error adding schema: %v", err)
}

_, err = sl.Compile(NewStringLoader(`{
"$id" : "http://localhost:1234/test6.json",
"$schema" : "http://localhost:1234/test5.json",
"type" : "string"
}`))
assert.Nil(t, err)
if err != nil {
t.Errorf("Error compiling schema: %v", err)
}

sl = NewSchemaLoader()
sl.Validate = true
err = sl.AddSchemas(loader)
assert.Nil(t, err)
if err != nil {
t.Errorf("Error adding schema: %v", err)
}

_, err = sl.Compile(NewStringLoader(`{
"$id" : "http://localhost:1234/test7.json",
"$schema" : "http://localhost:1234/test5.json",
"multipleOf" : 5
}`))
assert.NotNil(t, err)
if err == nil {
t.Errorf("Expected error compiling schema, got none")
}
}

func TestSchemaDetection(t *testing.T) {
Expand All @@ -117,14 +148,18 @@ func TestSchemaDetection(t *testing.T) {

// The schema should produce an error in draft-04 mode
_, err := NewSchema(loader)
assert.NotNil(t, err)
if err == nil {
t.Errorf("Expected error, got none")
}

// With schema detection disabled the schema should not produce an error in hybrid mode
sl := NewSchemaLoader()
sl.AutoDetect = false

_, err = sl.Compile(loader)
assert.Nil(t, err)
if err != nil {
t.Errorf("Error compiling schema: %v", err)
}
}

func TestDraftCrossReferencing(t *testing.T) {
Expand Down Expand Up @@ -152,14 +187,20 @@ func TestDraftCrossReferencing(t *testing.T) {
sl.AutoDetect = b

err := sl.AddSchemas(loader1)
assert.Nil(t, err)
if err != nil {
t.Errorf("Error adding schema: %v", err)
}

_, err = sl.Compile(loader2)

// It will always fail with autodetection on as "exclusiveMinimum" : 5
// is only valid since draft-06. With autodetection off it will pass if
// draft-06 or newer is used

assert.Equal(t, err == nil, !b && draft >= Draft6)
got := !b && draft >= Draft6
if (err == nil) != got {
t.Errorf("Expected error: %v, got: %v", !got, err)
}
}
}
}
Expand All @@ -172,6 +213,11 @@ func TestParseSchemaURL_NotMap(t *testing.T) {
//WHEN
_, err := NewSchema(sl)
//THEN
require.Error(t, err)
assert.EqualError(t, err, "schema is invalid")
if err == nil {
t.Fatalf("Expected error, got none")
}

if err.Error() != "schema is invalid" {
t.Fatalf("Expected error: %s, got: %s", "schema is invalid", err.Error())
}
}
Loading