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 1.9 kB

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