github.com/windmeup/goreleaser@v1.21.95/internal/testctx/testctx.go (about) 1 // Package testctx provides a test context to be used in unit tests. 2 package testctx 3 4 import ( 5 "time" 6 7 "github.com/windmeup/goreleaser/internal/skips" 8 "github.com/windmeup/goreleaser/pkg/config" 9 "github.com/windmeup/goreleaser/pkg/context" 10 ) 11 12 // Opt is an option for a test context. 13 type Opt func(ctx *context.Context) 14 15 func GitHubTokenType(ctx *context.Context) { 16 WithTokenType(context.TokenTypeGitHub)(ctx) 17 WithToken("githubtoken")(ctx) 18 } 19 20 func GitLabTokenType(ctx *context.Context) { 21 WithTokenType(context.TokenTypeGitLab)(ctx) 22 WithToken("gitlabtoken")(ctx) 23 } 24 25 func GiteaTokenType(ctx *context.Context) { 26 WithTokenType(context.TokenTypeGitea)(ctx) 27 WithToken("giteatoken")(ctx) 28 } 29 30 func WithTokenType(t context.TokenType) Opt { 31 return func(ctx *context.Context) { 32 ctx.TokenType = t 33 } 34 } 35 36 func WithToken(t string) Opt { 37 return func(ctx *context.Context) { 38 ctx.Token = t 39 } 40 } 41 42 func WithVersion(v string) Opt { 43 return func(ctx *context.Context) { 44 ctx.Version = v 45 } 46 } 47 48 func WithSemver(major, minor, patch uint64, prerelease string) Opt { 49 return func(ctx *context.Context) { 50 ctx.Semver = context.Semver{ 51 Major: major, 52 Minor: minor, 53 Patch: patch, 54 Prerelease: prerelease, 55 } 56 } 57 } 58 59 func WithGitInfo(git context.GitInfo) Opt { 60 return func(ctx *context.Context) { 61 ctx.Git = git 62 } 63 } 64 65 func WithCurrentTag(tag string) Opt { 66 return func(ctx *context.Context) { 67 ctx.Git.CurrentTag = tag 68 } 69 } 70 71 func WithCommit(commig string) Opt { 72 return func(ctx *context.Context) { 73 ctx.Git.Commit = commig 74 ctx.Git.FullCommit = commig 75 } 76 } 77 78 func WithCommitDate(d time.Time) Opt { 79 return func(ctx *context.Context) { 80 ctx.Git.CommitDate = d 81 } 82 } 83 84 func WithPreviousTag(tag string) Opt { 85 return func(ctx *context.Context) { 86 ctx.Git.PreviousTag = tag 87 } 88 } 89 90 func WithEnv(env map[string]string) Opt { 91 return func(ctx *context.Context) { 92 ctx.Env = env 93 } 94 } 95 96 func WithDate(t time.Time) Opt { 97 return func(ctx *context.Context) { 98 ctx.Date = t 99 } 100 } 101 102 func WithFakeRuntime(ctx *context.Context) { 103 ctx.Runtime = context.Runtime{ 104 Goos: "fakeos", 105 Goarch: "fakearch", 106 } 107 } 108 109 func Skip(keys ...skips.Key) Opt { 110 return func(ctx *context.Context) { 111 skips.Set(ctx, keys...) 112 } 113 } 114 115 func Snapshot(ctx *context.Context) { 116 ctx.Snapshot = true 117 } 118 119 func NewWithCfg(c config.Project, opts ...Opt) *context.Context { 120 ctx := context.New(c) 121 for _, opt := range opts { 122 opt(ctx) 123 } 124 return ctx 125 } 126 127 func New(opts ...Opt) *context.Context { 128 return NewWithCfg(config.Project{}, opts...) 129 }