github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/internal/testlib/git.go (about)

     1  package testlib
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/goreleaser/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  	GitCommitWithDate(tb, msg, time.Time{})
    42  }
    43  
    44  // GitCommitWithDate creates a git commit with a commit date.
    45  func GitCommitWithDate(tb testing.TB, msg string, commitDate time.Time) {
    46  	tb.Helper()
    47  	env := (map[string]string)(nil)
    48  	if !commitDate.IsZero() {
    49  		env = map[string]string{
    50  			"GIT_COMMITTER_DATE": commitDate.Format(time.RFC1123Z),
    51  		}
    52  	}
    53  	out, err := fakeGitEnv(env, "commit", "--allow-empty", "-m", msg)
    54  	require.NoError(tb, err)
    55  	require.Contains(tb, out, "main", msg)
    56  }
    57  
    58  // GitTag creates a git tag.
    59  func GitTag(tb testing.TB, tag string) {
    60  	tb.Helper()
    61  	out, err := fakeGit("tag", tag)
    62  	require.NoError(tb, err)
    63  	require.Empty(tb, out)
    64  }
    65  
    66  // GitBranch creates a git branch.
    67  func GitBranch(tb testing.TB, branch string) {
    68  	tb.Helper()
    69  	out, err := fakeGit("branch", branch)
    70  	require.NoError(tb, err)
    71  	require.Empty(tb, out)
    72  }
    73  
    74  // GitAdd adds all files to stage.
    75  func GitAdd(tb testing.TB) {
    76  	tb.Helper()
    77  	out, err := fakeGit("add", "-A")
    78  	require.NoError(tb, err)
    79  	require.Empty(tb, out)
    80  }
    81  
    82  func fakeGitEnv(env map[string]string, args ...string) (string, error) {
    83  	allArgs := []string{
    84  		"-c", "user.name='GoReleaser'",
    85  		"-c", "user.email='test@goreleaser.github.com'",
    86  		"-c", "commit.gpgSign=false",
    87  		"-c", "log.showSignature=false",
    88  	}
    89  	allArgs = append(allArgs, args...)
    90  	return git.RunEnv(env, allArgs...)
    91  }
    92  
    93  func fakeGit(args ...string) (string, error) {
    94  	return fakeGitEnv(nil, args...)
    95  }
    96  
    97  // GitCheckoutBranch allows us to change the active branch that we're using.
    98  func GitCheckoutBranch(tb testing.TB, name string) {
    99  	tb.Helper()
   100  	out, err := fakeGit("checkout", "-b", name)
   101  	require.NoError(tb, err)
   102  	require.Empty(tb, out)
   103  }