summaryrefslogtreecommitdiffstats
path: root/pkg/gui/test_mode.go
blob: c5014ad729e27fb8aa5b3f6cf1b4775aa374d8b5 (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
package gui

import (
	"log"
	"os"
	"time"

	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/gui/popup"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
	"github.com/jesseduffield/lazygit/pkg/integration/components"
	"github.com/jesseduffield/lazygit/pkg/utils"
)

type IntegrationTest interface {
	Run(*GuiDriver)
}

func (gui *Gui) handleTestMode() {
	test := gui.integrationTest
	if os.Getenv(components.SANDBOX_ENV_VAR) == "true" {
		return
	}

	if test != nil {
		isIdleChan := make(chan struct{})

		gui.c.GocuiGui().AddIdleListener(isIdleChan)

		waitUntilIdle := func() {
			<-isIdleChan
		}

		go func() {
			waitUntilIdle()

			toastChan := make(chan string, 100)
			gui.PopupHandler.(*popup.PopupHandler).SetToastFunc(
				func(message string, kind types.ToastKind) { toastChan <- message })

			test.Run(&GuiDriver{gui: gui, isIdleChan: isIdleChan, toastChan: toastChan, headless: Headless()})

			gui.g.Update(func(*gocui.Gui) error {
				return gocui.ErrQuit
			})

			waitUntilIdle()

			time.Sleep(time.Second * 1)

			log.Fatal("gocui should have already exited")
		}()

		if os.Getenv(components.WAIT_FOR_DEBUGGER_ENV_VAR) == "" {
			go utils.Safe(func() {
				time.Sleep(time.Second * 40)
				log.Fatal("40 seconds is up, lazygit recording took too long to complete")
			})
		}
	}
}

func Headless() bool {
	return os.Getenv("HEADLESS") != ""
}