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"
2024-05-26 15:02:57 -03:00
"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"
2020-10-06 09:48:04 -03:00
"github.com/stretchr/testify/require"
2017-03-25 20:40:45 -03:00
)
2023-04-05 16:33:22 -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
}
}
2024-11-09 20:10:14 +02:00
m . Run ( )
2023-04-05 16:33:22 -03:00
for k , v := range restores {
_ = os . Setenv ( k , v )
}
}
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 ) {
2023-03-02 00:01:11 -03:00
ctx := testctx . New ( )
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"
2023-03-02 00:01:11 -03:00
ctx := testctx . NewWithCfg ( config . Project {
2018-02-03 00:06:48 -02:00
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 ) {
2023-03-02 00:01:11 -03:00
ctx := testctx . NewWithCfg ( config . Project {
2021-05-24 23:23:59 -03:00
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"
2023-04-05 16:33:22 -03:00
t . Setenv ( "BAR" , "lebar" )
t . Setenv ( "GITHUB_TOKEN" , "fake" )
2021-05-24 23:23:59 -03:00
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 ) {
2023-03-02 00:01:11 -03:00
ctx := testctx . NewWithCfg ( config . Project {
2021-05-24 23:23:59 -03:00
Env : [ ] string {
"FOO={{ .Asss }" ,
} ,
} )
2023-08-24 22:06:12 -03:00
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 ) {
2023-04-05 16:33:22 -03:00
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" )
2023-03-02 00:01:11 -03:00
ctx := testctx . New ( )
2021-09-27 08:13:56 -03:00
require . NoError ( t , Pipe { } . Run ( ctx ) )
2023-04-05 16:33:22 -03:00
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
}
2019-06-29 16:02:40 +02:00
func TestValidGithubEnv ( t * testing . T ) {
2023-04-05 16:33:22 -03:00
t . Setenv ( "GITHUB_TOKEN" , "asdf" )
2023-03-02 00:01:11 -03:00
ctx := testctx . New ( )
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
}
func TestValidGitlabEnv ( t * testing . T ) {
2023-04-05 16:33:22 -03:00
t . Setenv ( "GITLAB_TOKEN" , "qwertz" )
2023-03-02 00:01:11 -03:00
ctx := testctx . New ( )
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 )
2017-03-25 20:40:45 -03:00
}
2019-08-26 10:31:38 +03:00
func TestValidGiteaEnv ( t * testing . T ) {
2023-04-05 16:33:22 -03:00
t . Setenv ( "GITEA_TOKEN" , "token" )
2023-03-02 00:01:11 -03:00
ctx := testctx . New ( )
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
}
2017-03-25 20:40:45 -03:00
func TestInvalidEnv ( t * testing . T ) {
2023-03-02 00:01:11 -03:00
ctx := testctx . New ( )
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 ) {
2023-04-05 16:33:22 -03:00
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" )
2023-03-02 00:01:11 -03:00
ctx := testctx . New ( )
2020-10-06 09:48:04 -03:00
require . Error ( t , Pipe { } . Run ( ctx ) )
2021-12-05 22:25:29 -03: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" )
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 ) {
2023-03-02 00:01:11 -03:00
ctx := testctx . New ( )
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 ) {
2023-03-02 00:01:11 -03:00
ctx := testctx . New ( )
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 ) {
2023-03-02 00:01:11 -03:00
ctx := testctx . New ( )
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 ) {
2021-11-21 15:10:08 +01:00
f , err := os . CreateTemp ( 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 ( ) , 0 o377 ) )
2023-03-02 00:01:11 -03:00
ctx := testctx . NewWithCfg ( config . Project {
EnvFiles : config . EnvFiles {
GitHubToken : f . Name ( ) ,
2018-02-03 00:06:48 -02:00
} ,
2023-03-02 00:01:11 -03:00
} )
2023-06-18 11:58:17 -03:00
err = Pipe { } . Run ( ctx )
2024-11-16 10:30:39 -03:00
requireErrAccess ( t , err )
2024-02-19 21:28:06 +02:00
require . ErrorContains ( t , err , "failed to load github token" )
2017-04-29 12:49:22 +02:00
}
2019-06-29 16:02:40 +02:00
func TestEmptyGitlabEnvFile ( t * testing . T ) {
2021-11-21 15:10:08 +01:00
f , err := os . CreateTemp ( 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 ( ) , 0 o377 ) )
2023-03-02 00:01:11 -03:00
ctx := testctx . NewWithCfg ( config . Project {
EnvFiles : config . EnvFiles {
GitLabToken : f . Name ( ) ,
2019-06-29 16:02:40 +02:00
} ,
2023-03-02 00:01:11 -03:00
} )
2023-06-18 11:58:17 -03:00
err = Pipe { } . Run ( ctx )
2024-11-16 10:30:39 -03:00
requireErrAccess ( t , err )
2024-02-19 21:28:06 +02:00
require . ErrorContains ( t , err , "failed to load gitlab token" )
2019-06-29 16:02:40 +02:00
}
2019-08-26 10:31:38 +03:00
func TestEmptyGiteaEnvFile ( t * testing . T ) {
2021-11-21 15:10:08 +01:00
f , err := os . CreateTemp ( 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 ( ) , 0 o377 ) )
2023-03-02 00:01:11 -03:00
ctx := testctx . NewWithCfg ( config . Project {
EnvFiles : config . EnvFiles {
GiteaToken : f . Name ( ) ,
2019-08-26 10:31:38 +03:00
} ,
2023-03-02 00:01:11 -03:00
} )
2023-06-18 11:58:17 -03:00
err = Pipe { } . Run ( ctx )
2024-11-16 10:30:39 -03:00
requireErrAccess ( t , err )
2024-02-19 21:28:06 +02:00
require . ErrorContains ( t , err , "failed to load gitea token" )
2019-08-26 10:31:38 +03:00
}
2017-05-01 10:23:28 -03:00
func TestInvalidEnvChecksSkipped ( t * testing . T ) {
2023-09-16 17:01:20 -03:00
ctx := testctx . New ( testctx . Skip ( skips . Publish ) )
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 ) {
2023-01-23 22:27:01 -03:00
t . Run ( "true" , func ( t * testing . T ) {
2023-03-02 00:01:11 -03:00
ctx := testctx . NewWithCfg ( config . Project {
2023-01-23 22:27:01 -03:00
Env : [ ] string { } ,
2018-05-08 21:03:21 -03:00
Release : config . Release {
2023-01-23 22:27:01 -03:00
Disable : "true" ,
2018-05-08 21:03:21 -03:00
} ,
2023-01-23 22:27:01 -03:00
} )
require . NoError ( t , Pipe { } . Run ( ctx ) )
} )
t . Run ( "tmpl true" , func ( t * testing . T ) {
2023-03-02 00:01:11 -03:00
ctx := testctx . NewWithCfg ( config . Project {
2023-01-23 22:27:01 -03:00
Env : [ ] string { "FOO=true" } ,
Release : config . Release {
Disable : "{{ .Env.FOO }}" ,
} ,
} )
require . NoError ( t , Pipe { } . Run ( ctx ) )
} )
t . Run ( "tmpl false" , func ( t * testing . T ) {
2023-03-02 00:01:11 -03:00
ctx := testctx . NewWithCfg ( config . Project {
2023-01-23 22:27:01 -03:00
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 ) {
2023-03-02 00:01:11 -03:00
ctx := testctx . NewWithCfg ( config . Project {
2023-01-23 22:27:01 -03:00
Release : config . Release {
Disable : "{{ .Env.FOO }}" ,
} ,
} )
2023-01-28 23:21:43 -03:00
testlib . RequireTemplateError ( t , Pipe { } . Run ( ctx ) )
2023-01-23 22:27:01 -03:00
} )
}
2018-02-03 00:06:48 -02:00
func TestLoadEnv ( t * testing . T ) {
2023-04-05 16:33:22 -03:00
const env = "SUPER_SECRET_ENV_NOPE"
2021-01-07 09:15:32 -03:00
t . Run ( "env exists" , func ( t * testing . T ) {
2023-04-05 16:33:22 -03:00
t . 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-11-21 15:10:08 +01:00
f , err := os . CreateTemp ( 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-11-21 15:10:08 +01:00
f , err := os . CreateTemp ( 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 ) {
2024-11-16 10:57:48 -03:00
testlib . SkipIfWindows ( t , "permissions work differently in windows" )
2021-11-21 15:10:08 +01:00
f , err := os . CreateTemp ( 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 ( ) , 0 o377 )
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
} )
}
2024-11-16 10:30:39 -03: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 )
}