github.com/windmeup/goreleaser@v1.21.95/cmd/util_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	"github.com/windmeup/goreleaser/internal/testlib"
     9  )
    10  
    11  type exitMemento struct {
    12  	code int
    13  }
    14  
    15  func (e *exitMemento) Exit(i int) {
    16  	e.code = i
    17  }
    18  
    19  func setup(tb testing.TB) string {
    20  	tb.Helper()
    21  
    22  	_ = os.Unsetenv("GITHUB_TOKEN")
    23  	_ = os.Unsetenv("GITLAB_TOKEN")
    24  
    25  	previous, err := os.Getwd()
    26  	require.NoError(tb, err)
    27  
    28  	tb.Cleanup(func() {
    29  		require.NoError(tb, os.Chdir(previous))
    30  	})
    31  
    32  	folder := tb.TempDir()
    33  	require.NoError(tb, os.Chdir(folder))
    34  
    35  	createGoReleaserYaml(tb)
    36  	createMainGo(tb)
    37  	goModInit(tb)
    38  	testlib.GitInit(tb)
    39  	testlib.GitAdd(tb)
    40  	testlib.GitCommit(tb, "asdf")
    41  	testlib.GitTag(tb, "v0.0.1")
    42  	testlib.GitCommit(tb, "asas89d")
    43  	testlib.GitCommit(tb, "assssf")
    44  	testlib.GitCommit(tb, "assd")
    45  	testlib.GitTag(tb, "v0.0.2")
    46  	testlib.GitRemoteAdd(tb, "git@github.com:goreleaser/fake.git")
    47  
    48  	return folder
    49  }
    50  
    51  func createFile(tb testing.TB, filename, contents string) {
    52  	tb.Helper()
    53  	require.NoError(tb, os.WriteFile(filename, []byte(contents), 0o644))
    54  }
    55  
    56  func createMainGo(tb testing.TB) {
    57  	tb.Helper()
    58  	createFile(tb, "main.go", "package main\nfunc main() {println(0)}")
    59  }
    60  
    61  func goModInit(tb testing.TB) {
    62  	tb.Helper()
    63  	createFile(tb, "go.mod", `module foo
    64  
    65  go 1.21
    66  `)
    67  }
    68  
    69  func createGoReleaserYaml(tb testing.TB) {
    70  	tb.Helper()
    71  	yaml := `build:
    72    binary: fake
    73    goos:
    74      - linux
    75    goarch:
    76      - amd64
    77  release:
    78    github:
    79      owner: goreleaser
    80      name: fake
    81  `
    82  	createFile(tb, "goreleaser.yml", yaml)
    83  }