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