github.com/goreleaser/goreleaser@v1.25.1/internal/testlib/git.go (about) 1 package testlib 2 3 import ( 4 "context" 5 "os/exec" 6 "path/filepath" 7 "testing" 8 9 "github.com/charmbracelet/keygen" 10 "github.com/goreleaser/goreleaser/internal/git" 11 "github.com/stretchr/testify/require" 12 ) 13 14 // GitInit inits a new git project. 15 func GitInit(tb testing.TB) { 16 tb.Helper() 17 out, err := fakeGit("init") 18 require.NoError(tb, err) 19 require.Contains(tb, out, "Initialized empty Git repository") 20 require.NoError(tb, err) 21 GitCheckoutBranch(tb, "main") 22 _, _ = fakeGit("branch", "-D", "master") 23 } 24 25 // GitRemoteAdd adds the given url as remote. 26 func GitRemoteAdd(tb testing.TB, url string) { 27 tb.Helper() 28 out, err := fakeGit("remote", "add", "origin", url) 29 require.NoError(tb, err) 30 require.Empty(tb, out) 31 } 32 33 // GitRemoteAddWithName adds the given url as remote with given name. 34 func GitRemoteAddWithName(tb testing.TB, remote, url string) { 35 tb.Helper() 36 out, err := fakeGit("remote", "add", remote, url) 37 require.NoError(tb, err) 38 require.Empty(tb, out) 39 } 40 41 // GitCommit creates a git commits. 42 func GitCommit(tb testing.TB, msg string) { 43 tb.Helper() 44 out, err := fakeGit("commit", "--allow-empty", "-m", msg) 45 require.NoError(tb, err) 46 require.Contains(tb, out, "main", msg) 47 } 48 49 // GitTag creates a git tag. 50 func GitTag(tb testing.TB, tag string) { 51 tb.Helper() 52 out, err := fakeGit("tag", tag) 53 require.NoError(tb, err) 54 require.Empty(tb, out) 55 } 56 57 // GitAnnotatedTag creates an annotated tag. 58 func GitAnnotatedTag(tb testing.TB, tag, message string) { 59 tb.Helper() 60 out, err := fakeGit("tag", "-a", tag, "-m", message) 61 require.NoError(tb, err) 62 require.Empty(tb, out) 63 } 64 65 // GitBranch creates a git branch. 66 func GitBranch(tb testing.TB, branch string) { 67 tb.Helper() 68 out, err := fakeGit("branch", branch) 69 require.NoError(tb, err) 70 require.Empty(tb, out) 71 } 72 73 // GitAdd adds all files to stage. 74 func GitAdd(tb testing.TB) { 75 tb.Helper() 76 out, err := fakeGit("add", "-A") 77 require.NoError(tb, err) 78 require.Empty(tb, out) 79 } 80 81 func fakeGit(args ...string) (string, error) { 82 allArgs := []string{ 83 "-c", "user.name='GoReleaser'", 84 "-c", "user.email='test@goreleaser.github.com'", 85 "-c", "commit.gpgSign=false", 86 "-c", "tag.gpgSign=false", 87 "-c", "log.showSignature=false", 88 } 89 allArgs = append(allArgs, args...) 90 return git.Run(context.Background(), allArgs...) 91 } 92 93 // GitCheckoutBranch allows us to change the active branch that we're using. 94 func GitCheckoutBranch(tb testing.TB, name string) { 95 tb.Helper() 96 out, err := fakeGit("checkout", "-b", name) 97 require.NoError(tb, err) 98 require.Empty(tb, out) 99 } 100 101 func GitMakeBareRepository(tb testing.TB) string { 102 tb.Helper() 103 dir := tb.TempDir() 104 _, err := git.Run( 105 context.Background(), 106 "-C", dir, 107 "-c", "init.defaultBranch=master", 108 "init", 109 "--bare", 110 ".", 111 ) 112 require.NoError(tb, err) 113 return dir 114 } 115 116 func MakeNewSSHKey(tb testing.TB, pass string) string { 117 tb.Helper() 118 return MakeNewSSHKeyType(tb, pass, keygen.Ed25519) 119 } 120 121 func MakeNewSSHKeyType(tb testing.TB, pass string, algo keygen.KeyType) string { 122 tb.Helper() 123 dir := tb.TempDir() 124 filepath := filepath.Join(dir, "id_"+algo.String()) 125 _, err := keygen.New( 126 filepath, 127 keygen.WithKeyType(algo), 128 keygen.WithWrite(), 129 keygen.WithPassphrase(pass), 130 ) 131 require.NoError(tb, err) 132 return filepath 133 } 134 135 func CatFileFromBareRepository(tb testing.TB, url, name string) []byte { 136 tb.Helper() 137 return CatFileFromBareRepositoryOnBranch(tb, url, "master", name) 138 } 139 140 func CatFileFromBareRepositoryOnBranch(tb testing.TB, url, branch, name string) []byte { 141 tb.Helper() 142 143 out, err := exec.Command( 144 "git", 145 "-C", url, 146 "show", 147 branch+":"+name, 148 ).CombinedOutput() 149 require.NoError(tb, err, "could not cat file "+name+" in repository on branch "+branch) 150 return out 151 }