1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-26 04:22:05 +02:00
goreleaser/pipeline/env/env_test.go

125 lines
3.3 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"
2018-02-03 00:06:48 -02:00
"io/ioutil"
2017-03-25 20:40:45 -03:00
"os"
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
2017-08-20 16:50:34 -03:00
"github.com/goreleaser/goreleaser/internal/testlib"
2017-03-25 20:40:45 -03:00
"github.com/stretchr/testify/assert"
)
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.String())
2017-03-25 20:40:45 -03:00
}
2018-02-03 00:06:48 -02:00
func TestDefault(t *testing.T) {
t.Run("empty config", func(tt *testing.T) {
ctx := context.New(config.Project{})
assert.NoError(t, Pipe{}.Default(ctx))
assert.Equal(t, "~/.config/goreleaser/github_token", ctx.Config.EnvFiles.GitHubToken)
})
t.Run("custom config config", func(tt *testing.T) {
cfg := "what"
ctx := context.New(config.Project{
EnvFiles: config.EnvFiles{
GitHubToken: cfg,
},
})
assert.NoError(t, Pipe{}.Default(ctx))
assert.Equal(t, cfg, ctx.Config.EnvFiles.GitHubToken)
})
}
2017-03-25 20:40:45 -03:00
func TestValidEnv(t *testing.T) {
assert.NoError(t, os.Setenv("GITHUB_TOKEN", "asdf"))
2017-03-25 20:40:45 -03:00
var ctx = &context.Context{
Config: config.Project{},
2017-03-25 20:40:45 -03:00
}
assert.NoError(t, Pipe{}.Run(ctx))
2017-03-25 20:40:45 -03:00
}
func TestInvalidEnv(t *testing.T) {
assert.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
2017-03-25 20:40:45 -03:00
var ctx = &context.Context{
Config: config.Project{},
2017-03-25 20:40:45 -03:00
}
assert.Error(t, Pipe{}.Run(ctx))
2017-03-25 20:40:45 -03:00
}
2018-02-03 00:06:48 -02:00
func TestEmptyFileEnv(t *testing.T) {
assert.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
var ctx = &context.Context{
Config: config.Project{},
2018-02-03 00:06:48 -02:00
}
assert.Error(t, Pipe{}.Run(ctx))
}
func TestEmptyEnvFile(t *testing.T) {
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
}
2017-05-01 10:23:28 -03:00
func TestInvalidEnvChecksSkipped(t *testing.T) {
assert.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
var ctx = &context.Context{
Config: config.Project{},
SkipPublish: true,
}
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
}
2018-02-03 00: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 16: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 00: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")
os.Chmod(f.Name(), 0377)
v, err := loadEnv(env, f.Name())
assert.EqualError(tt, err, fmt.Sprintf("open %s: permission denied", f.Name()))
assert.Equal(tt, "", v)
})
}