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