Skip to content

Commit 977993f

Browse files
authored
GODRIVER-2179 Unify testing API. (#1137)
* Removes the testify dependency. * Removes the tidwall/pretty dependency. * Modifies the existing internal/assert package to be a copy of a subset of testify/assert and pmezard/go-difflib. * Adds a new internal/require package that is a copy of a subset of testify/require. * Changes all assert and require imports to refer to internal. * Runs go mod tidy and go mod vendor. * Fixes tests that relied on old test API behavior.
1 parent 0e5ae89 commit 977993f

File tree

182 files changed

+4354
-18948
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+4354
-18948
lines changed

THIRD-PARTY-NOTICES

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -716,31 +716,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
716716
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
717717
SOFTWARE.
718718

719-
----------------------------------------------------------------------
720-
License notice for github.com/tidwall/pretty
721-
----------------------------------------------------------------------
722-
723-
The MIT License (MIT)
724-
725-
Copyright (c) 2017 Josh Baker
726-
727-
Permission is hereby granted, free of charge, to any person obtaining a copy of
728-
this software and associated documentation files (the "Software"), to deal in
729-
the Software without restriction, including without limitation the rights to
730-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
731-
the Software, and to permit persons to whom the Software is furnished to do so,
732-
subject to the following conditions:
733-
734-
The above copyright notice and this permission notice shall be included in all
735-
copies or substantial portions of the Software.
736-
737-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
738-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
739-
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
740-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
741-
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
742-
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
743-
744719
----------------------------------------------------------------------
745720
License notice for github.com/xdg-go/pbkdf2
746721
----------------------------------------------------------------------

benchmark/harness.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"testing"
1212
"time"
1313

14-
"github.com/stretchr/testify/require"
14+
"go.mongodb.org/mongo-driver/internal/require"
1515
)
1616

1717
const (

bson/bson_corpus_spec_test.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package bson
88

99
import (
10+
"bytes"
1011
"encoding/hex"
1112
"encoding/json"
1213
"fmt"
@@ -20,10 +21,9 @@ import (
2021
"unicode/utf8"
2122

2223
"github.com/google/go-cmp/cmp"
23-
"github.com/stretchr/testify/require"
24-
"github.com/tidwall/pretty"
2524
"go.mongodb.org/mongo-driver/bson/primitive"
26-
"go.mongodb.org/mongo-driver/internal/testutil/assert"
25+
"go.mongodb.org/mongo-driver/internal/assert"
26+
"go.mongodb.org/mongo-driver/internal/require"
2727
)
2828

2929
type testCase struct {
@@ -312,7 +312,9 @@ func runTest(t *testing.T, file string) {
312312
expectNoError(t, err, fmt.Sprintf("%s: reading canonical BSON", v.Description))
313313

314314
// get canonical extended JSON
315-
cEJ := unescapeUnicode(string(pretty.Ugly([]byte(v.CanonicalExtJSON))), test.BsonType)
315+
var compactEJ bytes.Buffer
316+
require.NoError(t, json.Compact(&compactEJ, []byte(v.CanonicalExtJSON)))
317+
cEJ := unescapeUnicode(compactEJ.String(), test.BsonType)
316318
if test.BsonType == "0x01" {
317319
cEJ = normalizeCanonicalDouble(t, *test.TestKey, cEJ)
318320
}
@@ -328,7 +330,9 @@ func runTest(t *testing.T, file string) {
328330

329331
// native_to_relaxed_extended_json(bson_to_native(cB)) = rEJ (if rEJ exists)
330332
if v.RelaxedExtJSON != nil {
331-
rEJ := unescapeUnicode(string(pretty.Ugly([]byte(*v.RelaxedExtJSON))), test.BsonType)
333+
var compactEJ bytes.Buffer
334+
require.NoError(t, json.Compact(&compactEJ, []byte(*v.RelaxedExtJSON)))
335+
rEJ := unescapeUnicode(compactEJ.String(), test.BsonType)
332336
if test.BsonType == "0x01" {
333337
rEJ = normalizeRelaxedDouble(t, *test.TestKey, rEJ)
334338
}
@@ -368,7 +372,9 @@ func runTest(t *testing.T, file string) {
368372

369373
/*** degenerate JSON round-trip tests (if exists) ***/
370374
if v.DegenerateExtJSON != nil {
371-
dEJ := unescapeUnicode(string(pretty.Ugly([]byte(*v.DegenerateExtJSON))), test.BsonType)
375+
var compactEJ bytes.Buffer
376+
require.NoError(t, json.Compact(&compactEJ, []byte(*v.DegenerateExtJSON)))
377+
dEJ := unescapeUnicode(compactEJ.String(), test.BsonType)
372378
if test.BsonType == "0x01" {
373379
dEJ = normalizeCanonicalDouble(t, *test.TestKey, dEJ)
374380
}
@@ -506,11 +512,18 @@ func TestRelaxedUUIDValidation(t *testing.T) {
506512

507513
for _, tc := range testCases {
508514
t.Run(tc.description, func(t *testing.T) {
509-
// get canonical extended JSON
510-
cEJ := unescapeUnicode(string(pretty.Ugly([]byte(tc.canonicalExtJSON))), "0x05")
515+
// get canonical extended JSON (if provided)
516+
cEJ := ""
517+
if tc.canonicalExtJSON != "" {
518+
var compactCEJ bytes.Buffer
519+
require.NoError(t, json.Compact(&compactCEJ, []byte(tc.canonicalExtJSON)))
520+
cEJ = unescapeUnicode(compactCEJ.String(), "0x05")
521+
}
511522

512523
// get degenerate extended JSON
513-
dEJ := unescapeUnicode(string(pretty.Ugly([]byte(tc.degenerateExtJSON))), "0x05")
524+
var compactDEJ bytes.Buffer
525+
require.NoError(t, json.Compact(&compactDEJ, []byte(tc.degenerateExtJSON)))
526+
dEJ := unescapeUnicode(compactDEJ.String(), "0x05")
514527

515528
// convert dEJ to native doc
516529
var doc D

bson/bson_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"go.mongodb.org/mongo-driver/bson/bsoncodec"
2020
"go.mongodb.org/mongo-driver/bson/bsonoptions"
2121
"go.mongodb.org/mongo-driver/bson/bsontype"
22-
"go.mongodb.org/mongo-driver/internal/testutil/assert"
22+
"go.mongodb.org/mongo-driver/internal/assert"
2323
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2424
)
2525

bson/bsoncodec/cond_addr_codec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
"go.mongodb.org/mongo-driver/bson/bsonrw"
1414
"go.mongodb.org/mongo-driver/bson/bsonrw/bsonrwtest"
15-
"go.mongodb.org/mongo-driver/internal/testutil/assert"
15+
"go.mongodb.org/mongo-driver/internal/assert"
1616
)
1717

1818
func TestCondAddrCodec(t *testing.T) {

bson/bsoncodec/default_value_decoders_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"go.mongodb.org/mongo-driver/bson/bsonrw/bsonrwtest"
2424
"go.mongodb.org/mongo-driver/bson/bsontype"
2525
"go.mongodb.org/mongo-driver/bson/primitive"
26-
"go.mongodb.org/mongo-driver/internal/testutil/assert"
26+
"go.mongodb.org/mongo-driver/internal/assert"
2727
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2828
)
2929

bson/bsoncodec/registry_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/google/go-cmp/cmp"
1414
"go.mongodb.org/mongo-driver/bson/bsonrw"
1515
"go.mongodb.org/mongo-driver/bson/bsontype"
16-
"go.mongodb.org/mongo-driver/internal/testutil/assert"
16+
"go.mongodb.org/mongo-driver/internal/assert"
1717
)
1818

1919
func TestRegistry(t *testing.T) {

bson/bsoncodec/string_codec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"go.mongodb.org/mongo-driver/bson/bsonrw/bsonrwtest"
1515
"go.mongodb.org/mongo-driver/bson/bsontype"
1616
"go.mongodb.org/mongo-driver/bson/primitive"
17-
"go.mongodb.org/mongo-driver/internal/testutil/assert"
17+
"go.mongodb.org/mongo-driver/internal/assert"
1818
)
1919

2020
func TestStringCodec(t *testing.T) {

bson/bsoncodec/struct_codec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13-
"github.com/stretchr/testify/assert"
13+
"go.mongodb.org/mongo-driver/internal/assert"
1414
)
1515

1616
func TestZeoerInterfaceUsedByDecoder(t *testing.T) {

bson/bsoncodec/time_codec_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"go.mongodb.org/mongo-driver/bson/bsonoptions"
1515
"go.mongodb.org/mongo-driver/bson/bsonrw/bsonrwtest"
1616
"go.mongodb.org/mongo-driver/bson/bsontype"
17-
"go.mongodb.org/mongo-driver/internal/testutil/assert"
17+
"go.mongodb.org/mongo-driver/internal/assert"
1818
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
1919
)
2020

@@ -43,7 +43,13 @@ func TestTimeCodec(t *testing.T) {
4343
actualTime := actual.Interface().(time.Time)
4444
assert.Equal(t, actualTime.Location().String() == "UTC", tc.utc,
4545
"Expected UTC: %v, got %v", tc.utc, actualTime.Location())
46-
assert.Equal(t, now, actualTime, "expected time %v, got %v", now, actualTime)
46+
47+
if tc.utc {
48+
nowUTC := now.UTC()
49+
assert.Equal(t, nowUTC, actualTime, "expected time %v, got %v", nowUTC, actualTime)
50+
} else {
51+
assert.Equal(t, now, actualTime, "expected time %v, got %v", now, actualTime)
52+
}
4753
})
4854
}
4955
})
@@ -72,7 +78,8 @@ func TestTimeCodec(t *testing.T) {
7278
if tc.name == "timestamp" {
7379
now = time.Unix(now.Unix(), 0)
7480
}
75-
assert.Equal(t, now, actualTime, "expected time %v, got %v", now, actualTime)
81+
nowUTC := now.UTC()
82+
assert.Equal(t, nowUTC, actualTime, "expected time %v, got %v", nowUTC, actualTime)
7683
})
7784
}
7885
})

0 commit comments

Comments
 (0)