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