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

integration_test.go 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2017 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. "bytes"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "os"
  11. "testing"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/routers"
  15. "code.gitea.io/gitea/routers/routes"
  16. "github.com/Unknwon/com"
  17. "github.com/stretchr/testify/assert"
  18. "gopkg.in/macaron.v1"
  19. "gopkg.in/testfixtures.v2"
  20. )
  21. var mac *macaron.Macaron
  22. func TestMain(m *testing.M) {
  23. appIniPath := os.Getenv("GITEA_CONF")
  24. if appIniPath == "" {
  25. fmt.Println("Environment variable $GITEA_CONF not set")
  26. os.Exit(1)
  27. }
  28. setting.CustomConf = appIniPath
  29. routers.GlobalInit()
  30. mac = routes.NewMacaron()
  31. routes.RegisterRoutes(mac)
  32. var helper testfixtures.Helper
  33. if setting.UseMySQL {
  34. helper = &testfixtures.MySQL{}
  35. } else if setting.UsePostgreSQL {
  36. helper = &testfixtures.PostgreSQL{}
  37. } else if setting.UseSQLite3 {
  38. helper = &testfixtures.SQLite{}
  39. } else {
  40. fmt.Println("Unsupported RDBMS for integration tests")
  41. os.Exit(1)
  42. }
  43. err := models.InitFixtures(
  44. helper,
  45. "models/fixtures/",
  46. )
  47. if err != nil {
  48. fmt.Printf("Error initializing test database: %v\n", err)
  49. os.Exit(1)
  50. }
  51. os.Exit(m.Run())
  52. }
  53. func prepareTestEnv(t *testing.T) {
  54. assert.NoError(t, models.LoadFixtures())
  55. assert.NoError(t, os.RemoveAll("integrations/gitea-integration"))
  56. assert.NoError(t, com.CopyDir("integrations/gitea-integration-meta", "integrations/gitea-integration"))
  57. }
  58. type TestResponseWriter struct {
  59. HeaderCode int
  60. Writer io.Writer
  61. }
  62. func (w *TestResponseWriter) Header() http.Header {
  63. return make(map[string][]string)
  64. }
  65. func (w *TestResponseWriter) Write(b []byte) (int, error) {
  66. return w.Writer.Write(b)
  67. }
  68. func (w *TestResponseWriter) WriteHeader(n int) {
  69. w.HeaderCode = n
  70. }
  71. type TestResponse struct {
  72. HeaderCode int
  73. Body []byte
  74. }
  75. func MakeRequest(req *http.Request) *TestResponse {
  76. buffer := bytes.NewBuffer(nil)
  77. respWriter := &TestResponseWriter{
  78. Writer: buffer,
  79. }
  80. mac.ServeHTTP(respWriter, req)
  81. return &TestResponse{
  82. HeaderCode: respWriter.HeaderCode,
  83. Body: buffer.Bytes(),
  84. }
  85. }