File: cmd_obj_runner_test.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 (131 lines) | stat: -rw-r--r-- 3,592 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
package oscommands

import (
	"strings"
	"testing"

	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/utils"
)

func getRunner() *cmdObjRunner {
	log := utils.NewDummyLog()
	return &cmdObjRunner{
		log:   log,
		guiIO: NewNullGuiIO(log),
	}
}

func toChanFn(f func(ct CredentialType) string) func(CredentialType) <-chan string {
	return func(ct CredentialType) <-chan string {
		ch := make(chan string)

		go func() {
			ch <- f(ct)
		}()

		return ch
	}
}

func TestProcessOutput(t *testing.T) {
	defaultPromptUserForCredential := func(ct CredentialType) string {
		switch ct {
		case Password:
			return "password"
		case Username:
			return "username"
		case Passphrase:
			return "passphrase"
		case PIN:
			return "pin"
		case Token:
			return "token"
		default:
			panic("unexpected credential type")
		}
	}

	scenarios := []struct {
		name                    string
		promptUserForCredential func(CredentialType) string
		output                  string
		expectedToWrite         string
	}{
		{
			name:                    "no output",
			promptUserForCredential: defaultPromptUserForCredential,
			output:                  "",
			expectedToWrite:         "",
		},
		{
			name:                    "password prompt",
			promptUserForCredential: defaultPromptUserForCredential,
			output:                  "Password:",
			expectedToWrite:         "password",
		},
		{
			name:                    "password prompt 2",
			promptUserForCredential: defaultPromptUserForCredential,
			output:                  "Bill's password:",
			expectedToWrite:         "password",
		},
		{
			name:                    "password prompt 3",
			promptUserForCredential: defaultPromptUserForCredential,
			output:                  "Password for 'Bill':",
			expectedToWrite:         "password",
		},
		{
			name:                    "username prompt",
			promptUserForCredential: defaultPromptUserForCredential,
			output:                  "Username for 'Bill':",
			expectedToWrite:         "username",
		},
		{
			name:                    "passphrase prompt",
			promptUserForCredential: defaultPromptUserForCredential,
			output:                  "Enter passphrase for key '123':",
			expectedToWrite:         "passphrase",
		},
		{
			name:                    "pin prompt",
			promptUserForCredential: defaultPromptUserForCredential,
			output:                  "Enter PIN for key '123':",
			expectedToWrite:         "pin",
		},
		{
			name:                    "2FA token prompt",
			promptUserForCredential: defaultPromptUserForCredential,
			output:                  "testuser 2FA Token (citadel)",
			expectedToWrite:         "token",
		},
		{
			name:                    "username and password prompt",
			promptUserForCredential: defaultPromptUserForCredential,
			output:                  "Password:\nUsername for 'Alice':\n",
			expectedToWrite:         "passwordusername",
		},
		{
			name:                    "user submits empty credential",
			promptUserForCredential: func(ct CredentialType) string { return "" },
			output:                  "Password:\n",
			expectedToWrite:         "",
		},
	}

	for _, scenario := range scenarios {
		t.Run(scenario.name, func(t *testing.T) {
			runner := getRunner()
			reader := strings.NewReader(scenario.output)
			writer := &strings.Builder{}

			task := gocui.NewFakeTask()
			runner.processOutput(reader, writer, toChanFn(scenario.promptUserForCredential), task)

			if writer.String() != scenario.expectedToWrite {
				t.Errorf("expected to write '%s' but got '%s'", scenario.expectedToWrite, writer.String())
			}
		})
	}
}