2017-03-25 20:40:45 -03:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
2017-05-01 10:23:28 -03:00
|
|
|
"fmt"
|
2018-02-03 00:06:48 -02:00
|
|
|
"io/ioutil"
|
2017-03-25 20:40:45 -03:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2018-08-14 23:50:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2020-10-06 09:48:04 -03:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-03-25 20:40:45 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDescription(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NotEmpty(t, Pipe{}.String())
|
2017-03-25 20:40:45 -03:00
|
|
|
}
|
|
|
|
|
2020-03-16 12:10:56 -03:00
|
|
|
func TestSetDefaultTokenFiles(t *testing.T) {
|
2021-01-07 09:15:32 -03:00
|
|
|
t.Run("empty config", func(t *testing.T) {
|
2018-02-03 00:06:48 -02:00
|
|
|
ctx := context.New(config.Project{})
|
2020-03-16 12:10:56 -03:00
|
|
|
setDefaultTokenFiles(ctx)
|
2020-10-06 09:48:04 -03:00
|
|
|
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
|
|
|
})
|
2021-01-07 09:15:32 -03:00
|
|
|
t.Run("custom config config", func(t *testing.T) {
|
2018-02-03 00:06:48 -02:00
|
|
|
cfg := "what"
|
|
|
|
ctx := context.New(config.Project{
|
|
|
|
EnvFiles: config.EnvFiles{
|
|
|
|
GitHubToken: cfg,
|
|
|
|
},
|
|
|
|
})
|
2020-03-16 12:10:56 -03:00
|
|
|
setDefaultTokenFiles(ctx)
|
2020-10-06 09:48:04 -03:00
|
|
|
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 := context.New(config.Project{
|
|
|
|
ProjectName: "foobar",
|
|
|
|
Env: []string{
|
|
|
|
"FOO=FOO_{{ .Env.BAR }}",
|
|
|
|
"FOOBAR={{.ProjectName}}",
|
2021-06-04 18:11:29 +00:00
|
|
|
"EMPTY_VAL=",
|
2021-05-24 23:23:59 -03:00
|
|
|
},
|
|
|
|
})
|
2021-06-04 18:11:29 +00:00
|
|
|
ctx.Env["FOOBAR"] = "old foobar"
|
2021-05-24 23:23:59 -03:00
|
|
|
os.Setenv("BAR", "lebar")
|
|
|
|
os.Setenv("GITHUB_TOKEN", "fake")
|
|
|
|
require.NoError(t, Pipe{}.Run(ctx))
|
2021-06-04 18:11:29 +00:00
|
|
|
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 := context.New(config.Project{
|
|
|
|
Env: []string{
|
|
|
|
"FOO={{ .Asss }",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1: unexpected "}" in operand`)
|
|
|
|
})
|
2021-09-27 08:13:56 -03:00
|
|
|
|
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
ctx := context.New(config.Project{})
|
|
|
|
require.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
require.Equal(t, ctx.TokenType, context.TokenTypeGitHub)
|
|
|
|
})
|
2018-02-03 00:06:48 -02:00
|
|
|
}
|
|
|
|
|
2019-06-29 16:02:40 +02:00
|
|
|
func TestValidGithubEnv(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Setenv("GITHUB_TOKEN", "asdf"))
|
2021-04-25 14:20:49 -03:00
|
|
|
ctx := &context.Context{
|
2018-02-24 17:59:08 -03:00
|
|
|
Config: config.Project{},
|
2017-03-25 20:40:45 -03:00
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
require.Equal(t, "asdf", ctx.Token)
|
|
|
|
require.Equal(t, context.TokenTypeGitHub, ctx.TokenType)
|
2019-06-29 16:02:40 +02:00
|
|
|
// so the tests do not depend on each other
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
|
2019-06-29 16:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidGitlabEnv(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Setenv("GITLAB_TOKEN", "qwertz"))
|
2021-04-25 14:20:49 -03:00
|
|
|
ctx := &context.Context{
|
2019-06-29 16:02:40 +02:00
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
require.Equal(t, "qwertz", ctx.Token)
|
|
|
|
require.Equal(t, context.TokenTypeGitLab, ctx.TokenType)
|
2019-06-29 16:02:40 +02:00
|
|
|
// so the tests do not depend on each other
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITLAB_TOKEN"))
|
2017-03-25 20:40:45 -03:00
|
|
|
}
|
|
|
|
|
2019-08-26 10:31:38 +03:00
|
|
|
func TestValidGiteaEnv(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Setenv("GITEA_TOKEN", "token"))
|
2021-04-25 14:20:49 -03:00
|
|
|
ctx := &context.Context{
|
2019-08-26 10:31:38 +03:00
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
require.Equal(t, "token", ctx.Token)
|
|
|
|
require.Equal(t, context.TokenTypeGitea, ctx.TokenType)
|
2019-08-26 10:31:38 +03:00
|
|
|
// so the tests do not depend on each other
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITEA_TOKEN"))
|
2019-08-26 10:31:38 +03:00
|
|
|
}
|
|
|
|
|
2017-03-25 20:40:45 -03:00
|
|
|
func TestInvalidEnv(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
|
|
|
|
require.NoError(t, os.Unsetenv("GITLAB_TOKEN"))
|
2021-04-25 14:20:49 -03:00
|
|
|
ctx := &context.Context{
|
2019-06-29 16:02:40 +02:00
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Error(t, Pipe{}.Run(ctx))
|
|
|
|
require.EqualError(t, Pipe{}.Run(ctx), ErrMissingToken.Error())
|
2019-06-29 16:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleEnvTokens(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Setenv("GITHUB_TOKEN", "asdf"))
|
|
|
|
require.NoError(t, os.Setenv("GITLAB_TOKEN", "qwertz"))
|
|
|
|
require.NoError(t, os.Setenv("GITEA_TOKEN", "token"))
|
2021-04-25 14:20:49 -03:00
|
|
|
ctx := &context.Context{
|
2018-02-24 17:59:08 -03:00
|
|
|
Config: config.Project{},
|
2017-03-25 20:40:45 -03:00
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Error(t, Pipe{}.Run(ctx))
|
|
|
|
require.EqualError(t, Pipe{}.Run(ctx), ErrMultipleTokens.Error())
|
2019-06-29 16:02:40 +02:00
|
|
|
// so the tests do not depend on each other
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
|
|
|
|
require.NoError(t, os.Unsetenv("GITLAB_TOKEN"))
|
|
|
|
require.NoError(t, os.Unsetenv("GITEA_TOKEN"))
|
2017-03-25 20:40:45 -03:00
|
|
|
}
|
2017-04-18 13:10:13 -03:00
|
|
|
|
2019-06-29 16:02:40 +02:00
|
|
|
func TestEmptyGithubFileEnv(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
|
2021-04-25 14:20:49 -03:00
|
|
|
ctx := &context.Context{
|
2018-02-24 17:59:08 -03:00
|
|
|
Config: config.Project{},
|
2018-02-03 00:06:48 -02:00
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Error(t, Pipe{}.Run(ctx))
|
2018-02-03 00:06:48 -02:00
|
|
|
}
|
|
|
|
|
2019-06-29 16:02:40 +02:00
|
|
|
func TestEmptyGitlabFileEnv(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITLAB_TOKEN"))
|
2021-04-25 14:20:49 -03:00
|
|
|
ctx := &context.Context{
|
2019-06-29 16:02:40 +02:00
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Error(t, Pipe{}.Run(ctx))
|
2019-06-29 16:02:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 10:31:38 +03:00
|
|
|
func TestEmptyGiteaFileEnv(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITEA_TOKEN"))
|
2021-04-25 14:20:49 -03:00
|
|
|
ctx := &context.Context{
|
2019-08-26 10:31:38 +03:00
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Error(t, Pipe{}.Run(ctx))
|
2019-08-26 10:31:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-29 16:02:40 +02:00
|
|
|
func TestEmptyGithubEnvFile(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
|
2020-12-13 10:33:08 -03:00
|
|
|
f, err := ioutil.TempFile(t.TempDir(), "token")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2021-04-25 11:34:40 -03:00
|
|
|
require.NoError(t, f.Close())
|
2021-04-25 14:20:49 -03:00
|
|
|
require.NoError(t, os.Chmod(f.Name(), 0o377))
|
|
|
|
ctx := &context.Context{
|
2018-02-03 00:06:48 -02:00
|
|
|
Config: config.Project{
|
|
|
|
EnvFiles: config.EnvFiles{
|
|
|
|
GitHubToken: f.Name(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.EqualError(t, Pipe{}.Run(ctx), fmt.Sprintf("failed to load github token: open %s: permission denied", f.Name()))
|
2017-04-29 12:49:22 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 16:02:40 +02:00
|
|
|
func TestEmptyGitlabEnvFile(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITLAB_TOKEN"))
|
2020-12-13 10:33:08 -03:00
|
|
|
f, err := ioutil.TempFile(t.TempDir(), "token")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2021-04-25 11:34:40 -03:00
|
|
|
require.NoError(t, f.Close())
|
2021-04-25 14:20:49 -03:00
|
|
|
require.NoError(t, os.Chmod(f.Name(), 0o377))
|
|
|
|
ctx := &context.Context{
|
2019-06-29 16:02:40 +02:00
|
|
|
Config: config.Project{
|
|
|
|
EnvFiles: config.EnvFiles{
|
|
|
|
GitLabToken: f.Name(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.EqualError(t, Pipe{}.Run(ctx), fmt.Sprintf("failed to load gitlab token: open %s: permission denied", f.Name()))
|
2019-06-29 16:02:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 10:31:38 +03:00
|
|
|
func TestEmptyGiteaEnvFile(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITEA_TOKEN"))
|
2020-12-13 10:33:08 -03:00
|
|
|
f, err := ioutil.TempFile(t.TempDir(), "token")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2021-04-25 11:34:40 -03:00
|
|
|
require.NoError(t, f.Close())
|
2021-04-25 14:20:49 -03:00
|
|
|
require.NoError(t, os.Chmod(f.Name(), 0o377))
|
|
|
|
ctx := &context.Context{
|
2019-08-26 10:31:38 +03:00
|
|
|
Config: config.Project{
|
|
|
|
EnvFiles: config.EnvFiles{
|
|
|
|
GiteaToken: f.Name(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.EqualError(t, Pipe{}.Run(ctx), fmt.Sprintf("failed to load gitea token: open %s: permission denied", f.Name()))
|
2019-08-26 10:31:38 +03:00
|
|
|
}
|
|
|
|
|
2017-05-01 10:23:28 -03:00
|
|
|
func TestInvalidEnvChecksSkipped(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
|
2021-04-25 14:20:49 -03:00
|
|
|
ctx := &context.Context{
|
2018-03-01 01:12:58 -03:00
|
|
|
Config: config.Project{},
|
|
|
|
SkipPublish: true,
|
2017-04-18 13:10:13 -03:00
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, Pipe{}.Run(ctx))
|
2017-04-18 13:10:13 -03:00
|
|
|
}
|
2018-02-03 00:06:48 -02:00
|
|
|
|
2018-05-08 21:03:21 -03:00
|
|
|
func TestInvalidEnvReleaseDisabled(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
|
2021-04-25 14:20:49 -03:00
|
|
|
ctx := &context.Context{
|
2018-05-08 21:03:21 -03:00
|
|
|
Config: config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
Disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, Pipe{}.Run(ctx))
|
2018-05-08 21:03:21 -03:00
|
|
|
}
|
|
|
|
|
2018-02-03 00:06:48 -02:00
|
|
|
func TestLoadEnv(t *testing.T) {
|
2021-01-07 09:15:32 -03:00
|
|
|
t.Run("env exists", func(t *testing.T) {
|
2021-04-25 14:20:49 -03:00
|
|
|
env := "SUPER_SECRET_ENV"
|
2021-01-07 09:15:32 -03:00
|
|
|
require.NoError(t, os.Setenv(env, "1"))
|
2018-02-03 00:06:48 -02:00
|
|
|
v, err := loadEnv(env, "nope")
|
2021-01-07 09:15:32 -03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "1", v)
|
2018-02-03 00:06:48 -02:00
|
|
|
})
|
2021-01-07 09:15:32 -03:00
|
|
|
t.Run("env file exists", func(t *testing.T) {
|
2021-04-25 14:20:49 -03:00
|
|
|
env := "SUPER_SECRET_ENV_NOPE"
|
2021-01-07 09:15:32 -03:00
|
|
|
require.NoError(t, os.Unsetenv(env))
|
2020-12-13 10:33:08 -03:00
|
|
|
f, err := ioutil.TempFile(t.TempDir(), "token")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2018-02-03 00:06:48 -02:00
|
|
|
fmt.Fprintf(f, "123")
|
2021-04-25 11:34:40 -03:00
|
|
|
require.NoError(t, f.Close())
|
2018-02-03 00:06:48 -02:00
|
|
|
v, err := loadEnv(env, f.Name())
|
2021-01-07 09:15:32 -03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "123", v)
|
2018-02-03 00:06:48 -02:00
|
|
|
})
|
2021-01-07 09:15:32 -03:00
|
|
|
t.Run("env file with an empty line at the end", func(t *testing.T) {
|
2021-04-25 14:20:49 -03:00
|
|
|
env := "SUPER_SECRET_ENV_NOPE"
|
2021-01-07 09:15:32 -03:00
|
|
|
require.NoError(t, os.Unsetenv(env))
|
2020-12-13 10:33:08 -03:00
|
|
|
f, err := ioutil.TempFile(t.TempDir(), "token")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2018-02-03 16:51:19 -02:00
|
|
|
fmt.Fprintf(f, "123\n")
|
2021-04-25 11:34:40 -03:00
|
|
|
require.NoError(t, f.Close())
|
2018-02-03 16:51:19 -02:00
|
|
|
v, err := loadEnv(env, f.Name())
|
2021-01-07 09:15:32 -03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "123", v)
|
2018-02-03 16:51:19 -02:00
|
|
|
})
|
2021-01-07 09:15:32 -03:00
|
|
|
t.Run("env file is not readable", func(t *testing.T) {
|
2021-04-25 14:20:49 -03:00
|
|
|
env := "SUPER_SECRET_ENV_NOPE"
|
2021-01-07 09:15:32 -03:00
|
|
|
require.NoError(t, os.Unsetenv(env))
|
2020-12-13 10:33:08 -03:00
|
|
|
f, err := ioutil.TempFile(t.TempDir(), "token")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2018-02-03 00:06:48 -02:00
|
|
|
fmt.Fprintf(f, "123")
|
2021-04-25 11:34:40 -03:00
|
|
|
require.NoError(t, f.Close())
|
2021-04-25 14:20:49 -03:00
|
|
|
err = os.Chmod(f.Name(), 0o377)
|
2021-01-07 09:15:32 -03:00
|
|
|
require.NoError(t, err)
|
2018-02-03 00:06:48 -02:00
|
|
|
v, err := loadEnv(env, f.Name())
|
2021-01-07 09:15:32 -03:00
|
|
|
require.EqualError(t, err, fmt.Sprintf("open %s: permission denied", f.Name()))
|
|
|
|
require.Equal(t, "", v)
|
2018-02-03 00:06:48 -02:00
|
|
|
})
|
|
|
|
}
|