github.com/goreleaser/goreleaser@v1.25.1/internal/pipe/gomod/gomod_test.go (about)

     1  package gomod
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/goreleaser/goreleaser/internal/testctx"
    11  	"github.com/goreleaser/goreleaser/internal/testlib"
    12  	"github.com/goreleaser/goreleaser/pkg/config"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestRun(t *testing.T) {
    17  	ctx := testctx.New()
    18  	require.NoError(t, Pipe{}.Default(ctx))
    19  	require.NoError(t, Pipe{}.Run(ctx))
    20  	require.Equal(t, "github.com/goreleaser/goreleaser", ctx.ModulePath)
    21  }
    22  
    23  func TestRunGoWork(t *testing.T) {
    24  	dir := testlib.Mktmp(t)
    25  	require.NoError(t, os.WriteFile(
    26  		filepath.Join(dir, "go.mod"),
    27  		[]byte("module a"),
    28  		0o666,
    29  	))
    30  	require.NoError(t, os.Mkdir(filepath.Join(dir, "b"), 0o755))
    31  	require.NoError(t, os.WriteFile(
    32  		filepath.Join(dir, "b", "go.mod"),
    33  		[]byte("module a/b"),
    34  		0o666,
    35  	))
    36  	require.NoError(t, os.WriteFile(
    37  		filepath.Join(dir, "go.work"),
    38  		[]byte("use (\n\t.\n\tb\n)"),
    39  		0o666,
    40  	))
    41  	out, err := exec.Command("go", "list", "-m").CombinedOutput()
    42  	require.NoError(t, err)
    43  	require.Equal(t, "a\na/b", strings.TrimSpace(string(out)))
    44  	ctx := testctx.New()
    45  	require.NoError(t, Pipe{}.Default(ctx))
    46  	require.NoError(t, Pipe{}.Run(ctx))
    47  	require.Equal(t, "a", ctx.ModulePath)
    48  }
    49  
    50  func TestRunCustomMod(t *testing.T) {
    51  	ctx := testctx.NewWithCfg(config.Project{
    52  		GoMod: config.GoMod{
    53  			Mod: "readonly",
    54  		},
    55  	})
    56  	require.NoError(t, Pipe{}.Default(ctx))
    57  	require.NoError(t, Pipe{}.Run(ctx))
    58  	require.Equal(t, "github.com/goreleaser/goreleaser", ctx.ModulePath)
    59  }
    60  
    61  func TestCustomEnv(t *testing.T) {
    62  	bin := filepath.Join(t.TempDir(), "go.bin")
    63  	require.NoError(t, os.WriteFile(
    64  		bin,
    65  		[]byte("#!/bin/sh\nenv | grep -qw FOO=bar"),
    66  		0o755,
    67  	))
    68  	ctx := testctx.NewWithCfg(config.Project{
    69  		GoMod: config.GoMod{
    70  			GoBinary: bin,
    71  			Env:      []string{"FOO=bar"},
    72  		},
    73  	})
    74  	require.NoError(t, Pipe{}.Default(ctx))
    75  	require.NoError(t, Pipe{}.Run(ctx))
    76  }
    77  
    78  func TestRunCustomDir(t *testing.T) {
    79  	dir := testlib.Mktmp(t)
    80  	require.NoError(t, os.MkdirAll(filepath.Join(dir, "src"), 0o755))
    81  	require.NoError(t, os.WriteFile(
    82  		filepath.Join(dir, "src/main.go"),
    83  		[]byte("package main\nfunc main() {println(0)}"),
    84  		0o666,
    85  	))
    86  	require.NoError(t, os.WriteFile(
    87  		filepath.Join(dir, "src/go.mod"),
    88  		[]byte("module foo"),
    89  		0o666,
    90  	))
    91  	ctx := testctx.NewWithCfg(config.Project{
    92  		GoMod: config.GoMod{
    93  			Dir: filepath.Join(dir, "src"),
    94  		},
    95  	})
    96  	require.NoError(t, Pipe{}.Default(ctx))
    97  	require.NoError(t, Pipe{}.Run(ctx))
    98  	require.Equal(t, "foo", ctx.ModulePath)
    99  }
   100  
   101  func TestRunOutsideGoModule(t *testing.T) {
   102  	dir := testlib.Mktmp(t)
   103  	require.NoError(t, os.WriteFile(
   104  		filepath.Join(dir, "main.go"),
   105  		[]byte("package main\nfunc main() {println(0)}"),
   106  		0o666,
   107  	))
   108  	ctx := testctx.New()
   109  	require.NoError(t, Pipe{}.Default(ctx))
   110  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   111  	require.Empty(t, ctx.ModulePath)
   112  }
   113  
   114  func TestRunCommandError(t *testing.T) {
   115  	ctx := testctx.NewWithCfg(config.Project{
   116  		GoMod: config.GoMod{
   117  			GoBinary: "not-a-valid-binary",
   118  		},
   119  	})
   120  	require.EqualError(
   121  		t,
   122  		Pipe{}.Run(ctx),
   123  		`failed to get module path: exec: "not-a-valid-binary": executable file not found in $PATH: `,
   124  	)
   125  	require.Empty(t, ctx.ModulePath)
   126  }
   127  
   128  func TestRunOldGoVersion(t *testing.T) {
   129  	bin := filepath.Join(t.TempDir(), "go.bin")
   130  	require.NoError(t, os.WriteFile(
   131  		bin,
   132  		[]byte("#!/bin/sh\necho \"flag provided but not defined: -m\"\nexit 1"),
   133  		0o755,
   134  	))
   135  	ctx := testctx.NewWithCfg(config.Project{
   136  		GoMod: config.GoMod{
   137  			GoBinary: bin,
   138  		},
   139  	})
   140  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   141  	require.Empty(t, ctx.ModulePath)
   142  }
   143  
   144  func TestDescription(t *testing.T) {
   145  	require.NotEmpty(t, Pipe{}.String())
   146  }