github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/cmd/platform/test.go (about)

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