github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/cmd/mattermost/commands/test.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package commands 5 6 import ( 7 "bufio" 8 "fmt" 9 "os" 10 "os/exec" 11 12 "os/signal" 13 "syscall" 14 15 "github.com/mattermost/mattermost-server/api4" 16 "github.com/mattermost/mattermost-server/model" 17 "github.com/mattermost/mattermost-server/utils" 18 "github.com/mattermost/mattermost-server/wsapi" 19 "github.com/spf13/cobra" 20 ) 21 22 var TestCmd = &cobra.Command{ 23 Use: "test", 24 Short: "Testing Commands", 25 Hidden: true, 26 } 27 28 var RunWebClientTestsCmd = &cobra.Command{ 29 Use: "web_client_tests", 30 Short: "Run the web client tests", 31 RunE: webClientTestsCmdF, 32 } 33 34 var RunServerForWebClientTestsCmd = &cobra.Command{ 35 Use: "web_client_tests_server", 36 Short: "Run the server configured for running the web client tests against it", 37 RunE: serverForWebClientTestsCmdF, 38 } 39 40 func init() { 41 TestCmd.AddCommand( 42 RunWebClientTestsCmd, 43 RunServerForWebClientTestsCmd, 44 ) 45 RootCmd.AddCommand(TestCmd) 46 } 47 48 func webClientTestsCmdF(command *cobra.Command, args []string) error { 49 a, err := InitDBCommandContextCobra(command) 50 if err != nil { 51 return err 52 } 53 defer a.Shutdown() 54 55 utils.InitTranslations(a.Config().LocalizationSettings) 56 serverErr := a.Srv.Start() 57 if serverErr != nil { 58 return serverErr 59 } 60 61 api4.Init(a, a.Srv.AppOptions, a.Srv.Router) 62 wsapi.Init(a, a.Srv.WebSocketRouter) 63 a.UpdateConfig(setupClientTests) 64 runWebClientTests() 65 66 return nil 67 } 68 69 func serverForWebClientTestsCmdF(command *cobra.Command, args []string) error { 70 a, err := InitDBCommandContextCobra(command) 71 if err != nil { 72 return err 73 } 74 defer a.Shutdown() 75 76 utils.InitTranslations(a.Config().LocalizationSettings) 77 serverErr := a.Srv.Start() 78 if serverErr != nil { 79 return serverErr 80 } 81 82 api4.Init(a, a.Srv.AppOptions, a.Srv.Router) 83 wsapi.Init(a, a.Srv.WebSocketRouter) 84 a.UpdateConfig(setupClientTests) 85 86 c := make(chan os.Signal, 1) 87 signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) 88 <-c 89 90 return nil 91 } 92 93 func setupClientTests(cfg *model.Config) { 94 *cfg.TeamSettings.EnableOpenServer = true 95 *cfg.ServiceSettings.EnableCommands = false 96 *cfg.ServiceSettings.EnableCustomEmoji = true 97 *cfg.ServiceSettings.EnableIncomingWebhooks = false 98 *cfg.ServiceSettings.EnableOutgoingWebhooks = false 99 } 100 101 func executeTestCommand(command *exec.Cmd) { 102 cmdOutPipe, err := command.StdoutPipe() 103 if err != nil { 104 CommandPrintErrorln("Failed to run tests") 105 os.Exit(1) 106 return 107 } 108 109 cmdErrOutPipe, err := command.StderrPipe() 110 if err != nil { 111 CommandPrintErrorln("Failed to run tests") 112 os.Exit(1) 113 return 114 } 115 116 cmdOutReader := bufio.NewScanner(cmdOutPipe) 117 cmdErrOutReader := bufio.NewScanner(cmdErrOutPipe) 118 go func() { 119 for cmdOutReader.Scan() { 120 fmt.Println(cmdOutReader.Text()) 121 } 122 }() 123 124 go func() { 125 for cmdErrOutReader.Scan() { 126 fmt.Println(cmdErrOutReader.Text()) 127 } 128 }() 129 130 if err := command.Run(); err != nil { 131 CommandPrintErrorln("Client Tests failed") 132 os.Exit(1) 133 return 134 } 135 } 136 137 func runWebClientTests() { 138 if webappDir := os.Getenv("WEBAPP_DIR"); webappDir != "" { 139 os.Chdir(webappDir) 140 } else { 141 os.Chdir("../mattermost-webapp") 142 } 143 144 cmd := exec.Command("npm", "test") 145 executeTestCommand(cmd) 146 }