-
Notifications
You must be signed in to change notification settings - Fork 375
/
helpers.go
116 lines (93 loc) · 3.08 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package setup
import (
"path/filepath"
"runtime"
"strings"
"github.com/stretchr/testify/require"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/util/testutil/testfail"
"github.com/FerretDB/FerretDB/internal/util/testutil/testtb"
)
// IsPostgreSQL returns true if the current test is running for PostgreSQL backend.
//
// This function should not be used lightly.
func IsPostgreSQL(tb testtb.TB) bool {
return *targetBackendF == "ferretdb-postgresql"
}
// IsSQLite returns true if the current test is running for SQLite backend.
//
// This function should not be used lightly.
func IsSQLite(tb testtb.TB) bool {
return *targetBackendF == "ferretdb-sqlite"
}
// IsMongoDB returns true if the current test is running for MongoDB.
//
// This function should not be used lightly.
func IsMongoDB(tb testtb.TB) bool {
return *targetBackendF == "mongodb"
}
// IsHana returns true if the current test is running for Hana backend.
//
// This function should not be used lightly.
func IsHana(tb testtb.TB) bool {
return *targetBackendF == "ferretdb-hana"
}
// ensureIssueURL panics if URL is not a valid FerretDB issue URL.
func ensureIssueURL(url string) {
must.BeTrue(strings.HasPrefix(url, "https://github.com/FerretDB/FerretDB/issues/"))
}
// FailsForFerretDB return testtb.TB that expects test to fail for FerretDB and pass for MongoDB.
//
// This function should not be used lightly and always with an issue URL.
func FailsForFerretDB(tb testtb.TB, url string) testtb.TB {
tb.Helper()
ensureIssueURL(url)
if IsMongoDB(tb) {
return tb
}
return testfail.Expected(tb, url)
}
// FailsForMongoDB return testtb.TB that expects test to fail for MongoDB and pass for FerretDB.
//
// This function should not be used lightly.
func FailsForMongoDB(tb testtb.TB, reason string) testtb.TB {
tb.Helper()
if IsMongoDB(tb) {
return testfail.Expected(tb, reason)
}
return tb
}
// SkipForMongoDB skips the current test for MongoDB.
//
// Deprecated: Use [FailsForMongoDB] in new code.
func SkipForMongoDB(tb testtb.TB, reason string) {
tb.Helper()
if IsMongoDB(tb) {
require.NotEmpty(tb, reason, "reason must not be empty")
tb.Skipf("Skipping for MongoDB: %s.", reason)
}
}
// PushdownDisabled returns true if FerretDB pushdown is disabled.
func PushdownDisabled() bool {
return *disablePushdownF
}
// Dir returns the absolute directory of this package.
func Dir(tb testtb.TB) string {
tb.Helper()
_, file, _, ok := runtime.Caller(0)
require.True(tb, ok)
return filepath.Dir(file)
}