testhelp

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FakeTestCfg = GhTestCfg{
	Token: "fakeToken",
	Owner: "fakeOwner",
	Repo:  "fakeRepo",
	SHA:   "0123456789012345678901234567890123456789",
}

FakeTestCfg is a fake test configuration that can be used in some tests that need configuration but don't really use any external service.

Functions

func CopyDir

func CopyDir(dst string, src string, dirRenamer Renamer, templatedata TemplateData) error

CopyDir recursively copies src directory below dst directory, with optional transformations. It performs the following transformations:

  • Renames any directory with renamer.
  • If templatedata is not empty, will consider each file ending with ".template" as a Go template.
  • If a file name contains basic Go template formatting (eg: `foo-{{.bar}}.template`), the file will be renamed accordingly.

It will fail if the dst directory doesn't exist.

For example, if src directory is `foo`:

foo
└── dot.git
   └── config

and dst directory is `bar`, src will be copied as:

bar
└── foo
   └── .git        <= dot renamed
     └── config

func DotRenamer

func DotRenamer(name string) string

If name begins with "dot.", replace with ".". Otherwise leave it alone.

func FromJSON

func FromJSON(t *testing.T, data []byte, thing any)

FromJSON unmarshals the JSON-encoded data into thing.

func HttpRemote

func HttpRemote(hostname, owner, repo string) string

HttpRemote returns a GitHub HTTP URL

func HttpsRemote

func HttpsRemote(hostname, owner, repo string) string

HttpsRemote returns a GitHub HTTPS URL

func IdentityRenamer

func IdentityRenamer(name string) string

func MakeGitRepoFromTestdata

func MakeGitRepoFromTestdata(
	t *testing.T,
	testdataDir string,
	repoURL string,
	commitSHA string,
	head string,
) string

MakeGitRepoFromTestdata creates a temporary directory by rendering the templated contents of testdataDir with values from (repoURL, commitSHA, head) and returns the path to the directory.

MakeGitRepoFromTestdata also renames directories of the form 'dot.git' to '.git', thus making said directory a git repository. This allows to supply the 'dot.git' directory as test input, avoiding the problem of having this testdata .git directory a nested repository in the project repository.

The temporary directory is registered for removal via t.Cleanup. If any operation fails, makeGitRepoFromTestdata terminates the test by calling t.Fatal.

func MergeStructs

func MergeStructs[T any](a, b T) T

MergeStructs merges b into a and returns the merged copy. Said in another way, a is the default and b is the override. Used to express succinctly the delta in the test cases. Since it is a test helper, it will panic in case of error.

func SpyHttpServer

func SpyHttpServer(request any, reply any, theUrl **url.URL, successCode int,
) *httptest.Server

SpyHttpServer returns a very basic HTTP test server (spy). The handler will JSON decode the request body into `request` and will set `theUrl` to the request URL. On JSON decode success, the handler will return to the client the HTTP status code `successCode`. On JSON decode failure, the handler will return 418 I am a teapot. If `reply` is not nil, the handler will send it to the client, JSON encoded. To avoid races, call ts.Close() before reading any parameters.

Example:

var ghReq github.AddRequest
var URL *url.URL
ts := SpyHttpServer(&ghReq, nil, &URL, http.StatusCreated)

func SshRemote

func SshRemote(hostname, owner, repo string) string

SshRemote returns a GitHub SSH URL

func ToJSON

func ToJSON(t *testing.T, thing any) []byte

ToJSON returns the JSON encoding of thing.

Types

type FailingWriter

type FailingWriter struct{}

FailingWriter is an io.Writer that always returns an error.

func (*FailingWriter) Write

func (t *FailingWriter) Write([]byte) (n int, err error)

type GChatTestCfg

type GChatTestCfg struct {
	Hook string
}

GChatTestCfg contains the secrets needed to run integration tests against the Google Chat API.

func GoogleChatSecretsOrFail

func GoogleChatSecretsOrFail(t *testing.T) GChatTestCfg

GoogleChatSecretsOrFail returns the secrets needed to run integration tests against the Google Chat API. If the secrets are missing, GoogleChatSecretsOrFail fails the test.

type GhTestCfg

type GhTestCfg struct {
	Token string
	Owner string
	Repo  string
	SHA   string
}

GhTestCfg contains the secrets needed to run integration tests against the GitHub Commit Status API.

func GitHubSecretsOrFail

func GitHubSecretsOrFail(t *testing.T) GhTestCfg

GitHubSecretsOrFail returns the secrets needed to run integration tests against the GitHub Commit Status API. If the secrets are missing, GitHubSecretsOrFail fails the test.

type Renamer

type Renamer func(string) string

type TemplateData

type TemplateData map[string]string

Passed to template.Execute()

Jump to

Keyboard shortcuts

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