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