gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. // This is primarily coped from /tests/integration/integration_test.go
  4. // TODO: Move common functions to shared file
  5. //nolint:forbidigo // use of print functions is allowed in tests
  6. package e2e
  7. import (
  8. "bytes"
  9. "context"
  10. "fmt"
  11. "net/url"
  12. "os"
  13. "os/exec"
  14. "path/filepath"
  15. "testing"
  16. "code.gitea.io/gitea/models/unittest"
  17. "code.gitea.io/gitea/modules/graceful"
  18. "code.gitea.io/gitea/modules/log"
  19. "code.gitea.io/gitea/modules/setting"
  20. "code.gitea.io/gitea/modules/testlogger"
  21. "code.gitea.io/gitea/modules/util"
  22. "code.gitea.io/gitea/modules/web"
  23. "code.gitea.io/gitea/routers"
  24. "code.gitea.io/gitea/tests"
  25. )
  26. var testE2eWebRoutes *web.Router
  27. func TestMain(m *testing.M) {
  28. defer log.GetManager().Close()
  29. managerCtx, cancel := context.WithCancel(context.Background())
  30. graceful.InitManager(managerCtx)
  31. defer cancel()
  32. tests.InitTest(false)
  33. testE2eWebRoutes = routers.NormalRoutes()
  34. err := unittest.InitFixtures(
  35. unittest.FixturesOptions{
  36. Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
  37. },
  38. )
  39. if err != nil {
  40. fmt.Printf("Error initializing test database: %v\n", err)
  41. os.Exit(1)
  42. }
  43. exitVal := m.Run()
  44. testlogger.WriterCloser.Reset()
  45. if err = util.RemoveAll(setting.Indexer.IssuePath); err != nil {
  46. fmt.Printf("util.RemoveAll: %v\n", err)
  47. os.Exit(1)
  48. }
  49. if err = util.RemoveAll(setting.Indexer.RepoPath); err != nil {
  50. fmt.Printf("Unable to remove repo indexer: %v\n", err)
  51. os.Exit(1)
  52. }
  53. os.Exit(exitVal)
  54. }
  55. // TestE2e should be the only test e2e necessary. It will collect all "*.test.e2e.ts" files in this directory and build a test for each.
  56. func TestE2e(t *testing.T) {
  57. // Find the paths of all e2e test files in test directory.
  58. searchGlob := filepath.Join(filepath.Dir(setting.AppPath), "tests", "e2e", "*.test.e2e.ts")
  59. paths, err := filepath.Glob(searchGlob)
  60. if err != nil {
  61. t.Fatal(err)
  62. } else if len(paths) == 0 {
  63. t.Fatal(fmt.Errorf("No e2e tests found in %s", searchGlob))
  64. }
  65. runArgs := []string{"npx", "playwright", "test"}
  66. // To update snapshot outputs
  67. if _, set := os.LookupEnv("ACCEPT_VISUAL"); set {
  68. runArgs = append(runArgs, "--update-snapshots")
  69. }
  70. // Create new test for each input file
  71. for _, path := range paths {
  72. _, filename := filepath.Split(path)
  73. testname := filename[:len(filename)-len(filepath.Ext(path))]
  74. t.Run(testname, func(t *testing.T) {
  75. // Default 2 minute timeout
  76. onGiteaRun(t, func(*testing.T, *url.URL) {
  77. cmd := exec.Command(runArgs[0], runArgs...)
  78. cmd.Env = os.Environ()
  79. cmd.Env = append(cmd.Env, "GITEA_URL="+setting.AppURL)
  80. var stdout, stderr bytes.Buffer
  81. cmd.Stdout = &stdout
  82. cmd.Stderr = &stderr
  83. err = cmd.Run()
  84. if err != nil {
  85. // Currently colored output is conflicting. Using Printf until that is resolved.
  86. fmt.Printf("%v", stdout.String())
  87. fmt.Printf("%v", stderr.String())
  88. log.Fatal("Playwright Failed: %s", err)
  89. }
  90. fmt.Printf("%v", stdout.String())
  91. })
  92. })
  93. }
  94. }