Skip to content

Commit 35c9898

Browse files
authored
initial implementation of test helpers project (#1)
1 parent 2977c35 commit 35c9898

35 files changed

+1710
-0
lines changed

.circleci/config.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
version: 2.1
2+
3+
orbs:
4+
win: circleci/[email protected]
5+
6+
workflows:
7+
workflow:
8+
jobs:
9+
- go-test:
10+
name: Go 1.14
11+
docker-image: circleci/golang:1.14
12+
- go-test:
13+
name: Go 1.13
14+
docker-image: circleci/golang:1.13
15+
- go-test:
16+
name: Go 1.12
17+
docker-image: circleci/golang:1.12
18+
- go-test:
19+
name: Go 1.11
20+
docker-image: circleci/golang:1.11
21+
- go-test:
22+
name: Go 1.10
23+
docker-image: circleci/golang:1.10
24+
run-lint: false # the version of golangci-lint we're using doesn't work with older Go versions
25+
- go-test:
26+
name: Go 1.9
27+
docker-image: circleci/golang:1.9
28+
run-lint: false # the version of golangci-lint we're using doesn't work with older Go versions
29+
- go-test:
30+
name: Go 1.8
31+
docker-image: circleci/golang:1.8
32+
run-lint: false # the version of golangci-lint we're using doesn't work with older Go versions
33+
- go-test-windows:
34+
name: Windows
35+
36+
jobs:
37+
go-test:
38+
parameters:
39+
run-lint:
40+
type: boolean
41+
default: true
42+
docker-image:
43+
type: string
44+
45+
docker:
46+
- image: <<parameters.docker-image>>
47+
environment:
48+
CIRCLE_TEST_REPORTS: /tmp/circle-reports
49+
CIRCLE_ARTIFACTS: /tmp/circle-artifacts
50+
COMMON_GO_PACKAGES: >
51+
github.com/jstemmer/go-junit-report
52+
53+
working_directory: /go/src/github.com/launchdarkly/go-test-helpers
54+
55+
steps:
56+
- checkout
57+
- run: go get -u $COMMON_GO_PACKAGES
58+
- when:
59+
condition: <<parameters.run-lint>>
60+
steps:
61+
- run: make lint
62+
63+
- run:
64+
name: Run tests
65+
command: |
66+
mkdir -p $CIRCLE_TEST_REPORTS
67+
mkdir -p $CIRCLE_ARTIFACTS
68+
trap "go-junit-report < $CIRCLE_ARTIFACTS/report.txt > $CIRCLE_TEST_REPORTS/junit.xml" EXIT
69+
make test | tee $CIRCLE_ARTIFACTS/report.txt
70+
71+
- store_test_results:
72+
path: /tmp/circle-reports
73+
74+
- store_artifacts:
75+
path: /tmp/circle-artifacts
76+
77+
go-test-windows:
78+
executor:
79+
name: win/vs2019
80+
shell: powershell.exe
81+
82+
environment:
83+
GOPATH: C:\Users\VssAdministrator\go
84+
PACKAGE_PATH: github.com/launchdarkly/go-test-helpers
85+
86+
steps:
87+
- checkout
88+
- run: go version
89+
- run:
90+
name: move source
91+
command: |
92+
go env GOPATH
93+
mkdir ${env:GOPATH}\src\${env:PACKAGE_PATH}
94+
mv * ${env:GOPATH}\src\${env:PACKAGE_PATH}
95+
- run:
96+
name: build and test
97+
command: |
98+
cd ${env:GOPATH}\src\${env:PACKAGE_PATH}
99+
go get -t ./...
100+
go test -race ./...

.golangci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
run:
2+
deadline: 120s
3+
tests: false
4+
5+
linters:
6+
enable-all: true
7+
disable:
8+
- funlen # allow long/complex functions for now
9+
- gocognit # allow long/complex functions for now
10+
- gomnd # extremely aggressive linter that complains about all numeric literals, even just adding 1 to something (https://github.com/tommy-muehle/go-mnd)
11+
- wsl # enforces a very specific style with an idiosyncratic definition of what "cuddling" is
12+
fast: false
13+
14+
linter-settings:
15+
gofmt:
16+
simplify: false
17+
18+
issues:
19+
exclude-use-default: false
20+
max-same-issues: 1000
21+
max-per-linter: 1000

.ldrelease/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
publications:
2+
- url: https://godoc.org/github.com/launchdarkly/go-test-helpers
3+
description: documentation

.ldrelease/update-version.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
# This script updates the version for the ldclient.go
3+
4+
set -ue
5+
6+
LDCLIENT_GO_TEMP=./ldclient.go.tmp
7+
sed "s/const Version =.*/const Version = \"${LD_RELEASE_VERSION}\"/g" ldclient.go > ${LDCLIENT_GO_TEMP}
8+
mv ${LDCLIENT_GO_TEMP} ldclient.go

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Change log
2+
3+
All notable changes to the project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org).

CONTRIBUTING.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Contributing to go-test-helpers
2+
3+
## Submitting bug reports and feature requests
4+
5+
The LaunchDarkly SDK team maintains this repository and monitors the [issue tracker](https://github.com/launchdarkly/go-test-helpers/issues) there. Bug reports and feature requests specific to this project should be filed in this issue tracker. The team will respond to all newly filed issues within two business days.
6+
7+
## Submitting pull requests
8+
9+
We encourage pull requests and other contributions from the community. Before submitting pull requests, ensure that all temporary or unintended code is removed. Don't worry about adding reviewers to the pull request; the LaunchDarkly SDK team will add themselves. The SDK team will acknowledge all pull requests within two business days.
10+
11+
## Build instructions
12+
13+
### Prerequisites
14+
15+
This project should be built against Go 1.8 or newer.
16+
17+
### Building
18+
19+
To build the project without running any tests:
20+
```
21+
make
22+
```
23+
24+
If you wish to clean your working directory between builds, you can clean it by running:
25+
```
26+
make clean
27+
```
28+
29+
To run the linter:
30+
```
31+
make lint
32+
```
33+
34+
### Testing
35+
36+
To build the project and run all unit tests:
37+
```
38+
make test
39+
```

Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
GOLANGCI_LINT_VERSION=v1.23.7
3+
4+
LINTER=./bin/golangci-lint
5+
LINTER_VERSION_FILE=./bin/.golangci-lint-version-$(GOLANGCI_LINT_VERSION)
6+
7+
.PHONY: build clean test lint
8+
9+
build:
10+
go get ./...
11+
go build ./...
12+
13+
clean:
14+
go clean
15+
16+
test: build
17+
go get -t ./...
18+
@# Note, we need to specify all these packages individually for go test in order to remain 1.8-compatible
19+
go test -race -v . ./httphelpers ./ldservices
20+
21+
$(LINTER_VERSION_FILE):
22+
rm -f $(LINTER)
23+
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s $(GOLANGCI_LINT_VERSION)
24+
touch $(LINTER_VERSION_FILE)
25+
26+
lint: $(LINTER_VERSION_FILE)
27+
go get ./...
28+
$(LINTER) run ./...

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# LaunchDarkly Go Test Helpers
2+
3+
[![Circle CI](https://circleci.com/gh/launchdarkly/go-test-helpers.svg?style=svg)](https://circleci.com/gh/launchdarkly/go-test-helpers)
4+
5+
This package centralizes some test support code that is used by LaunchDarkly's Go SDK and related components, and that may be useful in other Go projects.
6+
7+
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.
8+
9+
## Contents
10+
11+
The main package provides general-purpose helper functions.
12+
13+
Subpackage `httphelpers` provides convenience wrappers for using `net/http` and `net/http/httptest` in test code.
14+
15+
Subpackage `ldservices` is specifically for testing LaunchDarkly SDK client components; it provides HTTP handlers that simulate the service endpoints used by the SDK.
16+
17+
## Usage
18+
19+
Import any of these packages in your test code:
20+
21+
```go
22+
import (
23+
"github.com/launchdarkly/go-test-helpers"
24+
"github.com/launchdarkly/go-test-helpers/httphelpers"
25+
"github.com/launchdarkly/go-test-helpers/ldservices"
26+
)
27+
```
28+
29+
Breaking changes will only be made in a new major version. It is advisable to use a dependency manager to pin these dependencies to a major version branch (`v1`, etc.).
30+
31+
## Contributing
32+
33+
We encourage pull requests and other contributions from the community. Check out our [contributing guidelines](CONTRIBUTING.md) for instructions on how to contribute to this project.
34+
35+
## About LaunchDarkly
36+
37+
* LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
38+
* Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
39+
* Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
40+
* Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
41+
* Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan). Disable parts of your application to facilitate maintenance, without taking everything offline.
42+
* LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Check out [our documentation](https://docs.launchdarkly.com/docs) for a complete list.
43+
* Explore LaunchDarkly
44+
* [launchdarkly.com](https://www.launchdarkly.com/ "LaunchDarkly Main Website") for more information
45+
* [docs.launchdarkly.com](https://docs.launchdarkly.com/ "LaunchDarkly Documentation") for our documentation and SDK reference guides
46+
* [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ "LaunchDarkly API Documentation") for our API documentation
47+
* [blog.launchdarkly.com](https://blog.launchdarkly.com/ "LaunchDarkly Blog Documentation") for the latest product updates
48+
* [Feature Flagging Guide](https://github.com/launchdarkly/featureflags/ "Feature Flagging Guide") for best practices and strategies

closer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package helpers
2+
3+
import (
4+
"io"
5+
"log"
6+
)
7+
8+
// WithCloser executes a function and ensures that the given object's Close() method is always called afterward.
9+
//
10+
// This is simply a way to get more specific control over an object's lifetime than using defer. A test function
11+
// may wish to ensure that an object is closed before some subsequent actions are taken, rather than at the end
12+
// of the entire test.
13+
//
14+
// If closing the object fails, an error is logged.
15+
func WithCloser(closeableObject io.Closer, action func()) {
16+
defer func() {
17+
err := closeableObject.Close()
18+
if err != nil {
19+
log.Printf("failed to close %T: %s", closeableObject, err)
20+
}
21+
}()
22+
action()
23+
}

closer_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package helpers
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type myCloser struct {
10+
closed bool
11+
}
12+
13+
func (m *myCloser) Close() error {
14+
m.closed = true
15+
return nil
16+
}
17+
18+
func TestWithCloser(t *testing.T) {
19+
c := &myCloser{}
20+
WithCloser(c, func() {
21+
assert.False(t, c.closed)
22+
})
23+
assert.True(t, c.closed)
24+
}

0 commit comments

Comments
 (0)