summaryrefslogtreecommitdiffstats
path: root/pkg/commands/oscommands/os_windows_test.go
blob: 60853b8411207c09cc2fa76fda1d33e3147c33e7 (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
//go:build windows
// +build windows

package oscommands

import (
	"os/exec"
	"testing"

	"github.com/jesseduffield/lazygit/pkg/secureexec"
	"github.com/stretchr/testify/assert"
)

// TestOSCommandOpenFileWindows tests the OpenFile command on Linux
func TestOSCommandOpenFileWindows(t *testing.T) {
	type scenario struct {
		filename string
		command  func(string, ...string) *exec.Cmd
		test     func(error)
	}

	scenarios := []scenario{
		{
			"test",
			func(name string, arg ...string) *exec.Cmd {
				return secureexec.Command("exit", "1")
			},
			func(err error) {
				assert.Error(t, err)
			},
		},
		{
			"test",
			func(name string, arg ...string) *exec.Cmd {
				assert.Equal(t, "cmd", name)
				assert.Equal(t, []string{"/c", "start", "", "test"}, arg)
				return secureexec.Command("echo")
			},
			func(err error) {
				assert.NoError(t, err)
			},
		},
		{
			"filename with spaces",
			func(name string, arg ...string) *exec.Cmd {
				assert.Equal(t, "cmd", name)
				assert.Equal(t, []string{"/c", "start", "", "filename with spaces"}, arg)
				return secureexec.Command("echo")
			},
			func(err error) {
				assert.NoError(t, err)
			},
		},
		{
			"let's_test_with_single_quote",
			func(name string, arg ...string) *exec.Cmd {
				assert.Equal(t, "cmd", name)
				assert.Equal(t, []string{"/c", "start", "", "let's_test_with_single_quote"}, arg)
				return secureexec.Command("echo")
			},
			func(err error) {
				assert.NoError(t, err)
			},
		},
		{
			"$USER.txt",
			func(name string, arg ...string) *exec.Cmd {
				assert.Equal(t, "cmd", name)
				assert.Equal(t, []string{"/c", "start", "", "$USER.txt"}, arg)
				return secureexec.Command("echo")
			},
			func(err error) {
				assert.NoError(t, err)
			},
		},
	}

	for _, s := range scenarios {
		OSCmd := NewDummyOSCommand()
		OSCmd.Command = s.command
		OSCmd.Platform.OS = "windows"
		OSCmd.UserConfig.OS.OpenCommand = `start "" {{filename}}`

		s.test(OSCmd.OpenFile(s.filename))
	}
}