github.com/sergiusens/goreleaser@v0.34.3-0.20171009111917-ae6f7c157c5c/internal/name/name_test.go (about)

     1  package name
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/goreleaser/goreleaser/config"
     7  	"github.com/goreleaser/goreleaser/context"
     8  	"github.com/goreleaser/goreleaser/internal/buildtarget"
     9  	"github.com/goreleaser/goreleaser/pipeline/defaults"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestChecksums(t *testing.T) {
    14  	var config = config.Project{
    15  		Checksum: config.Checksum{
    16  			NameTemplate: "{{.ProjectName }}_{{.Tag}}_{{.Version}}_checksums.txt",
    17  		},
    18  		ProjectName: "testcheck",
    19  	}
    20  	var ctx = &context.Context{
    21  		Config:  config,
    22  		Version: "1.0.0",
    23  		Git: context.GitInfo{
    24  			CurrentTag: "v1.0.0",
    25  		},
    26  	}
    27  
    28  	name, err := ForChecksums(ctx)
    29  	assert.NoError(t, err)
    30  	assert.Equal(t, "testcheck_v1.0.0_1.0.0_checksums.txt", name)
    31  }
    32  
    33  func TestNameFor(t *testing.T) {
    34  	var config = config.Project{
    35  		Archive: config.Archive{
    36  			NameTemplate: "{{.Binary}}_{{.Os}}_{{.Arch}}_{{.Tag}}_{{.Version}}",
    37  			Replacements: map[string]string{
    38  				"darwin": "Darwin",
    39  				"amd64":  "x86_64",
    40  			},
    41  		},
    42  		ProjectName: "test",
    43  	}
    44  	var ctx = &context.Context{
    45  		Config:  config,
    46  		Version: "1.2.3",
    47  		Git: context.GitInfo{
    48  			CurrentTag: "v1.2.3",
    49  		},
    50  	}
    51  
    52  	name, err := For(ctx, buildtarget.New("darwin", "amd64", ""))
    53  	assert.NoError(t, err)
    54  	assert.Equal(t, "test_Darwin_x86_64_v1.2.3_1.2.3", name)
    55  }
    56  
    57  func TestNameForBuild(t *testing.T) {
    58  	var ctx = &context.Context{
    59  		Config: config.Project{
    60  			Archive: config.Archive{
    61  				NameTemplate: "{{.Binary}}_{{.Os}}_{{.Arch}}_{{.Tag}}_{{.Version}}",
    62  				Replacements: map[string]string{
    63  					"darwin": "Darwin",
    64  					"amd64":  "x86_64",
    65  				},
    66  			},
    67  			ProjectName: "test",
    68  		},
    69  		Version: "1.2.3",
    70  		Git: context.GitInfo{
    71  			CurrentTag: "v1.2.3",
    72  		},
    73  	}
    74  
    75  	name, err := ForBuild(
    76  		ctx,
    77  		config.Build{Binary: "foo"},
    78  		buildtarget.New("darwin", "amd64", ""),
    79  	)
    80  	assert.NoError(t, err)
    81  	assert.Equal(t, "foo_Darwin_x86_64_v1.2.3_1.2.3", name)
    82  }
    83  
    84  func TestInvalidNameTemplate(t *testing.T) {
    85  	var ctx = &context.Context{
    86  		Config: config.Project{
    87  			Archive: config.Archive{
    88  				NameTemplate: "{{.Binary}_{{.Os}}_{{.Arch}}_{{.Version}}",
    89  			},
    90  			ProjectName: "test",
    91  		},
    92  		Git: context.GitInfo{
    93  			CurrentTag: "v1.2.3",
    94  		},
    95  	}
    96  
    97  	_, err := For(ctx, buildtarget.New("darwin", "amd64", ""))
    98  	assert.Error(t, err)
    99  }
   100  
   101  func TestNameDefaultTemplate(t *testing.T) {
   102  	var ctx = &context.Context{
   103  		Config: config.Project{
   104  			Archive: config.Archive{
   105  				NameTemplate: defaults.NameTemplate,
   106  			},
   107  			ProjectName: "test",
   108  		},
   109  		Version: "1.2.3",
   110  	}
   111  	for key, target := range map[string]buildtarget.Target{
   112  		"test_1.2.3_darwin_amd64": buildtarget.New("darwin", "amd64", ""),
   113  		"test_1.2.3_linux_arm64":  buildtarget.New("linux", "arm64", ""),
   114  		"test_1.2.3_linux_armv7":  buildtarget.New("linux", "arm", "7"),
   115  	} {
   116  		t.Run(key, func(t *testing.T) {
   117  			name, err := For(ctx, target)
   118  			assert.NoError(t, err)
   119  			assert.Equal(t, key, name)
   120  		})
   121  	}
   122  }