1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/testctx/testctx.go
Carlos Alexandro Becker 25a054c5e1
feat: improve --single-target (#4442)
closes #4437 
closes #4426

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2023-11-27 18:29:50 -03:00

134 lines
2.5 KiB
Go

// Package testctx provides a test context to be used in unit tests.
package testctx
import (
"time"
"github.com/goreleaser/goreleaser/internal/skips"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Opt is an option for a test context.
type Opt func(ctx *context.Context)
func GitHubTokenType(ctx *context.Context) {
WithTokenType(context.TokenTypeGitHub)(ctx)
WithToken("githubtoken")(ctx)
}
func GitLabTokenType(ctx *context.Context) {
WithTokenType(context.TokenTypeGitLab)(ctx)
WithToken("gitlabtoken")(ctx)
}
func GiteaTokenType(ctx *context.Context) {
WithTokenType(context.TokenTypeGitea)(ctx)
WithToken("giteatoken")(ctx)
}
func WithTokenType(t context.TokenType) Opt {
return func(ctx *context.Context) {
ctx.TokenType = t
}
}
func WithToken(t string) Opt {
return func(ctx *context.Context) {
ctx.Token = t
}
}
func WithVersion(v string) Opt {
return func(ctx *context.Context) {
ctx.Version = v
}
}
func WithSemver(major, minor, patch uint64, prerelease string) Opt {
return func(ctx *context.Context) {
ctx.Semver = context.Semver{
Major: major,
Minor: minor,
Patch: patch,
Prerelease: prerelease,
}
}
}
func WithGitInfo(git context.GitInfo) Opt {
return func(ctx *context.Context) {
ctx.Git = git
}
}
func WithCurrentTag(tag string) Opt {
return func(ctx *context.Context) {
ctx.Git.CurrentTag = tag
}
}
func WithCommit(commig string) Opt {
return func(ctx *context.Context) {
ctx.Git.Commit = commig
ctx.Git.FullCommit = commig
}
}
func WithCommitDate(d time.Time) Opt {
return func(ctx *context.Context) {
ctx.Git.CommitDate = d
}
}
func WithPreviousTag(tag string) Opt {
return func(ctx *context.Context) {
ctx.Git.PreviousTag = tag
}
}
func WithEnv(env map[string]string) Opt {
return func(ctx *context.Context) {
ctx.Env = env
}
}
func WithDate(t time.Time) Opt {
return func(ctx *context.Context) {
ctx.Date = t
}
}
func WithFakeRuntime(ctx *context.Context) {
ctx.Runtime = context.Runtime{
Goos: "fakeos",
Goarch: "fakearch",
}
}
func Skip(keys ...skips.Key) Opt {
return func(ctx *context.Context) {
skips.Set(ctx, keys...)
}
}
func Snapshot(ctx *context.Context) {
ctx.Snapshot = true
}
func Partial(ctx *context.Context) {
ctx.Partial = true
}
func NewWithCfg(c config.Project, opts ...Opt) *context.Context {
ctx := context.New(c)
for _, opt := range opts {
opt(ctx)
}
return ctx
}
func New(opts ...Opt) *context.Context {
return NewWithCfg(config.Project{}, opts...)
}