summaryrefslogtreecommitdiffstats
path: root/cmd/commands/test.go
blob: 837b988cc42df7a9b26a59e760d8c53fdefd48ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package commands

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"

	"os/signal"
	"syscall"

	"github.com/mattermost/mattermost-server/api4"
	"github.com/mattermost/mattermost-server/cmd"
	"github.com/mattermost/mattermost-server/model"
	"github.com/mattermost/mattermost-server/utils"
	"github.com/mattermost/mattermost-server/wsapi"
	"github.com/spf13/cobra"
)

var TestCmd = &cobra.Command{
	Use:    "test",
	Short:  "Testing Commands",
	Hidden: true,
}

var RunWebClientTestsCmd = &cobra.Command{
	Use:   "web_client_tests",
	Short: "Run the web client tests",
	RunE:  webClientTestsCmdF,
}

var RunServerForWebClientTestsCmd = &cobra.Command{
	Use:   "web_client_tests_server",
	Short: "Run the server configured for running the web client tests against it",
	RunE:  serverForWebClientTestsCmdF,
}

func init() {
	TestCmd.AddCommand(
		RunWebClientTestsCmd,
		RunServerForWebClientTestsCmd,
	)
	cmd.RootCmd.AddCommand(TestCmd)
}

func webClientTestsCmdF(command *cobra.Command, args []string) error {
	a, err := cmd.InitDBCommandContextCobra(command)
	if err != nil {
		return err
	}
	defer a.Shutdown()

	utils.InitTranslations(a.Config().LocalizationSettings)
	serverErr := a.StartServer()
	if serverErr != nil {
		return serverErr
	}

	api4.Init(a, a.Srv.Router)
	wsapi.Init(a, a.Srv.WebSocketRouter)
	a.UpdateConfig(setupClientTests)
	runWebClientTests()

	return nil
}

func serverForWebClientTestsCmdF(command *cobra.Command, args []string) error {
	a, err := cmd.InitDBCommandContextCobra(command)
	if err != nil {
		return err
	}
	defer a.Shutdown()

	utils.InitTranslations(a.Config().LocalizationSettings)
	serverErr := a.StartServer()
	if serverErr != nil {
		return serverErr
	}

	api4.Init(a, a.Srv.Router)
	wsapi.Init(a, a.Srv.WebSocketRouter)
	a.UpdateConfig(setupClientTests)

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
	<-c

	return nil
}

func setupClientTests(cfg *model.Config) {
	*cfg.TeamSettings.EnableOpenServer = true
	*cfg.ServiceSettings.EnableCommands = false
	*cfg.ServiceSettings.EnableOnlyAdminIntegrations = false
	*cfg.ServiceSettings.EnableCustomEmoji = true
	cfg.ServiceSettings.EnableIncomingWebhooks = false
	cfg.ServiceSettings.EnableOutgoingWebhooks = false
}

func executeTestCommand(command *exec.Cmd) {
	cmdOutPipe, err := command.StdoutPipe()
	if err != nil {
		cmd.CommandPrintErrorln("Failed to run tests")
		os.Exit(1)
		return
	}

	cmdErrOutPipe, err := command.StderrPipe()
	if err != nil {
		cmd.CommandPrintErrorln("Failed to run tests")
		os.Exit(1)
		return
	}

	cmdOutReader := bufio.NewScanner(cmdOutPipe)
	cmdErrOutReader := bufio.NewScanner(cmdErrOutPipe)
	go func() {
		for cmdOutReader.Scan() {
			fmt.Println(cmdOutReader.Text())
		}
	}()

	go func() {
		for cmdErrOutReader.Scan() {
			fmt.Println(cmdErrOutReader.Text())
		}
	}()

	if err := command.Run(); err != nil {
		cmd.CommandPrintErrorln("Client Tests failed")
		os.Exit(1)
		return
	}
}

func runWebClientTests() {
	if webappDir := os.Getenv("WEBAPP_DIR"); webappDir != "" {
		os.Chdir(webappDir)
	} else {
		os.Chdir("../mattermost-webapp")
	}

	cmd := exec.Command("npm", "test")
	executeTestCommand(cmd)
}