github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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.StartServer() 57 if serverErr != nil { 58 return serverErr 59 } 60 61 api4.Init(a, 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.StartServer() 78 if serverErr != nil { 79 return serverErr 80 } 81 82 api4.Init(a, 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.EnableOnlyAdminIntegrations = false 97 *cfg.ServiceSettings.EnableCustomEmoji = true 98 cfg.ServiceSettings.EnableIncomingWebhooks = false 99 cfg.ServiceSettings.EnableOutgoingWebhooks = false 100 } 101 102 func executeTestCommand(command *exec.Cmd) { 103 cmdOutPipe, err := command.StdoutPipe() 104 if err != nil { 105 CommandPrintErrorln("Failed to run tests") 106 os.Exit(1) 107 return 108 } 109 110 cmdErrOutPipe, err := command.StderrPipe() 111 if err != nil { 112 CommandPrintErrorln("Failed to run tests") 113 os.Exit(1) 114 return 115 } 116 117 cmdOutReader := bufio.NewScanner(cmdOutPipe) 118 cmdErrOutReader := bufio.NewScanner(cmdErrOutPipe) 119 go func() { 120 for cmdOutReader.Scan() { 121 fmt.Println(cmdOutReader.Text()) 122 } 123 }() 124 125 go func() { 126 for cmdErrOutReader.Scan() { 127 fmt.Println(cmdErrOutReader.Text()) 128 } 129 }() 130 131 if err := command.Run(); err != nil { 132 CommandPrintErrorln("Client Tests failed") 133 os.Exit(1) 134 return 135 } 136 } 137 138 func runWebClientTests() { 139 if webappDir := os.Getenv("WEBAPP_DIR"); webappDir != "" { 140 os.Chdir(webappDir) 141 } else { 142 os.Chdir("../mattermost-webapp") 143 } 144 145 cmd := exec.Command("npm", "test") 146 executeTestCommand(cmd) 147 }