Skip to content

Commit e105667

Browse files
committed
require Go 1.18, add some generic helpers
1 parent b61c710 commit e105667

24 files changed

+295
-225
lines changed

.circleci/config.yml

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@ workflows:
77
workflow:
88
jobs:
99
- go-test:
10-
name: Go 1.17
11-
docker-image: cimg/go:1.17
10+
name: Go 1.19
11+
docker-image: cimg/go:1.19
12+
run-lint: true
1213
- go-test:
13-
name: Go 1.16
14-
docker-image: cimg/go:1.16
15-
- go-test:
16-
name: Go 1.15
17-
docker-image: cimg/go:1.15
18-
- go-test:
19-
name: Go 1.14
20-
docker-image: cimg/go:1.14
21-
- go-test:
22-
name: Go 1.13
23-
docker-image: cimg/go:1.13
14+
name: Go 1.18
15+
docker-image: cimg/go:1.18
2416
- go-test-windows:
2517
name: Windows
2618

@@ -31,7 +23,7 @@ jobs:
3123
type: string
3224
run-lint:
3325
type: boolean
34-
default: true
26+
default: false
3527

3628
docker:
3729
- image: <<parameters.docker-image>>
@@ -43,7 +35,7 @@ jobs:
4335
- checkout
4436
- run:
4537
name: install go-junit-report
46-
command: go get -u github.com/jstemmer/go-junit-report
38+
command: go install github.com/jstemmer/go-junit-report/[email protected]
4739

4840
- when:
4941
condition: <<parameters.run-lint>>
@@ -74,6 +66,15 @@ jobs:
7466

7567
steps:
7668
- checkout
69+
- run:
70+
name: download Go 1.18.5
71+
command: |
72+
$ErrorActionPreference = "Stop"
73+
$installerUrl = "https://go.dev/dl/go1.18.5.windows-amd64.msi"
74+
(New-Object System.Net.WebClient).DownloadFile($installerUrl, "go1.18.5.windows-amd64.msi")
75+
- run:
76+
name: install Go 1.18.5
77+
command: Start-Process msiexec.exe -Wait -ArgumentList "/I go1.18.5.windows-amd64.msi /quiet"
7778
- run: go version
7879
- run:
7980
name: build and test

.golangci.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@ linters:
1818
- godox
1919
- gofmt
2020
- goimports
21-
- golint
2221
- gosec
2322
- gosimple
2423
- govet
2524
- ineffassign
26-
- interfacer
2725
- lll
2826
- megacheck
2927
- misspell
3028
- nakedret
3129
- nolintlint
3230
- prealloc
31+
- revive
3332
- staticcheck
34-
- structcheck
3533
- stylecheck
3634
- typecheck
3735
- unconvert

.ldrelease/config.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ version: 2
22

33
jobs:
44
- docker:
5-
image: golang:1.13-buster
5+
image: golang:1.18-buster
66
template:
77
name: go
8-
env:
9-
LD_RELEASE_GO_IMPORT_PATH: github.com/launchdarkly/go-test-helpers
108

119
publications:
12-
- url: https://pkg.go.dev/github.com/launchdarkly/go-test-helpers
10+
- url: https://pkg.go.dev/github.com/launchdarkly/go-test-helpers/v2
1311
description: documentation

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
GOLANGCI_LINT_VERSION=v1.27.0
2+
GOLANGCI_LINT_VERSION=v1.48.0
33

44
LINTER=./bin/golangci-lint
55
LINTER_VERSION_FILE=./bin/.golangci-lint-version-$(GOLANGCI_LINT_VERSION)
@@ -17,7 +17,7 @@ test: build
1717

1818
$(LINTER_VERSION_FILE):
1919
rm -f $(LINTER)
20-
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s $(GOLANGCI_LINT_VERSION)
20+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s $(GOLANGCI_LINT_VERSION)
2121
touch $(LINTER_VERSION_FILE)
2222

2323
lint: $(LINTER_VERSION_FILE)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This project centralizes some test support code that is used by LaunchDarkly's G
66

77
While this code may be useful in other projects, it is primarily geared toward LaunchDarkly's own development needs and is not meant to provide a large general-purpose framework. It is meant for unit test code and should not be used as a runtime dependency.
88

9-
This version of the project requires Go 1.13 or higher.
9+
This version of the project requires Go 1.18 or higher.
1010

1111
## Contents
1212

channels.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package helpers
2+
3+
import (
4+
"time"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
// TryReceive returns the next value from the channel and true if successful, or returns an empty
11+
// value and false if the timeout expires first.
12+
func TryReceive[V any](ch <-chan V, timeout time.Duration) (V, bool) {
13+
select {
14+
case v := <-ch:
15+
return v, true
16+
case <-time.After(timeout):
17+
var empty V
18+
return empty, false
19+
}
20+
}
21+
22+
// RequireValue returns the next value from the channel, or forces an immediate test failure
23+
// and exit if the timeout expires first.
24+
func RequireValue[V any](t require.TestingT, ch <-chan V, timeout time.Duration) V {
25+
if v, ok := TryReceive(ch, timeout); ok {
26+
return v
27+
}
28+
var empty V
29+
t.Errorf("expected a %T value from channel but did not receive one in %s", empty, timeout)
30+
t.FailNow()
31+
return empty // never reached
32+
}
33+
34+
// AssertNoMoreValues asserts that no value is available from the channel within the timeout.
35+
func AssertNoMoreValues[V any](t assert.TestingT, ch <-chan V, timeout time.Duration) bool {
36+
if v, ok := TryReceive(ch, timeout); ok {
37+
t.Errorf("expected no more %T values from channel but got one: %+v", v, v)
38+
return false
39+
}
40+
return true
41+
}
42+
43+
// RequireNoMoreValues is equivalent to AssertNoMoreValues except that it forces an immediate
44+
// test exit on failure.
45+
func RequireNoMoreValues[V any](t require.TestingT, ch <-chan V, timeout time.Duration) {
46+
if !AssertNoMoreValues(t, ch, timeout) {
47+
t.FailNow()
48+
}
49+
}

channels_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package helpers
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/launchdarkly/go-test-helpers/v2/testbox"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestTryReceive(t *testing.T) {
12+
ch := make(chan string, 1)
13+
v, ok := TryReceive(ch, time.Millisecond)
14+
assert.False(t, ok)
15+
assert.Equal(t, "", v)
16+
17+
ch <- "a"
18+
v, ok = TryReceive(ch, time.Millisecond)
19+
assert.True(t, ok)
20+
assert.Equal(t, "a", v)
21+
}
22+
23+
func TestRequireValue(t *testing.T) {
24+
result := testbox.SandboxTest(func(t1 testbox.TestingT) {
25+
ch := make(chan string, 1)
26+
_ = RequireValue(t1, ch, time.Millisecond)
27+
t.Errorf("test should have exited early but did not")
28+
})
29+
assert.True(t, result.Failed)
30+
31+
ch := make(chan string, 1)
32+
go func() {
33+
ch <- "a"
34+
}()
35+
v := RequireValue(t, ch, time.Second)
36+
assert.Equal(t, "a", v)
37+
}
38+
39+
func TestAssertNoMoreValues(t *testing.T) {
40+
ch := make(chan string, 1)
41+
AssertNoMoreValues(t, ch, time.Millisecond)
42+
43+
result := testbox.SandboxTest(func(t testbox.TestingT) {
44+
ch := make(chan string, 1)
45+
go func() {
46+
ch <- "a"
47+
}()
48+
AssertNoMoreValues(t, ch, time.Second)
49+
})
50+
assert.True(t, result.Failed)
51+
}
52+
53+
func TestRequireNoMoreValues(t *testing.T) {
54+
ch := make(chan string, 1)
55+
AssertNoMoreValues(t, ch, time.Millisecond)
56+
57+
result := testbox.SandboxTest(func(t1 testbox.TestingT) {
58+
ch := make(chan string, 1)
59+
go func() {
60+
ch <- "a"
61+
}()
62+
RequireNoMoreValues(t1, ch, time.Second)
63+
t.Errorf("test should have exited early but did not")
64+
})
65+
assert.True(t, result.Failed)
66+
}

files.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package helpers
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"log"
76
"os"
87
)
@@ -20,11 +19,11 @@ func FilePathExists(path string) bool {
2019
// If deletion of the file fails (assuming it has not already been deleted) then an error is logged, but there is no
2120
// panic.
2221
//
23-
// helpers.WithTempFile(func(path string) {
24-
// DoSomethingWithTempFile(path)
25-
// }) // the file is deleted at the end of this block
22+
// helpers.WithTempFile(func(path string) {
23+
// DoSomethingWithTempFile(path)
24+
// }) // the file is deleted at the end of this block
2625
func WithTempFile(f func(string)) {
27-
file, err := ioutil.TempFile("", "test")
26+
file, err := os.CreateTemp("", "test")
2827
if err != nil {
2928
panic(fmt.Errorf("can't create temp file: %s", err))
3029
}

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module github.com/launchdarkly/go-test-helpers/v2
22

3-
go 1.13
3+
go 1.18
4+
5+
require github.com/stretchr/testify v1.5.1
46

57
require (
68
github.com/davecgh/go-spew v1.1.1 // indirect
7-
github.com/stretchr/testify v1.5.1
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
810
gopkg.in/yaml.v2 v2.2.8 // indirect
911
)

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
55
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6-
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
76
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
87
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
98
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=

0 commit comments

Comments
 (0)