Skip to content

Commit

Permalink
[tests] Remove testify dependency (#5762)
Browse files Browse the repository at this point in the history
We have testify in our go.mod. This sometimes misleads contributors (including myself!) to think that we can use testify in test code. Since testify is a common testing package, many go developers default to using it.

I'm not sure if we want to merge this as we might rather leave these vendored packages untouched, but it was 10 minutes of work and I was interested to see if we can avoid issues like this in future:

* #5753 (comment)
* #5447 (comment)
* #3952 (review)

Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 authored Mar 16, 2023
1 parent 9474b8f commit ec4a11e
Show file tree
Hide file tree
Showing 31 changed files with 164 additions and 9,381 deletions.
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

0 comments on commit ec4a11e

Please sign in to comment.