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.

e2e_test.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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
  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.Route
  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. os.Unsetenv("GIT_AUTHOR_NAME")
  35. os.Unsetenv("GIT_AUTHOR_EMAIL")
  36. os.Unsetenv("GIT_AUTHOR_DATE")
  37. os.Unsetenv("GIT_COMMITTER_NAME")
  38. os.Unsetenv("GIT_COMMITTER_EMAIL")
  39. os.Unsetenv("GIT_COMMITTER_DATE")
  40. err := unittest.InitFixtures(
  41. unittest.FixturesOptions{
  42. Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
  43. },
  44. )
  45. if err != nil {
  46. fmt.Printf("Error initializing test database: %v\n", err)
  47. os.Exit(1)
  48. }
  49. exitVal := m.Run()
  50. testlogger.WriterCloser.Reset()
  51. if err = util.RemoveAll(setting.Indexer.IssuePath); err != nil {
  52. fmt.Printf("util.RemoveAll: %v\n", err)
  53. os.Exit(1)
  54. }
  55. if err = util.RemoveAll(setting.Indexer.RepoPath); err != nil {
  56. fmt.Printf("Unable to remove repo indexer: %v\n", err)
  57. os.Exit(1)
  58. }
  59. os.Exit(exitVal)
  60. }
  61. // TestE2e should be the only test e2e necessary. It will collect all "*.test.e2e.js" files in this directory and build a test for each.
  62. func TestE2e(t *testing.T) {
  63. // Find the paths of all e2e test files in test test directory.
  64. searchGlob := filepath.Join(filepath.Dir(setting.AppPath), "tests", "e2e", "*.test.e2e.js")
  65. paths, err := filepath.Glob(searchGlob)
  66. if err != nil {
  67. t.Fatal(err)
  68. } else if len(paths) == 0 {
  69. t.Fatal(fmt.Errorf("No e2e tests found in %s", searchGlob))
  70. }
  71. runArgs := []string{"npx", "playwright", "test"}
  72. // To update snapshot outputs
  73. if _, set := os.LookupEnv("ACCEPT_VISUAL"); set {
  74. runArgs = append(runArgs, "--update-snapshots")
  75. }
  76. // Create new test for each input file
  77. for _, path := range paths {
  78. _, filename := filepath.Split(path)
  79. testname := filename[:len(filename)-len(filepath.Ext(path))]
  80. t.Run(testname, func(t *testing.T) {
  81. // Default 2 minute timeout
  82. onGiteaRun(t, func(*testing.T, *url.URL) {
  83. cmd := exec.Command(runArgs[0], runArgs...)
  84. cmd.Env = os.Environ()
  85. cmd.Env = append(cmd.Env, fmt.Sprintf("GITEA_URL=%s", setting.AppURL))
  86. var stdout, stderr bytes.Buffer
  87. cmd.Stdout = &stdout
  88. cmd.Stderr = &stderr
  89. err := cmd.Run()
  90. if err != nil {
  91. // Currently colored output is conflicting. Using Printf until that is resolved.
  92. fmt.Printf("%v", stdout.String())
  93. fmt.Printf("%v", stderr.String())
  94. log.Fatal("Playwright Failed: %s", err)
  95. } else {
  96. fmt.Printf("%v", stdout.String())
  97. }
  98. })
  99. })
  100. }
  101. }