github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/tmpl/tmpl_test.go (about)

     1  package tmpl
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/goreleaser/goreleaser/internal/artifact"
     7  	"github.com/goreleaser/goreleaser/pkg/config"
     8  	"github.com/goreleaser/goreleaser/pkg/context"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestWithArtifact(t *testing.T) {
    13  	var ctx = context.New(config.Project{
    14  		ProjectName: "proj",
    15  	})
    16  	ctx.Env = map[string]string{
    17  		"FOO": "bar",
    18  	}
    19  	ctx.Version = "1.0.0"
    20  	ctx.Git.CurrentTag = "v1.0.0"
    21  	ctx.Git.Commit = "commit"
    22  	ctx.Git.FullCommit = "fullcommit"
    23  	ctx.Git.ShortCommit = "shortcommit"
    24  	for expect, tmpl := range map[string]string{
    25  		"bar":         "{{.Env.FOO}}",
    26  		"Linux":       "{{.Os}}",
    27  		"amd64":       "{{.Arch}}",
    28  		"6":           "{{.Arm}}",
    29  		"1.0.0":       "{{.Version}}",
    30  		"v1.0.0":      "{{.Tag}}",
    31  		"commit":      "{{.Commit}}",
    32  		"fullcommit":  "{{.FullCommit}}",
    33  		"shortcommit": "{{.ShortCommit}}",
    34  		"binary":      "{{.Binary}}",
    35  		"proj":        "{{.ProjectName}}",
    36  	} {
    37  		tmpl := tmpl
    38  		expect := expect
    39  		t.Run(expect, func(tt *testing.T) {
    40  			tt.Parallel()
    41  			result, err := New(ctx).WithArtifact(
    42  				artifact.Artifact{
    43  					Name:   "not-this-binary",
    44  					Goarch: "amd64",
    45  					Goos:   "linux",
    46  					Goarm:  "6",
    47  					Extra: map[string]string{
    48  						"Binary": "binary",
    49  					},
    50  				},
    51  				map[string]string{"linux": "Linux"},
    52  			).Apply(tmpl)
    53  			assert.NoError(tt, err)
    54  			assert.Equal(tt, expect, result)
    55  		})
    56  	}
    57  
    58  	t.Run("artifact without binary name", func(tt *testing.T) {
    59  		tt.Parallel()
    60  		result, err := New(ctx).WithArtifact(
    61  			artifact.Artifact{
    62  				Name:   "another-binary",
    63  				Goarch: "amd64",
    64  				Goos:   "linux",
    65  				Goarm:  "6",
    66  			}, map[string]string{},
    67  		).Apply("{{ .Binary }}")
    68  		assert.NoError(tt, err)
    69  		assert.Equal(tt, ctx.Config.ProjectName, result)
    70  	})
    71  
    72  	t.Run("template using artifact fields with no artifact", func(tt *testing.T) {
    73  		tt.Parallel()
    74  		result, err := New(ctx).Apply("{{ .Os }}")
    75  		assert.EqualError(tt, err, `template: tmpl:1:3: executing "tmpl" at <.Os>: map has no entry for key "Os"`)
    76  		assert.Empty(tt, result)
    77  	})
    78  }
    79  
    80  func TestEnv(t *testing.T) {
    81  	testCases := []struct {
    82  		desc string
    83  		in   string
    84  		out  string
    85  	}{
    86  		{
    87  			desc: "with env",
    88  			in:   "{{ .Env.FOO }}",
    89  			out:  "BAR",
    90  		},
    91  		{
    92  			desc: "with env",
    93  			in:   "{{ .Env.BAR }}",
    94  			out:  "",
    95  		},
    96  	}
    97  	var ctx = context.New(config.Project{})
    98  	ctx.Env = map[string]string{
    99  		"FOO": "BAR",
   100  	}
   101  	ctx.Git.CurrentTag = "v1.2.3"
   102  	for _, tC := range testCases {
   103  		t.Run(tC.desc, func(t *testing.T) {
   104  			out, _ := New(ctx).Apply(tC.in)
   105  			assert.Equal(t, tC.out, out)
   106  		})
   107  	}
   108  }
   109  
   110  func TestFuncMap(t *testing.T) {
   111  	var ctx = context.New(config.Project{
   112  		ProjectName: "proj",
   113  	})
   114  	ctx.Git.CurrentTag = "v1.2.4"
   115  	for _, tc := range []struct {
   116  		Template string
   117  		Name     string
   118  	}{
   119  		{
   120  			Template: `{{ time "2006-01-02" }}`,
   121  			Name:     "YYYY-MM-DD",
   122  		},
   123  		{
   124  			Template: `{{ time "01/02/2006" }}`,
   125  			Name:     "MM/DD/YYYY",
   126  		},
   127  		{
   128  			Template: `{{ time "01/02/2006" }}`,
   129  			Name:     "MM/DD/YYYY",
   130  		},
   131  	} {
   132  		out, err := New(ctx).Apply(tc.Template)
   133  		assert.NoError(t, err)
   134  		assert.NotEmpty(t, out)
   135  	}
   136  }
   137  
   138  func TestInvalidTemplate(t *testing.T) {
   139  	_, err := New(context.New(config.Project{})).Apply("{{{.Foo}")
   140  	assert.EqualError(t, err, "template: tmpl:1: unexpected \"{\" in command")
   141  }
   142  
   143  func TestEnvNotFound(t *testing.T) {
   144  	var ctx = context.New(config.Project{})
   145  	ctx.Git.CurrentTag = "v1.2.4"
   146  	result, err := New(ctx).Apply("{{.Env.FOO}}")
   147  	assert.Empty(t, result)
   148  	assert.EqualError(t, err, `template: tmpl:1:6: executing "tmpl" at <.Env.FOO>: map has no entry for key "FOO"`)
   149  }
   150  
   151  // This should actually never happen...
   152  func TestInvalidSemver(t *testing.T) {
   153  	var ctx = context.New(config.Project{})
   154  	ctx.Git.CurrentTag = "v1_2_3"
   155  	result, err := New(ctx).Apply("{{.Major}}")
   156  	assert.Empty(t, result)
   157  	assert.EqualError(t, err, `tmpl: Invalid Semantic Version`)
   158  }