Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

unit_tests.go 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2016 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 models
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "math"
  9. "net/url"
  10. "os"
  11. "path/filepath"
  12. "testing"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/storage"
  16. "code.gitea.io/gitea/modules/util"
  17. "github.com/stretchr/testify/assert"
  18. "xorm.io/xorm"
  19. "xorm.io/xorm/names"
  20. )
  21. // NonexistentID an ID that will never exist
  22. const NonexistentID = int64(math.MaxInt64)
  23. // giteaRoot a path to the gitea root
  24. var (
  25. giteaRoot string
  26. fixturesDir string
  27. )
  28. func fatalTestError(fmtStr string, args ...interface{}) {
  29. fmt.Fprintf(os.Stderr, fmtStr, args...)
  30. os.Exit(1)
  31. }
  32. // MainTest a reusable TestMain(..) function for unit tests that need to use a
  33. // test database. Creates the test database, and sets necessary settings.
  34. func MainTest(m *testing.M, pathToGiteaRoot string) {
  35. var err error
  36. giteaRoot = pathToGiteaRoot
  37. fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures")
  38. if err = CreateTestEngine(fixturesDir); err != nil {
  39. fatalTestError("Error creating test engine: %v\n", err)
  40. }
  41. setting.AppURL = "https://try.gitea.io/"
  42. setting.RunUser = "runuser"
  43. setting.SSH.Port = 3000
  44. setting.SSH.Domain = "try.gitea.io"
  45. setting.Database.UseSQLite3 = true
  46. setting.RepoRootPath, err = ioutil.TempDir(os.TempDir(), "repos")
  47. if err != nil {
  48. fatalTestError("TempDir: %v\n", err)
  49. }
  50. setting.AppDataPath, err = ioutil.TempDir(os.TempDir(), "appdata")
  51. if err != nil {
  52. fatalTestError("TempDir: %v\n", err)
  53. }
  54. setting.AppWorkPath = pathToGiteaRoot
  55. setting.StaticRootPath = pathToGiteaRoot
  56. setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/")
  57. if err != nil {
  58. fatalTestError("url.Parse: %v\n", err)
  59. }
  60. setting.Attachment.Storage.Path = filepath.Join(setting.AppDataPath, "attachments")
  61. setting.LFS.Storage.Path = filepath.Join(setting.AppDataPath, "lfs")
  62. setting.Avatar.Storage.Path = filepath.Join(setting.AppDataPath, "avatars")
  63. setting.RepoAvatar.Storage.Path = filepath.Join(setting.AppDataPath, "repo-avatars")
  64. setting.RepoArchive.Storage.Path = filepath.Join(setting.AppDataPath, "repo-archive")
  65. if err = storage.Init(); err != nil {
  66. fatalTestError("storage.Init: %v\n", err)
  67. }
  68. if err = util.RemoveAll(setting.RepoRootPath); err != nil {
  69. fatalTestError("util.RemoveAll: %v\n", err)
  70. }
  71. if err = util.CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
  72. fatalTestError("util.CopyDir: %v\n", err)
  73. }
  74. exitStatus := m.Run()
  75. if err = util.RemoveAll(setting.RepoRootPath); err != nil {
  76. fatalTestError("util.RemoveAll: %v\n", err)
  77. }
  78. if err = util.RemoveAll(setting.AppDataPath); err != nil {
  79. fatalTestError("util.RemoveAll: %v\n", err)
  80. }
  81. os.Exit(exitStatus)
  82. }
  83. // CreateTestEngine creates a memory database and loads the fixture data from fixturesDir
  84. func CreateTestEngine(fixturesDir string) error {
  85. var err error
  86. x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared&_txlock=immediate")
  87. if err != nil {
  88. return err
  89. }
  90. x.SetMapper(names.GonicMapper{})
  91. if err = syncTables(); err != nil {
  92. return err
  93. }
  94. switch os.Getenv("GITEA_UNIT_TESTS_VERBOSE") {
  95. case "true", "1":
  96. x.ShowSQL(true)
  97. }
  98. return InitFixtures(fixturesDir)
  99. }
  100. // PrepareTestDatabase load test fixtures into test database
  101. func PrepareTestDatabase() error {
  102. return LoadFixtures()
  103. }
  104. // PrepareTestEnv prepares the environment for unit tests. Can only be called
  105. // by tests that use the above MainTest(..) function.
  106. func PrepareTestEnv(t testing.TB) {
  107. assert.NoError(t, PrepareTestDatabase())
  108. assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
  109. metaPath := filepath.Join(giteaRoot, "integrations", "gitea-repositories-meta")
  110. assert.NoError(t, util.CopyDir(metaPath, setting.RepoRootPath))
  111. base.SetupGiteaRoot() // Makes sure GITEA_ROOT is set
  112. }
  113. type testCond struct {
  114. query interface{}
  115. args []interface{}
  116. }
  117. // Cond create a condition with arguments for a test
  118. func Cond(query interface{}, args ...interface{}) interface{} {
  119. return &testCond{query: query, args: args}
  120. }
  121. func whereConditions(sess *xorm.Session, conditions []interface{}) {
  122. for _, condition := range conditions {
  123. switch cond := condition.(type) {
  124. case *testCond:
  125. sess.Where(cond.query, cond.args...)
  126. default:
  127. sess.Where(cond)
  128. }
  129. }
  130. }
  131. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  132. sess := x.NewSession()
  133. defer sess.Close()
  134. whereConditions(sess, conditions)
  135. return sess.Get(bean)
  136. }
  137. // BeanExists for testing, check if a bean exists
  138. func BeanExists(t testing.TB, bean interface{}, conditions ...interface{}) bool {
  139. exists, err := loadBeanIfExists(bean, conditions...)
  140. assert.NoError(t, err)
  141. return exists
  142. }
  143. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  144. // database
  145. func AssertExistsAndLoadBean(t testing.TB, bean interface{}, conditions ...interface{}) interface{} {
  146. exists, err := loadBeanIfExists(bean, conditions...)
  147. assert.NoError(t, err)
  148. assert.True(t, exists,
  149. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  150. bean, bean, conditions)
  151. return bean
  152. }
  153. // GetCount get the count of a bean
  154. func GetCount(t testing.TB, bean interface{}, conditions ...interface{}) int {
  155. sess := x.NewSession()
  156. defer sess.Close()
  157. whereConditions(sess, conditions)
  158. count, err := sess.Count(bean)
  159. assert.NoError(t, err)
  160. return int(count)
  161. }
  162. // AssertNotExistsBean assert that a bean does not exist in the test database
  163. func AssertNotExistsBean(t testing.TB, bean interface{}, conditions ...interface{}) {
  164. exists, err := loadBeanIfExists(bean, conditions...)
  165. assert.NoError(t, err)
  166. assert.False(t, exists)
  167. }
  168. // AssertExistsIf asserts that a bean exists or does not exist, depending on
  169. // what is expected.
  170. func AssertExistsIf(t *testing.T, expected bool, bean interface{}, conditions ...interface{}) {
  171. exists, err := loadBeanIfExists(bean, conditions...)
  172. assert.NoError(t, err)
  173. assert.Equal(t, expected, exists)
  174. }
  175. // AssertSuccessfulInsert assert that beans is successfully inserted
  176. func AssertSuccessfulInsert(t testing.TB, beans ...interface{}) {
  177. _, err := x.Insert(beans...)
  178. assert.NoError(t, err)
  179. }
  180. // AssertCount assert the count of a bean
  181. func AssertCount(t testing.TB, bean, expected interface{}) {
  182. assert.EqualValues(t, expected, GetCount(t, bean))
  183. }
  184. // AssertInt64InRange assert value is in range [low, high]
  185. func AssertInt64InRange(t testing.TB, low, high, value int64) {
  186. assert.True(t, value >= low && value <= high,
  187. "Expected value in range [%d, %d], found %d", low, high, value)
  188. }