github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/internal/pipe/snapshot/snapshot_test.go (about)

     1  package snapshot
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/goreleaser/goreleaser/pkg/config"
     7  	"github.com/goreleaser/goreleaser/pkg/context"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestStringer(t *testing.T) {
    12  	require.NotEmpty(t, Pipe{}.String())
    13  }
    14  
    15  func TestDefault(t *testing.T) {
    16  	ctx := &context.Context{
    17  		Config: config.Project{
    18  			Snapshot: config.Snapshot{},
    19  		},
    20  	}
    21  	require.NoError(t, Pipe{}.Default(ctx))
    22  	require.Equal(t, "{{ .Version }}-SNAPSHOT-{{ .ShortCommit }}", ctx.Config.Snapshot.NameTemplate)
    23  }
    24  
    25  func TestDefaultSet(t *testing.T) {
    26  	ctx := &context.Context{
    27  		Config: config.Project{
    28  			Snapshot: config.Snapshot{
    29  				NameTemplate: "snap",
    30  			},
    31  		},
    32  	}
    33  	require.NoError(t, Pipe{}.Default(ctx))
    34  	require.Equal(t, "snap", ctx.Config.Snapshot.NameTemplate)
    35  }
    36  
    37  func TestSnapshotInvalidNametemplate(t *testing.T) {
    38  	ctx := context.New(config.Project{
    39  		Snapshot: config.Snapshot{
    40  			NameTemplate: "{{.ShortCommit}{{{sss}}}",
    41  		},
    42  	})
    43  	require.EqualError(t, Pipe{}.Run(ctx), `failed to generate snapshot name: template: tmpl:1: unexpected "}" in operand`)
    44  }
    45  
    46  func TestSnapshotEmptyFinalName(t *testing.T) {
    47  	ctx := context.New(config.Project{
    48  		Snapshot: config.Snapshot{
    49  			NameTemplate: "{{ .Commit }}",
    50  		},
    51  	})
    52  	ctx.Git.CurrentTag = "v1.2.3"
    53  	require.EqualError(t, Pipe{}.Run(ctx), "empty snapshot name")
    54  }
    55  
    56  func TestSnapshot(t *testing.T) {
    57  	ctx := context.New(config.Project{
    58  		Snapshot: config.Snapshot{
    59  			NameTemplate: "{{ incpatch .Tag }}",
    60  		},
    61  	})
    62  	ctx.Git.CurrentTag = "v1.2.3"
    63  	require.NoError(t, Pipe{}.Run(ctx))
    64  	require.Equal(t, "v1.2.4", ctx.Version)
    65  }
    66  
    67  func TestSkip(t *testing.T) {
    68  	t.Run("skip", func(t *testing.T) {
    69  		require.True(t, Pipe{}.Skip(context.New(config.Project{})))
    70  	})
    71  
    72  	t.Run("dont skip", func(t *testing.T) {
    73  		ctx := context.New(config.Project{})
    74  		ctx.Snapshot = true
    75  		require.False(t, Pipe{}.Skip(ctx))
    76  	})
    77  }