Skip to content

Commit 1cc09ee

Browse files
committed
GODRIVER-3445 Don't require package-relative paths for spectest.Path
1 parent 2483392 commit 1cc09ee

File tree

9 files changed

+23
-22
lines changed

9 files changed

+23
-22
lines changed

internal/serverselector/server_selector_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func compareServers(t *testing.T, expected []*serverDesc, actual []description.S
157157
}
158158
}
159159

160-
var maxStalenessTestsDir = spectest.TestPath(2, "max-staleness")
160+
var maxStalenessTestsDir = spectest.Path("max-staleness")
161161

162162
// Test case for all max staleness spec tests.
163163
func TestMaxStalenessSpec(t *testing.T) {
@@ -176,7 +176,7 @@ func TestMaxStalenessSpec(t *testing.T) {
176176
}
177177
}
178178

179-
var selectorTestsDir = spectest.TestPath(2, "server-selection")
179+
var selectorTestsDir = spectest.Path("server-selection")
180180

181181
func selectServers(t *testing.T, test *testCase) error {
182182
servers := make([]description.Server, 0, len(test.TopologyDescription.Servers))

internal/spectest/spectest.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os"
1111
"path"
1212
"path/filepath"
13-
"strings"
1413
"testing"
1514

1615
"go.mongodb.org/mongo-driver/v2/internal/require"
@@ -33,19 +32,21 @@ func FindJSONFilesInDir(t *testing.T, dir string) []string {
3332
files = append(files, entry.Name())
3433
}
3534

35+
if len(files) == 0 {
36+
t.Fatalf("no JSON files found in %q", dir)
37+
}
38+
3639
return files
3740
}
3841

39-
// TestPath will construct a path to a test file in the specifications git
40-
// submodule. The path will be relative to the current package so a depth should
41-
// be provided to indicate how many directories to go up.
42-
func TestPath(depth int, testDir string, subDirs ...string) string {
43-
const basePath = "testdata/specifications/source/"
44-
45-
// Create a string of "../" repeated 'depth' times
46-
relativePath := strings.Repeat("../", depth)
47-
// Construct the full path
48-
fullPath := relativePath + basePath + testDir + "/tests/" + filepath.Join(subDirs...)
42+
// Path returns the absolute path to the "tests" directory for the given
43+
// specification directory. If the spec test files are nested in a subdirectory,
44+
// pass the subdirectories as additional arguments after the spec directory.
45+
func Path(specDir string, subDirs ...string) string {
46+
testPath, err := filepath.Abs("../../../testdata/specifications/source/")
47+
if err != nil {
48+
panic(err)
49+
}
4950

50-
return fullPath
51+
return filepath.Join(testPath, specDir, "tests", filepath.Join(subDirs...))
5152
}

mongo/read_write_concern_spec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var (
3636
reg.RegisterTypeMapEntry(bson.TypeEmbeddedDocument, reflect.TypeOf(bson.Raw{}))
3737
return reg
3838
}()
39-
readWriteConcernTestsDir = spectest.TestPath(1, "read-write-concern")
39+
readWriteConcernTestsDir = spectest.Path("read-write-concern")
4040
)
4141

4242
type connectionStringTestFile struct {

x/mongo/driver/auth/auth_spec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type testContainer struct {
3838
}
3939

4040
// Note a test supporting the deprecated gssapiServiceName property was removed from data/auth/auth_tests.json
41-
var authTestsDir = spectest.TestPath(4, "auth", "legacy")
41+
var authTestsDir = spectest.Path("auth", "legacy")
4242

4343
func runTestsInFile(t *testing.T, dirname string, filename string) {
4444
filepath := path.Join(dirname, filename)

x/mongo/driver/connstring/connstring_spec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ type testContainer struct {
4747
Tests []testCase
4848
}
4949

50-
var connstringTestsDir = spectest.TestPath(4, "connection-string")
51-
var urioptionsTestDir = spectest.TestPath(4, "uri-options")
50+
var connstringTestsDir = spectest.Path("connection-string")
51+
var urioptionsTestDir = spectest.Path("uri-options")
5252

5353
func (h *host) toString() string {
5454
switch h.Type {

x/mongo/driver/topology/CMAP_spec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ type testInfo struct {
114114
sync.Mutex
115115
}
116116

117-
var cmapTestDir = spectest.TestPath(4, "connection-monitoring-and-pooling", "cmap-format")
117+
var cmapTestDir = spectest.Path("connection-monitoring-and-pooling", "cmap-format")
118118

119119
func TestCMAPSpec(t *testing.T) {
120120
for _, testFileName := range spectest.FindJSONFilesInDir(t, cmapTestDir) {

x/mongo/driver/topology/sdam_spec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func serverClosed(e *event.ServerClosedEvent) {
203203
lock.Unlock()
204204
}
205205

206-
var testsDir = spectest.TestPath(4, "server-discovery-and-monitoring")
206+
var testsDir = spectest.Path("server-discovery-and-monitoring")
207207

208208
var publishedEvents []interface{}
209209
var lock sync.Mutex

x/mongo/driver/topology/server_rtt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestServerSelectionRTTSpec(t *testing.T) {
2727
NewAvgRtt float64 `json:"new_avg_rtt"`
2828
}
2929

30-
testsDir := spectest.TestPath(4, "server-selection", "rtt")
30+
testsDir := spectest.Path("server-selection", "rtt")
3131

3232
for _, file := range spectest.FindJSONFilesInDir(t, testsDir) {
3333
func(t *testing.T, filename string) {

x/mongo/driver/topology/topology_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ type inWindowTestCase struct {
808808
// can't be effectively accomplished just with server descriptions like most other server selection
809809
// algorithms.
810810
func TestServerSelectionSpecInWindow(t *testing.T) {
811-
testsDir := spectest.TestPath(4, "server-selection", "in_window")
811+
testsDir := spectest.Path("server-selection", "in_window")
812812

813813
files := spectest.FindJSONFilesInDir(t, testsDir)
814814

0 commit comments

Comments
 (0)