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