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