github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/pipe/env/env_test.go (about)

     1  package env
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/goreleaser/goreleaser/internal/testlib"
    10  	"github.com/goreleaser/goreleaser/pkg/config"
    11  	"github.com/goreleaser/goreleaser/pkg/context"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestDescription(t *testing.T) {
    16  	assert.NotEmpty(t, Pipe{}.String())
    17  }
    18  
    19  func TestDefault(t *testing.T) {
    20  	t.Run("empty config", func(tt *testing.T) {
    21  		ctx := context.New(config.Project{})
    22  		assert.NoError(t, Pipe{}.Default(ctx))
    23  		assert.Equal(t, "~/.config/goreleaser/github_token", ctx.Config.EnvFiles.GitHubToken)
    24  	})
    25  	t.Run("custom config config", func(tt *testing.T) {
    26  		cfg := "what"
    27  		ctx := context.New(config.Project{
    28  			EnvFiles: config.EnvFiles{
    29  				GitHubToken: cfg,
    30  			},
    31  		})
    32  		assert.NoError(t, Pipe{}.Default(ctx))
    33  		assert.Equal(t, cfg, ctx.Config.EnvFiles.GitHubToken)
    34  	})
    35  }
    36  
    37  func TestValidEnv(t *testing.T) {
    38  	assert.NoError(t, os.Setenv("GITHUB_TOKEN", "asdf"))
    39  	var ctx = &context.Context{
    40  		Config: config.Project{},
    41  	}
    42  	assert.NoError(t, Pipe{}.Run(ctx))
    43  }
    44  
    45  func TestInvalidEnv(t *testing.T) {
    46  	assert.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
    47  	var ctx = &context.Context{
    48  		Config: config.Project{},
    49  	}
    50  	assert.Error(t, Pipe{}.Run(ctx))
    51  }
    52  
    53  func TestEmptyFileEnv(t *testing.T) {
    54  	assert.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
    55  	var ctx = &context.Context{
    56  		Config: config.Project{},
    57  	}
    58  	assert.Error(t, Pipe{}.Run(ctx))
    59  }
    60  
    61  func TestEmptyEnvFile(t *testing.T) {
    62  	assert.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
    63  	f, err := ioutil.TempFile("", "token")
    64  	assert.NoError(t, err)
    65  	assert.NoError(t, os.Chmod(f.Name(), 0377))
    66  	var ctx = &context.Context{
    67  		Config: config.Project{
    68  			EnvFiles: config.EnvFiles{
    69  				GitHubToken: f.Name(),
    70  			},
    71  		},
    72  	}
    73  	assert.EqualError(t, Pipe{}.Run(ctx), fmt.Sprintf("failed to load github token: open %s: permission denied", f.Name()))
    74  }
    75  
    76  func TestInvalidEnvChecksSkipped(t *testing.T) {
    77  	assert.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
    78  	var ctx = &context.Context{
    79  		Config:      config.Project{},
    80  		SkipPublish: true,
    81  	}
    82  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    83  }
    84  
    85  func TestInvalidEnvReleaseDisabled(t *testing.T) {
    86  	assert.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
    87  	var ctx = &context.Context{
    88  		Config: config.Project{
    89  			Release: config.Release{
    90  				Disable: true,
    91  			},
    92  		},
    93  	}
    94  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    95  }
    96  
    97  func TestLoadEnv(t *testing.T) {
    98  	t.Run("env exists", func(tt *testing.T) {
    99  		var env = "SUPER_SECRET_ENV"
   100  		assert.NoError(tt, os.Setenv(env, "1"))
   101  		v, err := loadEnv(env, "nope")
   102  		assert.NoError(tt, err)
   103  		assert.Equal(tt, "1", v)
   104  	})
   105  	t.Run("env file exists", func(tt *testing.T) {
   106  		var env = "SUPER_SECRET_ENV_NOPE"
   107  		assert.NoError(tt, os.Unsetenv(env))
   108  		f, err := ioutil.TempFile("", "token")
   109  		assert.NoError(t, err)
   110  		fmt.Fprintf(f, "123")
   111  		v, err := loadEnv(env, f.Name())
   112  		assert.NoError(tt, err)
   113  		assert.Equal(tt, "123", v)
   114  	})
   115  	t.Run("env file with an empty line at the end", func(tt *testing.T) {
   116  		var env = "SUPER_SECRET_ENV_NOPE"
   117  		assert.NoError(tt, os.Unsetenv(env))
   118  		f, err := ioutil.TempFile("", "token")
   119  		assert.NoError(t, err)
   120  		fmt.Fprintf(f, "123\n")
   121  		v, err := loadEnv(env, f.Name())
   122  		assert.NoError(tt, err)
   123  		assert.Equal(tt, "123", v)
   124  	})
   125  	t.Run("env file is not readable", func(tt *testing.T) {
   126  		var env = "SUPER_SECRET_ENV_NOPE"
   127  		assert.NoError(tt, os.Unsetenv(env))
   128  		f, err := ioutil.TempFile("", "token")
   129  		assert.NoError(t, err)
   130  		fmt.Fprintf(f, "123")
   131  		os.Chmod(f.Name(), 0377)
   132  		v, err := loadEnv(env, f.Name())
   133  		assert.EqualError(tt, err, fmt.Sprintf("open %s: permission denied", f.Name()))
   134  		assert.Equal(tt, "", v)
   135  	})
   136  }