1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-19 20:57:53 +02:00
goreleaser/internal/pipe/env/env_test.go

317 lines
8.6 KiB
Go
Raw Normal View History

2017-03-25 20:40:45 -03:00
package env
import (
2017-05-01 10:23:28 -03:00
"fmt"
2017-03-25 20:40:45 -03:00
"os"
2023-06-18 11:58:17 -03:00
"syscall"
2017-03-25 20:40:45 -03:00
"testing"
"github.com/goreleaser/goreleaser/v2/internal/skips"
"github.com/goreleaser/goreleaser/v2/internal/testctx"
"github.com/goreleaser/goreleaser/v2/internal/testlib"
"github.com/goreleaser/goreleaser/v2/pkg/config"
"github.com/goreleaser/goreleaser/v2/pkg/context"
"github.com/stretchr/testify/require"
2017-03-25 20:40:45 -03:00
)
func TestMain(m *testing.M) {
restores := map[string]string{}
for _, key := range []string{"GITHUB_TOKEN", "GITEA_TOKEN", "GITLAB_TOKEN"} {
prevValue, ok := os.LookupEnv(key)
if ok {
_ = os.Unsetenv(key)
restores[key] = prevValue
}
}
m.Run()
for k, v := range restores {
_ = os.Setenv(k, v)
}
}
2017-03-25 20:40:45 -03:00
func TestDescription(t *testing.T) {
require.NotEmpty(t, Pipe{}.String())
2017-03-25 20:40:45 -03:00
}
func TestSetDefaultTokenFiles(t *testing.T) {
t.Run("empty config", func(t *testing.T) {
ctx := testctx.New()
setDefaultTokenFiles(ctx)
require.Equal(t, "~/.config/goreleaser/github_token", ctx.Config.EnvFiles.GitHubToken)
require.Equal(t, "~/.config/goreleaser/gitlab_token", ctx.Config.EnvFiles.GitLabToken)
require.Equal(t, "~/.config/goreleaser/gitea_token", ctx.Config.EnvFiles.GiteaToken)
2018-02-03 00:06:48 -02:00
})
t.Run("custom config config", func(t *testing.T) {
2018-02-03 00:06:48 -02:00
cfg := "what"
ctx := testctx.NewWithCfg(config.Project{
2018-02-03 00:06:48 -02:00
EnvFiles: config.EnvFiles{
GitHubToken: cfg,
},
})
setDefaultTokenFiles(ctx)
require.Equal(t, cfg, ctx.Config.EnvFiles.GitHubToken)
2018-02-03 00:06:48 -02:00
})
2021-05-24 23:23:59 -03:00
t.Run("templates", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
2021-05-24 23:23:59 -03:00
ProjectName: "foobar",
Env: []string{
"FOO=FOO_{{ .Env.BAR }}",
"FOOBAR={{.ProjectName}}",
"EMPTY_VAL=",
2021-05-24 23:23:59 -03:00
},
})
ctx.Env["FOOBAR"] = "old foobar"
t.Setenv("BAR", "lebar")
t.Setenv("GITHUB_TOKEN", "fake")
2021-05-24 23:23:59 -03:00
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "FOO_lebar", ctx.Env["FOO"])
require.Equal(t, "foobar", ctx.Env["FOOBAR"])
require.Equal(t, "", ctx.Env["EMPTY_VAL"])
2021-05-24 23:23:59 -03:00
})
t.Run("template error", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
2021-05-24 23:23:59 -03:00
Env: []string{
"FOO={{ .Asss }",
},
})
testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
2021-05-24 23:23:59 -03:00
})
2021-09-27 08:13:56 -03:00
t.Run("no token", func(t *testing.T) {
ctx := testctx.New()
require.EqualError(t, Pipe{}.Run(ctx), ErrMissingToken.Error())
})
}
func TestForceToken(t *testing.T) {
t.Run("github", func(t *testing.T) {
t.Setenv("GITHUB_TOKEN", "fake")
t.Setenv("GORELEASER_FORCE_TOKEN", "github")
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, context.TokenTypeGitHub, ctx.TokenType)
})
t.Run("gitlab", func(t *testing.T) {
t.Setenv("GITLAB_TOKEN", "fake")
t.Setenv("GORELEASER_FORCE_TOKEN", "gitlab")
ctx := testctx.New()
2021-09-27 08:13:56 -03:00
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, context.TokenTypeGitLab, ctx.TokenType)
})
t.Run("gitea", func(t *testing.T) {
t.Setenv("GITEA_TOKEN", "fake")
t.Setenv("GORELEASER_FORCE_TOKEN", "gitea")
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, context.TokenTypeGitea, ctx.TokenType)
2021-09-27 08:13:56 -03:00
})
2018-02-03 00:06:48 -02:00
}
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
func TestValidGithubEnv(t *testing.T) {
t.Setenv("GITHUB_TOKEN", "asdf")
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "asdf", ctx.Token)
require.Equal(t, context.TokenTypeGitHub, ctx.TokenType)
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
}
func TestValidGitlabEnv(t *testing.T) {
t.Setenv("GITLAB_TOKEN", "qwertz")
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "qwertz", ctx.Token)
require.Equal(t, context.TokenTypeGitLab, ctx.TokenType)
2017-03-25 20:40:45 -03:00
}
func TestValidGiteaEnv(t *testing.T) {
t.Setenv("GITEA_TOKEN", "token")
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "token", ctx.Token)
require.Equal(t, context.TokenTypeGitea, ctx.TokenType)
}
2017-03-25 20:40:45 -03:00
func TestInvalidEnv(t *testing.T) {
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
require.EqualError(t, Pipe{}.Run(ctx), ErrMissingToken.Error())
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
}
func TestMultipleEnvTokens(t *testing.T) {
t.Setenv("GITHUB_TOKEN", "asdf")
t.Setenv("GITLAB_TOKEN", "qwertz")
t.Setenv("GITEA_TOKEN", "token")
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
require.EqualError(t, Pipe{}.Run(ctx), "multiple tokens found, but only one is allowed: GITHUB_TOKEN, GITLAB_TOKEN, GITEA_TOKEN\n\nLearn more at https://goreleaser.com/errors/multiple-tokens\n")
}
func TestMultipleEnvTokensForce(t *testing.T) {
t.Setenv("GITHUB_TOKEN", "asdf")
t.Setenv("GITLAB_TOKEN", "qwertz")
t.Setenv("GITEA_TOKEN", "token")
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
require.EqualError(t, Pipe{}.Run(ctx), "multiple tokens found, but only one is allowed: GITHUB_TOKEN, GITLAB_TOKEN, GITEA_TOKEN\n\nLearn more at https://goreleaser.com/errors/multiple-tokens\n")
2017-03-25 20:40:45 -03:00
}
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
func TestEmptyGithubFileEnv(t *testing.T) {
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
2018-02-03 00:06:48 -02:00
}
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
func TestEmptyGitlabFileEnv(t *testing.T) {
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
}
func TestEmptyGiteaFileEnv(t *testing.T) {
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
}
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
func TestEmptyGithubEnvFile(t *testing.T) {
f, err := os.CreateTemp(t.TempDir(), "token")
require.NoError(t, err)
require.NoError(t, f.Close())
require.NoError(t, os.Chmod(f.Name(), 0o377))
ctx := testctx.NewWithCfg(config.Project{
EnvFiles: config.EnvFiles{
GitHubToken: f.Name(),
2018-02-03 00:06:48 -02:00
},
})
2023-06-18 11:58:17 -03:00
err = Pipe{}.Run(ctx)
requireErrAccess(t, err)
require.ErrorContains(t, err, "failed to load github token")
2017-04-29 12:49:22 +02:00
}
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
func TestEmptyGitlabEnvFile(t *testing.T) {
f, err := os.CreateTemp(t.TempDir(), "token")
require.NoError(t, err)
require.NoError(t, f.Close())
require.NoError(t, os.Chmod(f.Name(), 0o377))
ctx := testctx.NewWithCfg(config.Project{
EnvFiles: config.EnvFiles{
GitLabToken: f.Name(),
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
},
})
2023-06-18 11:58:17 -03:00
err = Pipe{}.Run(ctx)
requireErrAccess(t, err)
require.ErrorContains(t, err, "failed to load gitlab token")
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
}
func TestEmptyGiteaEnvFile(t *testing.T) {
f, err := os.CreateTemp(t.TempDir(), "token")
require.NoError(t, err)
require.NoError(t, f.Close())
require.NoError(t, os.Chmod(f.Name(), 0o377))
ctx := testctx.NewWithCfg(config.Project{
EnvFiles: config.EnvFiles{
GiteaToken: f.Name(),
},
})
2023-06-18 11:58:17 -03:00
err = Pipe{}.Run(ctx)
requireErrAccess(t, err)
require.ErrorContains(t, err, "failed to load gitea token")
}
2017-05-01 10:23:28 -03:00
func TestInvalidEnvChecksSkipped(t *testing.T) {
ctx := testctx.New(testctx.Skip(skips.Publish))
require.NoError(t, Pipe{}.Run(ctx))
}
2018-02-03 00:06:48 -02:00
func TestInvalidEnvReleaseDisabled(t *testing.T) {
t.Run("true", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Env: []string{},
Release: config.Release{
Disable: "true",
},
})
require.NoError(t, Pipe{}.Run(ctx))
})
t.Run("tmpl true", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"FOO=true"},
Release: config.Release{
Disable: "{{ .Env.FOO }}",
},
})
require.NoError(t, Pipe{}.Run(ctx))
})
t.Run("tmpl false", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"FOO=true"},
Release: config.Release{
Disable: "{{ .Env.FOO }}-nope",
},
})
require.EqualError(t, Pipe{}.Run(ctx), ErrMissingToken.Error())
})
t.Run("tmpl error", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Disable: "{{ .Env.FOO }}",
},
})
testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
})
}
2018-02-03 00:06:48 -02:00
func TestLoadEnv(t *testing.T) {
const env = "SUPER_SECRET_ENV_NOPE"
t.Run("env exists", func(t *testing.T) {
t.Setenv(env, "1")
2018-02-03 00:06:48 -02:00
v, err := loadEnv(env, "nope")
require.NoError(t, err)
require.Equal(t, "1", v)
2018-02-03 00:06:48 -02:00
})
t.Run("env file exists", func(t *testing.T) {
f, err := os.CreateTemp(t.TempDir(), "token")
require.NoError(t, err)
2018-02-03 00:06:48 -02:00
fmt.Fprintf(f, "123")
require.NoError(t, f.Close())
2018-02-03 00:06:48 -02:00
v, err := loadEnv(env, f.Name())
require.NoError(t, err)
require.Equal(t, "123", v)
2018-02-03 00:06:48 -02:00
})
t.Run("env file with an empty line at the end", func(t *testing.T) {
f, err := os.CreateTemp(t.TempDir(), "token")
require.NoError(t, err)
2018-02-03 16:51:19 -02:00
fmt.Fprintf(f, "123\n")
require.NoError(t, f.Close())
2018-02-03 16:51:19 -02:00
v, err := loadEnv(env, f.Name())
require.NoError(t, err)
require.Equal(t, "123", v)
2018-02-03 16:51:19 -02:00
})
t.Run("env file is not readable", func(t *testing.T) {
testlib.SkipIfWindows(t, "permissions work differently in windows")
f, err := os.CreateTemp(t.TempDir(), "token")
require.NoError(t, err)
2018-02-03 00:06:48 -02:00
fmt.Fprintf(f, "123")
require.NoError(t, f.Close())
err = os.Chmod(f.Name(), 0o377)
require.NoError(t, err)
2018-02-03 00:06:48 -02:00
v, err := loadEnv(env, f.Name())
require.EqualError(t, err, fmt.Sprintf("open %s: permission denied", f.Name()))
require.Equal(t, "", v)
2018-02-03 00:06:48 -02:00
})
}
func requireErrAccess(tb testing.TB, err error) {
tb.Helper()
require.Error(tb, err)
// unsupported
if testlib.IsWindows() {
return
}
require.ErrorIs(tb, err, syscall.EACCES)
}