testutils

package module
v0.0.0-...-af35ec3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2024 License: MIT Imports: 23 Imported by: 0

README

testutils

A helper to write more concise and self-explanatory tests.

Examples

func TestICanCreateCertificates(t *testing.T) {
	fs := afero.NewMemMapFs()

	testutils.NewSelfSignedCertificate(t, fs, "/my/certificates", "localhost")

	fd, err := fs.Open("/my/certificates")
	if err != nil {
		return
	}
	names, err := fd.Readdirnames(-1)
	if err != nil {
		return
	}

	assert.Contains(t, names, "tls.crt")
	assert.Contains(t, names, "tls.key")
}
```

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertFileContents

func AssertFileContents[C string | []byte](t assert.TestingT, fs afero.Fs, path string, expected C, msgAndArgs ...interface{}) bool

func AssertFileExists

func AssertFileExists(t assert.TestingT, fs afero.Fs, path string, msgAndArgs ...interface{}) bool

func AssertFsFileEquivalent

func AssertFsFileEquivalent(t assert.TestingT, expected, actual afero.Fs, path string, msgAndArgs ...interface{}) bool

AssertFsFileEquivalent asserts that a file is equivalent on 2 different file systems

AssertFsFileEquivalent(t, referenceFs, testedFs, path,  "error message %s", "formatted")

Equivalence considers the availability of the file (no error returned when opening it), the file permissions, size and its content. Creation, Modification and Access time stamps are excluded from the comparison

func EnsureFileContent

func EnsureFileContent(t *testing.T, fs afero.Fs, path, content string)

func EnsureYAMLFileContent

func EnsureYAMLFileContent(t *testing.T, fs afero.Fs, path string, content interface{})

func IntegrationTest

func IntegrationTest(t SkippableTest)

func NewSelfSignedCertificate

func NewSelfSignedCertificate(t require.TestingT, fs afero.Fs, destinationFolder string, hosts ...string)

NewSelfSignedCertificate generates a new self-signed public key and certificate in the destination folder.

The certificate is stored in destinationFolder/tls.crt The private key in destinationFolder/tls.key

Example
package main

import (
	"fmt"
	"sort"
	"strings"

	testutils "github.com/adevinta/go-testutils-toolkit"
	"github.com/spf13/afero"
)

func main() {
	// use the real *testing.T from the test
	t := &testutils.FakeTest{Name: "TestICanCreateCertificates"}
	fs := afero.NewMemMapFs()

	testutils.NewSelfSignedCertificate(t, fs, "/my/certificates", "localhost")

	fd, err := fs.Open("/my/certificates")
	if err != nil {
		return
	}
	names, err := fd.Readdirnames(-1)
	if err != nil {
		return
	}

	sort.Strings(names)

	fmt.Println(strings.Join(names, " "))

	// Usually, this is done by the go framework
	fmt.Println(t)
}
Output:

tls.crt tls.key
--- PASS: TestICanCreateCertificates

func RequireFileContents

func RequireFileContents[C string | []byte](t require.TestingT, fs afero.Fs, path string, expected C, msgAndArgs ...interface{})

func RequireFileExists

func RequireFileExists(t require.TestingT, fs afero.Fs, path string, msgAndArgs ...interface{})

func RequireFsFileEquivalent

func RequireFsFileEquivalent(t require.TestingT, expected, actual afero.Fs, path string, msgAndArgs ...interface{})

RequireFsFileEquivalent asserts that a file is equivalent on 2 different file systems

RequireFsFileEquivalent(t, referenceFs, testedFs, path,  "error message %s", "formatted")

Equivalence considers the availability of the file (no error returned when opening it), the file permissions, size and its content. Creation, Modification and Access time stamps are excluded from the comparison

func StringBody

func StringBody(body string) io.ReadCloser

Types

type FakeTest

type FakeTest struct {
	ErrorFormats  []MsgAndArgs
	ErrorMessages []string
	Failed        bool
	Name          string
}

func (*FakeTest) Errorf

func (t *FakeTest) Errorf(msg string, args ...interface{})

func (*FakeTest) FailNow

func (t *FakeTest) FailNow()

func (*FakeTest) String

func (t *FakeTest) String() string

type HTTPResponseBuilder

type HTTPResponseBuilder struct {
	// contains filtered or unexported fields
}

func NewHTTPResponseBuilder

func NewHTTPResponseBuilder() *HTTPResponseBuilder

func (*HTTPResponseBuilder) Build

func (b *HTTPResponseBuilder) Build() *http.Response

func (*HTTPResponseBuilder) WithBody

func (*HTTPResponseBuilder) WithJsonBody

func (b *HTTPResponseBuilder) WithJsonBody(body interface{}) *HTTPResponseBuilder

func (*HTTPResponseBuilder) WithStatusCode

func (b *HTTPResponseBuilder) WithStatusCode(code int) *HTTPResponseBuilder

func (*HTTPResponseBuilder) WithTB

type MsgAndArgs

type MsgAndArgs struct {
	MSG  string
	Args []interface{}
}

type RoundTripperFunc

type RoundTripperFunc func(r *http.Request) (*http.Response, error)

func (RoundTripperFunc) RoundTrip

func (r RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error)

type SkippableTest

type SkippableTest interface {
	Helper()
	Skip(...any)
}

type TestHelper

type TestHelper interface {
	Helper()
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL