You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

git_helper_for_declarative_test.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "context"
  7. "fmt"
  8. "io/ioutil"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "os"
  13. "os/exec"
  14. "path/filepath"
  15. "testing"
  16. "time"
  17. "code.gitea.io/git"
  18. "code.gitea.io/gitea/modules/setting"
  19. "github.com/Unknwon/com"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. func withKeyFile(t *testing.T, keyname string, callback func(string)) {
  23. keyFile := filepath.Join(setting.AppDataPath, keyname)
  24. err := exec.Command("ssh-keygen", "-f", keyFile, "-t", "rsa", "-N", "").Run()
  25. assert.NoError(t, err)
  26. //Setup ssh wrapper
  27. os.Setenv("GIT_SSH_COMMAND",
  28. "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "+
  29. filepath.Join(setting.AppWorkPath, keyFile))
  30. os.Setenv("GIT_SSH_VARIANT", "ssh")
  31. callback(keyFile)
  32. defer os.RemoveAll(keyFile)
  33. defer os.RemoveAll(keyFile + ".pub")
  34. }
  35. func createSSHUrl(gitPath string, u *url.URL) *url.URL {
  36. u2 := *u
  37. u2.Scheme = "ssh"
  38. u2.User = url.User("git")
  39. u2.Host = fmt.Sprintf("%s:%d", setting.SSH.ListenHost, setting.SSH.ListenPort)
  40. u2.Path = gitPath
  41. return &u2
  42. }
  43. func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL)) {
  44. prepareTestEnv(t)
  45. s := http.Server{
  46. Handler: mac,
  47. }
  48. u, err := url.Parse(setting.AppURL)
  49. assert.NoError(t, err)
  50. listener, err := net.Listen("tcp", u.Host)
  51. assert.NoError(t, err)
  52. defer func() {
  53. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
  54. s.Shutdown(ctx)
  55. cancel()
  56. }()
  57. go s.Serve(listener)
  58. //Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  59. callback(t, u)
  60. }
  61. func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
  62. return func(t *testing.T) {
  63. assert.NoError(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
  64. assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
  65. }
  66. }
  67. func doGitCloneFail(dstLocalPath string, u *url.URL) func(*testing.T) {
  68. return func(t *testing.T) {
  69. assert.Error(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
  70. assert.False(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
  71. }
  72. }
  73. func doGitInitTestRepository(dstPath string) func(*testing.T) {
  74. return func(t *testing.T) {
  75. // Init repository in dstPath
  76. assert.NoError(t, git.InitRepository(dstPath, false))
  77. assert.NoError(t, ioutil.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644))
  78. assert.NoError(t, git.AddChanges(dstPath, true))
  79. signature := git.Signature{
  80. Email: "test@example.com",
  81. Name: "test",
  82. When: time.Now(),
  83. }
  84. assert.NoError(t, git.CommitChanges(dstPath, git.CommitChangesOptions{
  85. Committer: &signature,
  86. Author: &signature,
  87. Message: "Initial Commit",
  88. }))
  89. }
  90. }
  91. func doGitAddRemote(dstPath, remoteName string, u *url.URL) func(*testing.T) {
  92. return func(t *testing.T) {
  93. _, err := git.NewCommand("remote", "add", remoteName, u.String()).RunInDir(dstPath)
  94. assert.NoError(t, err)
  95. }
  96. }
  97. func doGitPushTestRepository(dstPath, remoteName, branch string) func(*testing.T) {
  98. return func(t *testing.T) {
  99. _, err := git.NewCommand("push", "-u", remoteName, branch).RunInDir(dstPath)
  100. assert.NoError(t, err)
  101. }
  102. }
  103. func doGitPushTestRepositoryFail(dstPath, remoteName, branch string) func(*testing.T) {
  104. return func(t *testing.T) {
  105. _, err := git.NewCommand("push", "-u", remoteName, branch).RunInDir(dstPath)
  106. assert.Error(t, err)
  107. }
  108. }