github.com/amane3/goreleaser@v0.182.0/internal/pipe/before/before_test.go (about)

     1  package before
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/amane3/goreleaser/pkg/config"
     8  	"github.com/amane3/goreleaser/pkg/context"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestDescription(t *testing.T) {
    13  	require.NotEmpty(t, Pipe{}.String())
    14  }
    15  
    16  func TestRunPipe(t *testing.T) {
    17  	for _, tc := range [][]string{
    18  		nil,
    19  		{},
    20  		{"go version"},
    21  		{"go version", "go list"},
    22  		{`bash -c "go version; echo \"lala spaces and such\""`},
    23  	} {
    24  		ctx := context.New(
    25  			config.Project{
    26  				Before: config.Before{
    27  					Hooks: tc,
    28  				},
    29  			},
    30  		)
    31  		require.NoError(t, Pipe{}.Run(ctx))
    32  	}
    33  }
    34  
    35  func TestRunPipeInvalidCommand(t *testing.T) {
    36  	ctx := context.New(
    37  		config.Project{
    38  			Before: config.Before{
    39  				Hooks: []string{`bash -c "echo \"unterminated command\"`},
    40  			},
    41  		},
    42  	)
    43  	require.EqualError(t, Pipe{}.Run(ctx), "invalid command line string")
    44  }
    45  
    46  func TestRunPipeFail(t *testing.T) {
    47  	for err, tc := range map[string][]string{
    48  		"hook failed: go tool foobar: exit status 2; output: go tool: no such tool \"foobar\"\n": {"go tool foobar"},
    49  		"hook failed: sh ./testdata/foo.sh: exit status 1; output: lalala\n":                     {"sh ./testdata/foo.sh"},
    50  	} {
    51  		ctx := context.New(
    52  			config.Project{
    53  				Before: config.Before{
    54  					Hooks: tc,
    55  				},
    56  			},
    57  		)
    58  		require.EqualError(t, Pipe{}.Run(ctx), err)
    59  	}
    60  }
    61  
    62  func TestRunWithEnv(t *testing.T) {
    63  	var f = filepath.Join(t.TempDir(), "testfile")
    64  	require.NoError(t, Pipe{}.Run(context.New(
    65  		config.Project{
    66  			Env: []string{
    67  				"TEST_FILE=" + f,
    68  			},
    69  			Before: config.Before{
    70  				Hooks: []string{"touch {{ .Env.TEST_FILE }}"},
    71  			},
    72  		},
    73  	)))
    74  	require.FileExists(t, f)
    75  }
    76  
    77  func TestInvalidTemplate(t *testing.T) {
    78  	require.EqualError(t, Pipe{}.Run(context.New(
    79  		config.Project{
    80  			Before: config.Before{
    81  				Hooks: []string{"touch {{ .fasdsd }"},
    82  			},
    83  		},
    84  	)), `template: tmpl:1: unexpected "}" in operand`)
    85  }