1// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2// See LICENSE.txt for license information. 3 4package commands 5 6import ( 7 "bufio" 8 "fmt" 9 "os" 10 "os/exec" 11 "os/signal" 12 "syscall" 13 14 "github.com/spf13/cobra" 15 16 "github.com/mattermost/mattermost-server/v6/api4" 17 "github.com/mattermost/mattermost-server/v6/model" 18 "github.com/mattermost/mattermost-server/v6/shared/i18n" 19 "github.com/mattermost/mattermost-server/v6/wsapi" 20) 21 22var TestCmd = &cobra.Command{ 23 Use: "test", 24 Short: "Testing Commands", 25 Hidden: true, 26} 27 28var RunWebClientTestsCmd = &cobra.Command{ 29 Use: "web_client_tests", 30 Short: "Run the web client tests", 31 RunE: webClientTestsCmdF, 32} 33 34var 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 40func init() { 41 TestCmd.AddCommand( 42 RunWebClientTestsCmd, 43 RunServerForWebClientTestsCmd, 44 ) 45 RootCmd.AddCommand(TestCmd) 46} 47 48func webClientTestsCmdF(command *cobra.Command, args []string) error { 49 a, err := InitDBCommandContextCobra(command) 50 if err != nil { 51 return err 52 } 53 defer a.Srv().Shutdown() 54 55 i18n.InitTranslations(*a.Config().LocalizationSettings.DefaultServerLocale, *a.Config().LocalizationSettings.DefaultClientLocale) 56 serverErr := a.Srv().Start() 57 if serverErr != nil { 58 return serverErr 59 } 60 61 api4.Init(a, a.Srv().Router) 62 wsapi.Init(a.Srv()) 63 a.UpdateConfig(setupClientTests) 64 runWebClientTests() 65 66 return nil 67} 68 69func serverForWebClientTestsCmdF(command *cobra.Command, args []string) error { 70 a, err := InitDBCommandContextCobra(command) 71 if err != nil { 72 return err 73 } 74 defer a.Srv().Shutdown() 75 76 i18n.InitTranslations(*a.Config().LocalizationSettings.DefaultServerLocale, *a.Config().LocalizationSettings.DefaultClientLocale) 77 serverErr := a.Srv().Start() 78 if serverErr != nil { 79 return serverErr 80 } 81 82 api4.Init(a, a.Srv().Router) 83 wsapi.Init(a.Srv()) 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 93func 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 101func 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 137func 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} 147