github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/pipe/checksums/checksums_test.go (about)

     1  package checksums
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/goreleaser/goreleaser/internal/artifact"
     9  	"github.com/goreleaser/goreleaser/pkg/config"
    10  	"github.com/goreleaser/goreleaser/pkg/context"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestDescription(t *testing.T) {
    16  	assert.NotEmpty(t, Pipe{}.String())
    17  }
    18  
    19  func TestPipe(t *testing.T) {
    20  	var binary = "binary"
    21  	var checksums = binary + "_bar_checksums.txt"
    22  	folder, err := ioutil.TempDir("", "goreleasertest")
    23  	assert.NoError(t, err)
    24  	var file = filepath.Join(folder, binary)
    25  	assert.NoError(t, ioutil.WriteFile(file, []byte("some string"), 0644))
    26  	var ctx = context.New(
    27  		config.Project{
    28  			Dist:        folder,
    29  			ProjectName: binary,
    30  			Checksum: config.Checksum{
    31  				NameTemplate: "{{ .ProjectName }}_{{ .Env.FOO }}_checksums.txt",
    32  			},
    33  		},
    34  	)
    35  	ctx.Git.CurrentTag = "1.2.3"
    36  	ctx.Env = map[string]string{"FOO": "bar"}
    37  	ctx.Artifacts.Add(artifact.Artifact{
    38  		Name: binary,
    39  		Path: file,
    40  		Type: artifact.UploadableBinary,
    41  	})
    42  	ctx.Artifacts.Add(artifact.Artifact{
    43  		Name: binary + ".tar.gz",
    44  		Path: file,
    45  		Type: artifact.UploadableArchive,
    46  	})
    47  	assert.NoError(t, Pipe{}.Run(ctx))
    48  	var artifacts []string
    49  	for _, a := range ctx.Artifacts.List() {
    50  		artifacts = append(artifacts, a.Name)
    51  	}
    52  	assert.Contains(t, artifacts, checksums, binary)
    53  	bts, err := ioutil.ReadFile(filepath.Join(folder, checksums))
    54  	assert.NoError(t, err)
    55  	assert.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc  binary")
    56  	assert.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc  binary.tar.gz")
    57  }
    58  
    59  func TestPipeFileNotExist(t *testing.T) {
    60  	folder, err := ioutil.TempDir("", "goreleasertest")
    61  	assert.NoError(t, err)
    62  	var ctx = context.New(
    63  		config.Project{
    64  			Dist: folder,
    65  			Checksum: config.Checksum{
    66  				NameTemplate: "checksums.txt",
    67  			},
    68  		},
    69  	)
    70  	ctx.Git.CurrentTag = "1.2.3"
    71  	ctx.Artifacts.Add(artifact.Artifact{
    72  		Name: "nope",
    73  		Path: "/nope",
    74  		Type: artifact.UploadableBinary,
    75  	})
    76  	err = Pipe{}.Run(ctx)
    77  	assert.Error(t, err)
    78  	assert.Contains(t, err.Error(), "/nope: no such file or directory")
    79  }
    80  
    81  func TestPipeInvalidNameTemplate(t *testing.T) {
    82  	for template, eerr := range map[string]string{
    83  		"{{ .Pro }_checksums.txt": `template: tmpl:1: unexpected "}" in operand`,
    84  		"{{.Env.NOPE}}":           `template: tmpl:1:6: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`,
    85  	} {
    86  		t.Run(template, func(tt *testing.T) {
    87  			folder, err := ioutil.TempDir("", "goreleasertest")
    88  			assert.NoError(tt, err)
    89  			var ctx = context.New(
    90  				config.Project{
    91  					Dist:        folder,
    92  					ProjectName: "name",
    93  					Checksum: config.Checksum{
    94  						NameTemplate: template,
    95  					},
    96  				},
    97  			)
    98  			ctx.Git.CurrentTag = "1.2.3"
    99  			ctx.Artifacts.Add(artifact.Artifact{
   100  				Name: "whatever",
   101  				Type: artifact.UploadableBinary,
   102  			})
   103  			err = Pipe{}.Run(ctx)
   104  			assert.Error(tt, err)
   105  			assert.Equal(tt, eerr, err.Error())
   106  		})
   107  	}
   108  }
   109  
   110  func TestPipeCouldNotOpenChecksumsTxt(t *testing.T) {
   111  	folder, err := ioutil.TempDir("", "goreleasertest")
   112  	assert.NoError(t, err)
   113  	var file = filepath.Join(folder, "checksums.txt")
   114  	assert.NoError(t, ioutil.WriteFile(file, []byte("some string"), 0000))
   115  	var ctx = context.New(
   116  		config.Project{
   117  			Dist: folder,
   118  			Checksum: config.Checksum{
   119  				NameTemplate: "checksums.txt",
   120  			},
   121  		},
   122  	)
   123  	ctx.Git.CurrentTag = "1.2.3"
   124  	ctx.Artifacts.Add(artifact.Artifact{
   125  		Name: "whatever",
   126  		Type: artifact.UploadableBinary,
   127  	})
   128  	err = Pipe{}.Run(ctx)
   129  	assert.Error(t, err)
   130  	assert.Contains(t, err.Error(), "/checksums.txt: permission denied")
   131  }
   132  
   133  func TestDefault(t *testing.T) {
   134  	var ctx = &context.Context{
   135  		Config: config.Project{
   136  			Checksum: config.Checksum{},
   137  		},
   138  	}
   139  	assert.NoError(t, Pipe{}.Default(ctx))
   140  	assert.Equal(
   141  		t,
   142  		"{{ .ProjectName }}_{{ .Version }}_checksums.txt",
   143  		ctx.Config.Checksum.NameTemplate,
   144  	)
   145  }
   146  
   147  func TestDefaultSet(t *testing.T) {
   148  	var ctx = &context.Context{
   149  		Config: config.Project{
   150  			Checksum: config.Checksum{
   151  				NameTemplate: "checksums.txt",
   152  			},
   153  		},
   154  	}
   155  	assert.NoError(t, Pipe{}.Default(ctx))
   156  	assert.Equal(t, "checksums.txt", ctx.Config.Checksum.NameTemplate)
   157  }