github.com/windmeup/goreleaser@v1.21.95/internal/pipe/before/before_test.go (about) 1 package before 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/caarlos0/log" 9 "github.com/stretchr/testify/require" 10 "github.com/windmeup/goreleaser/internal/skips" 11 "github.com/windmeup/goreleaser/internal/testctx" 12 "github.com/windmeup/goreleaser/internal/testlib" 13 "github.com/windmeup/goreleaser/pkg/config" 14 ) 15 16 func TestMain(m *testing.M) { 17 log.SetLevel(log.DebugLevel) 18 defer log.SetLevel(log.InfoLevel) 19 os.Exit(m.Run()) 20 } 21 22 func TestDescription(t *testing.T) { 23 require.NotEmpty(t, Pipe{}.String()) 24 } 25 26 func TestRunPipe(t *testing.T) { 27 for _, tc := range [][]string{ 28 nil, 29 {}, 30 {"go version"}, 31 {"go version", "go list"}, 32 {`bash -c "go version; echo \"lala spaces and such\""`}, 33 } { 34 ctx := testctx.NewWithCfg( 35 config.Project{ 36 Before: config.Before{ 37 Hooks: tc, 38 }, 39 }, 40 ) 41 require.NoError(t, Pipe{}.Run(ctx)) 42 } 43 } 44 45 func TestRunPipeInvalidCommand(t *testing.T) { 46 ctx := testctx.NewWithCfg( 47 config.Project{ 48 Before: config.Before{ 49 Hooks: []string{`bash -c "echo \"unterminated command\"`}, 50 }, 51 }, 52 ) 53 require.EqualError(t, Pipe{}.Run(ctx), "invalid command line string") 54 } 55 56 func TestRunPipeFail(t *testing.T) { 57 for err, tc := range map[string][]string{ 58 "hook failed: go tool foobar: exit status 2; output: go: no such tool \"foobar\"\n": {"go tool foobar"}, 59 "hook failed: sh ./testdata/foo.sh: exit status 1; output: lalala\n": {"sh ./testdata/foo.sh"}, 60 } { 61 ctx := testctx.NewWithCfg( 62 config.Project{ 63 Before: config.Before{ 64 Hooks: tc, 65 }, 66 }, 67 ) 68 require.EqualError(t, Pipe{}.Run(ctx), err) 69 } 70 } 71 72 func TestRunWithEnv(t *testing.T) { 73 f := filepath.Join(t.TempDir(), "testfile") 74 require.NoError(t, Pipe{}.Run(testctx.NewWithCfg( 75 config.Project{ 76 Env: []string{ 77 "TEST_FILE=" + f, 78 }, 79 Before: config.Before{ 80 Hooks: []string{"touch {{ .Env.TEST_FILE }}"}, 81 }, 82 }, 83 ))) 84 require.FileExists(t, f) 85 } 86 87 func TestInvalidTemplate(t *testing.T) { 88 testlib.RequireTemplateError(t, Pipe{}.Run(testctx.NewWithCfg( 89 config.Project{ 90 Before: config.Before{ 91 Hooks: []string{"touch {{ .fasdsd }"}, 92 }, 93 }, 94 ))) 95 } 96 97 func TestSkip(t *testing.T) { 98 t.Run("skip", func(t *testing.T) { 99 require.True(t, Pipe{}.Skip(testctx.New())) 100 }) 101 102 t.Run("skip before", func(t *testing.T) { 103 ctx := testctx.NewWithCfg(config.Project{ 104 Before: config.Before{ 105 Hooks: []string{""}, 106 }, 107 }, testctx.Skip(skips.Before)) 108 require.True(t, Pipe{}.Skip(ctx)) 109 }) 110 111 t.Run("dont skip", func(t *testing.T) { 112 ctx := testctx.NewWithCfg(config.Project{ 113 Before: config.Before{ 114 Hooks: []string{""}, 115 }, 116 }) 117 require.False(t, Pipe{}.Skip(ctx)) 118 }) 119 }