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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. package e2e
  6. import (
  7. "bytes"
  8. "context"
  9. "fmt"
  10. "net/url"
  11. "os"
  12. "os/exec"
  13. "path/filepath"
  14. "testing"
  15. "code.gitea.io/gitea/models/unittest"
  16. "code.gitea.io/gitea/modules/graceful"
  17. "code.gitea.io/gitea/modules/log"
  18. "code.gitea.io/gitea/modules/setting"
  19. "code.gitea.io/gitea/modules/util"
  20. "code.gitea.io/gitea/modules/web"
  21. "code.gitea.io/gitea/routers"
  22. "code.gitea.io/gitea/tests"
  23. )
  24. var c *web.Route
  25. func TestMain(m *testing.M) {
  26. defer log.Close()
  27. managerCtx, cancel := context.WithCancel(context.Background())
  28. graceful.InitManager(managerCtx)
  29. defer cancel()
  30. tests.InitTest(false)
  31. c = routers.NormalRoutes(context.TODO())
  32. os.Unsetenv("GIT_AUTHOR_NAME")
  33. os.Unsetenv("GIT_AUTHOR_EMAIL")
  34. os.Unsetenv("GIT_AUTHOR_DATE")
  35. os.Unsetenv("GIT_COMMITTER_NAME")
  36. os.Unsetenv("GIT_COMMITTER_EMAIL")
  37. os.Unsetenv("GIT_COMMITTER_DATE")
  38. err := unittest.InitFixtures(
  39. unittest.FixturesOptions{
  40. Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
  41. },
  42. )
  43. if err != nil {
  44. fmt.Printf("Error initializing test database: %v\n", err)
  45. os.Exit(1)
  46. }
  47. exitVal := m.Run()
  48. tests.WriterCloser.Reset()
  49. if err = util.RemoveAll(setting.Indexer.IssuePath); err != nil {
  50. fmt.Printf("util.RemoveAll: %v\n", err)
  51. os.Exit(1)
  52. }
  53. if err = util.RemoveAll(setting.Indexer.RepoPath); err != nil {
  54. fmt.Printf("Unable to remove repo indexer: %v\n", err)
  55. os.Exit(1)
  56. }
  57. os.Exit(exitVal)
  58. }
  59. // 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.
  60. func TestE2e(t *testing.T) {
  61. // Find the paths of all e2e test files in test test directory.
  62. searchGlob := filepath.Join(filepath.Dir(setting.AppPath), "tests", "e2e", "*.test.e2e.js")
  63. paths, err := filepath.Glob(searchGlob)
  64. if err != nil {
  65. t.Fatal(err)
  66. } else if len(paths) == 0 {
  67. t.Fatal(fmt.Errorf("No e2e tests found in %s", searchGlob))
  68. }
  69. runArgs := []string{"npx", "playwright", "test"}
  70. // To update snapshot outputs
  71. if _, set := os.LookupEnv("ACCEPT_VISUAL"); set {
  72. runArgs = append(runArgs, "--update-snapshots")
  73. }
  74. // Create new test for each input file
  75. for _, path := range paths {
  76. _, filename := filepath.Split(path)
  77. testname := filename[:len(filename)-len(filepath.Ext(path))]
  78. t.Run(testname, func(t *testing.T) {
  79. // Default 2 minute timeout
  80. onGiteaRun(t, func(*testing.T, *url.URL) {
  81. cmd := exec.Command(runArgs[0], runArgs...)
  82. cmd.Env = os.Environ()
  83. cmd.Env = append(cmd.Env, fmt.Sprintf("GITEA_URL=%s", setting.AppURL))
  84. var stdout, stderr bytes.Buffer
  85. cmd.Stdout = &stdout
  86. cmd.Stderr = &stderr
  87. err := cmd.Run()
  88. if err != nil {
  89. // Currently colored output is conflicting. Using Printf until that is resolved.
  90. fmt.Printf("%v", stdout.String())
  91. fmt.Printf("%v", stderr.String())
  92. log.Fatal("Playwright Failed: %s", err)
  93. } else {
  94. fmt.Printf("%v", stdout.String())
  95. }
  96. })
  97. })
  98. }
  99. }