File: fake_cmd_obj_runner.go

package info (click to toggle)
lazygit 0.50.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,808 kB
  • sloc: sh: 128; makefile: 76
file content (157 lines) | stat: -rw-r--r-- 3,831 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
package oscommands

import (
	"bufio"
	"fmt"
	"strings"
	"sync"
	"testing"

	"github.com/go-errors/errors"
	"github.com/samber/lo"
	"golang.org/x/exp/slices"
)

// for use in testing

type FakeCmdObjRunner struct {
	t *testing.T
	// commands can be run in any order; mimicking the concurrent behaviour of
	// production code.
	expectedCmds []CmdObjMatcher

	invokedCmdIndexes []int

	mutex sync.Mutex
}

type CmdObjMatcher struct {
	description string
	// returns true if the matcher matches the command object
	test func(ICmdObj) bool

	// output of the command
	output string
	// error of the command
	err error
}

var _ ICmdObjRunner = &FakeCmdObjRunner{}

func NewFakeRunner(t *testing.T) *FakeCmdObjRunner { //nolint:thelper
	return &FakeCmdObjRunner{t: t}
}

func (self *FakeCmdObjRunner) remainingExpectedCmds() []CmdObjMatcher {
	return lo.Filter(self.expectedCmds, func(_ CmdObjMatcher, i int) bool {
		return !lo.Contains(self.invokedCmdIndexes, i)
	})
}

func (self *FakeCmdObjRunner) Run(cmdObj ICmdObj) error {
	_, err := self.RunWithOutput(cmdObj)
	return err
}

func (self *FakeCmdObjRunner) RunWithOutput(cmdObj ICmdObj) (string, error) {
	self.mutex.Lock()
	defer self.mutex.Unlock()

	if len(self.remainingExpectedCmds()) == 0 {
		self.t.Errorf("ran too many commands. Unexpected command: `%s`", cmdObj.ToString())
		return "", errors.New("ran too many commands")
	}

	for i := range self.expectedCmds {
		if lo.Contains(self.invokedCmdIndexes, i) {
			continue
		}
		expectedCmd := self.expectedCmds[i]
		matched := expectedCmd.test(cmdObj)
		if matched {
			self.invokedCmdIndexes = append(self.invokedCmdIndexes, i)
			return expectedCmd.output, expectedCmd.err
		}
	}

	self.t.Errorf("Unexpected command: `%s`", cmdObj.ToString())
	return "", nil
}

func (self *FakeCmdObjRunner) RunWithOutputs(cmdObj ICmdObj) (string, string, error) {
	output, err := self.RunWithOutput(cmdObj)
	return output, "", err
}

func (self *FakeCmdObjRunner) RunAndProcessLines(cmdObj ICmdObj, onLine func(line string) (bool, error)) error {
	output, err := self.RunWithOutput(cmdObj)
	if err != nil {
		return err
	}

	scanner := bufio.NewScanner(strings.NewReader(output))
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		line := scanner.Text()
		stop, err := onLine(line)
		if err != nil {
			return err
		}
		if stop {
			break
		}
	}

	return nil
}

func (self *FakeCmdObjRunner) ExpectFunc(description string, fn func(cmdObj ICmdObj) bool, output string, err error) *FakeCmdObjRunner {
	self.mutex.Lock()
	defer self.mutex.Unlock()

	self.expectedCmds = append(self.expectedCmds, CmdObjMatcher{
		test:        fn,
		output:      output,
		err:         err,
		description: description,
	})

	return self
}

func (self *FakeCmdObjRunner) ExpectArgs(expectedArgs []string, output string, err error) *FakeCmdObjRunner {
	description := fmt.Sprintf("matches args %s", strings.Join(expectedArgs, " "))
	self.ExpectFunc(description, func(cmdObj ICmdObj) bool {
		return slices.Equal(expectedArgs, cmdObj.GetCmd().Args)
	}, output, err)

	return self
}

func (self *FakeCmdObjRunner) ExpectGitArgs(expectedArgs []string, output string, err error) *FakeCmdObjRunner {
	description := fmt.Sprintf("matches git args %s", strings.Join(expectedArgs, " "))
	self.ExpectFunc(description, func(cmdObj ICmdObj) bool {
		return slices.Equal(expectedArgs, cmdObj.GetCmd().Args[1:])
	}, output, err)

	return self
}

func (self *FakeCmdObjRunner) CheckForMissingCalls() {
	self.mutex.Lock()
	defer self.mutex.Unlock()

	remaining := self.remainingExpectedCmds()
	if len(remaining) > 0 {
		self.t.Errorf(
			"expected %d more command(s) to be run. Remaining commands:\n%s",
			len(remaining),
			strings.Join(
				lo.Map(remaining, func(cmdObj CmdObjMatcher, _ int) string {
					return cmdObj.description
				}),
				"\n",
			),
		)
	}
}