github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+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  	a.StartServer()
    57  	api4.Init(a, a.Srv.Router, false)
    58  	api.Init(a, a.Srv.Router)
    59  	wsapi.Init(a, a.Srv.WebSocketRouter)
    60  	a.UpdateConfig(setupClientTests)
    61  	runWebClientTests()
    62  
    63  	return nil
    64  }
    65  
    66  func serverForWebClientTestsCmdF(cmd *cobra.Command, args []string) error {
    67  	a, err := initDBCommandContextCobra(cmd)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	defer a.Shutdown()
    72  
    73  	utils.InitTranslations(a.Config().LocalizationSettings)
    74  	a.StartServer()
    75  	api4.Init(a, a.Srv.Router, false)
    76  	api.Init(a, a.Srv.Router)
    77  	wsapi.Init(a, a.Srv.WebSocketRouter)
    78  	a.UpdateConfig(setupClientTests)
    79  
    80  	c := make(chan os.Signal, 1)
    81  	signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
    82  	<-c
    83  
    84  	return nil
    85  }
    86  
    87  func setupClientTests(cfg *model.Config) {
    88  	*cfg.TeamSettings.EnableOpenServer = true
    89  	*cfg.ServiceSettings.EnableCommands = false
    90  	*cfg.ServiceSettings.EnableOnlyAdminIntegrations = false
    91  	*cfg.ServiceSettings.EnableCustomEmoji = true
    92  	cfg.ServiceSettings.EnableIncomingWebhooks = false
    93  	cfg.ServiceSettings.EnableOutgoingWebhooks = false
    94  }
    95  
    96  func executeTestCommand(cmd *exec.Cmd) {
    97  	cmdOutPipe, err := cmd.StdoutPipe()
    98  	if err != nil {
    99  		CommandPrintErrorln("Failed to run tests")
   100  		os.Exit(1)
   101  		return
   102  	}
   103  
   104  	cmdErrOutPipe, err := cmd.StderrPipe()
   105  	if err != nil {
   106  		CommandPrintErrorln("Failed to run tests")
   107  		os.Exit(1)
   108  		return
   109  	}
   110  
   111  	cmdOutReader := bufio.NewScanner(cmdOutPipe)
   112  	cmdErrOutReader := bufio.NewScanner(cmdErrOutPipe)
   113  	go func() {
   114  		for cmdOutReader.Scan() {
   115  			fmt.Println(cmdOutReader.Text())
   116  		}
   117  	}()
   118  
   119  	go func() {
   120  		for cmdErrOutReader.Scan() {
   121  			fmt.Println(cmdErrOutReader.Text())
   122  		}
   123  	}()
   124  
   125  	if err := cmd.Run(); err != nil {
   126  		CommandPrintErrorln("Client Tests failed")
   127  		os.Exit(1)
   128  		return
   129  	}
   130  }
   131  
   132  func runWebClientTests() {
   133  	if webappDir := os.Getenv("WEBAPP_DIR"); webappDir != "" {
   134  		os.Chdir(webappDir)
   135  	} else {
   136  		os.Chdir("../mattermost-webapp")
   137  	}
   138  
   139  	cmd := exec.Command("npm", "test")
   140  	executeTestCommand(cmd)
   141  }