github.com/triarius/goreleaser@v1.12.5/internal/testlib/git.go (about)

     1  package testlib
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/triarius/goreleaser/internal/git"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // GitInit inits a new git project.
    12  func GitInit(tb testing.TB) {
    13  	tb.Helper()
    14  	out, err := fakeGit("init")
    15  	require.NoError(tb, err)
    16  	require.Contains(tb, out, "Initialized empty Git repository")
    17  	require.NoError(tb, err)
    18  	GitCheckoutBranch(tb, "main")
    19  	_, _ = fakeGit("branch", "-D", "master")
    20  }
    21  
    22  // GitRemoteAdd adds the given url as remote.
    23  func GitRemoteAdd(tb testing.TB, url string) {
    24  	tb.Helper()
    25  	out, err := fakeGit("remote", "add", "origin", url)
    26  	require.NoError(tb, err)
    27  	require.Empty(tb, out)
    28  }
    29  
    30  // GitRemoteAddWithName adds the given url as remote with given name.
    31  func GitRemoteAddWithName(tb testing.TB, remote, url string) {
    32  	tb.Helper()
    33  	out, err := fakeGit("remote", "add", remote, url)
    34  	require.NoError(tb, err)
    35  	require.Empty(tb, out)
    36  }
    37  
    38  // GitCommit creates a git commits.
    39  func GitCommit(tb testing.TB, msg string) {
    40  	tb.Helper()
    41  	out, err := fakeGit("commit", "--allow-empty", "-m", msg)
    42  	require.NoError(tb, err)
    43  	require.Contains(tb, out, "main", msg)
    44  }
    45  
    46  // GitTag creates a git tag.
    47  func GitTag(tb testing.TB, tag string) {
    48  	tb.Helper()
    49  	out, err := fakeGit("tag", tag)
    50  	require.NoError(tb, err)
    51  	require.Empty(tb, out)
    52  }
    53  
    54  // GitAnnotatedTag creates an annotated tag.
    55  func GitAnnotatedTag(tb testing.TB, tag, message string) {
    56  	tb.Helper()
    57  	out, err := fakeGit("tag", "-a", tag, "-m", message)
    58  	require.NoError(tb, err)
    59  	require.Empty(tb, out)
    60  }
    61  
    62  // GitBranch creates a git branch.
    63  func GitBranch(tb testing.TB, branch string) {
    64  	tb.Helper()
    65  	out, err := fakeGit("branch", branch)
    66  	require.NoError(tb, err)
    67  	require.Empty(tb, out)
    68  }
    69  
    70  // GitAdd adds all files to stage.
    71  func GitAdd(tb testing.TB) {
    72  	tb.Helper()
    73  	out, err := fakeGit("add", "-A")
    74  	require.NoError(tb, err)
    75  	require.Empty(tb, out)
    76  }
    77  
    78  func fakeGit(args ...string) (string, error) {
    79  	allArgs := []string{
    80  		"-c", "user.name='GoReleaser'",
    81  		"-c", "user.email='test@goreleaser.github.com'",
    82  		"-c", "commit.gpgSign=false",
    83  		"-c", "tag.gpgSign=false",
    84  		"-c", "log.showSignature=false",
    85  	}
    86  	allArgs = append(allArgs, args...)
    87  	return git.Run(context.Background(), allArgs...)
    88  }
    89  
    90  // GitCheckoutBranch allows us to change the active branch that we're using.
    91  func GitCheckoutBranch(tb testing.TB, name string) {
    92  	tb.Helper()
    93  	out, err := fakeGit("checkout", "-b", name)
    94  	require.NoError(tb, err)
    95  	require.Empty(tb, out)
    96  }