mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-08 03:31:59 +02:00
ec2db4a727
<!-- Hi, thanks for contributing! Please make sure you read our CONTRIBUTING guide. Also, add tests and the respective documentation changes as well. --> <!-- If applied, this commit will... --> ... <!-- Why is this change being made? --> ... <!-- # Provide links to any relevant tickets, URLs or other resources --> ... --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
134 lines
2.5 KiB
Go
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/v2/internal/skips"
|
|
"github.com/goreleaser/goreleaser/v2/pkg/config"
|
|
"github.com/goreleaser/goreleaser/v2/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...)
|
|
}
|