Skip to content

Commit a3c5f3f

Browse files
committed
test: Update integration test for go1.23
- Add `testContext(*testing.T) context.Context` in `./test` package, with version-specific implementations matching `package unstructured` - Add `randText() string` in `./test` package. Version-specific implmentation for go1.24 and later delegates to `crypto/rand.Text`, while the implementation for go1.23 simply copies the body of `crypto/rand.Text` into a local implementation.
1 parent 0a10cde commit a3c5f3f

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

test/context_new_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:build go1.24 && integration
2+
3+
package test
4+
5+
import (
6+
"context"
7+
"crypto/rand"
8+
"testing"
9+
)
10+
11+
// textContext mimics [*testing.T.Context], which was added in go1.24.
12+
func testContext(t *testing.T) context.Context {
13+
return t.Context()
14+
}
15+
16+
func randText() string {
17+
return rand.Text()
18+
}

test/context_old_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build !go1.24 && integration
2+
3+
package test
4+
5+
import (
6+
"context"
7+
"crypto/rand"
8+
"testing"
9+
)
10+
11+
// textContext mimics [*testing.T.Context], which was added in go1.24.
12+
func testContext(t *testing.T) context.Context {
13+
t.Helper()
14+
ctx, cancel := context.WithCancel(context.Background())
15+
t.Cleanup(cancel)
16+
return ctx
17+
}
18+
19+
func randText() string {
20+
const base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
21+
src := make([]byte, 26)
22+
rand.Read(src)
23+
for i := range src {
24+
src[i] = base32[src[i]%32]
25+
}
26+
return string(src)
27+
}

test/destination_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package test
44

55
import (
66
"context"
7-
"crypto/rand"
87
"fmt"
98
"os"
109
"testing"
@@ -201,7 +200,7 @@ func TestDestinationPermutations(t *testing.T) {
201200
t.Parallel()
202201

203202
destination, err := client.CreateDestination(testContext(t), unstructured.CreateDestinationRequest{
204-
Name: fmt.Sprintf("test-%s-%s", name, rand.Text()),
203+
Name: fmt.Sprintf("test-%s-%s", name, randText()),
205204
Config: src,
206205
})
207206
if err != nil {

test/source_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package test
44

55
import (
66
"context"
7-
"crypto/rand"
87
"fmt"
98
"os"
109
"testing"
@@ -187,7 +186,7 @@ func TestSourcePermutations(t *testing.T) {
187186
t.Parallel()
188187

189188
source, err := client.CreateSource(testContext(t), unstructured.CreateSourceRequest{
190-
Name: fmt.Sprintf("test-%s-%s", name, rand.Text()),
189+
Name: fmt.Sprintf("test-%s-%s", name, randText()),
191190
Config: src,
192191
})
193192
if err != nil {

0 commit comments

Comments
 (0)