File: shell_parser_test.go

package info (click to toggle)
golang-github-openshift-imagebuilder 1.2.15%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,776 kB
  • sloc: makefile: 18; sh: 3; ansic: 1
file content (77 lines) | stat: -rw-r--r-- 2,501 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
package imagebuilder

import (
	"os/exec"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestProcessWord(t *testing.T) {
	envs := []string{
		"EDITOR=vim",
		"FOUREYES=iiii",
		"XMODIFIERS=@im=ibus",
	}
	testCases := []struct {
		pattern, expected string
		expectError       bool
	}{
		{"A", "A", false},
		{"${EDITOR}", "vim", false},
		{"${EDITOR:+emacs}", "emacs", false},
		{"${EDITOR:-emacs}", "vim", false},
		{"${EDITOR:-${FOUREYES}}", "vim", false},
		{"${EDITOR:+${FOUREYES}}", "iiii", false},
		{"${XMODIFIERS#*i}", "m=ibus", false},
		{"${XMODIFIERS##*i}", "bus", false},
		{"${XMODIFIERS#i*}", "@im=ibus", false},
		{"${XMODIFIERS##i*}", "@im=ibus", false},
		{"${XMODIFIERS%i*}", "@im=", false},
		{"${XMODIFIERS%%i*}", "@", false},
		{"${XMODIFIERS%*i}", "@im=ibus", false},
		{"${XMODIFIERS%%*i}", "@im=ibus", false},
		{"${XMODIFIERS/i/I}", "@Im=ibus", false},
		{"${XMODIFIERS//i/I}", "@Im=Ibus", false},
		{"${XMODIFIERS/#i/I}", "@im=ibus", false},
		{"${XMODIFIERS/%i/I}", "@im=ibus", false},
		{"${XMODIFIERS/#@/AT}", "ATim=ibus", false},
		{"${XMODIFIERS/#@}", "im=ibus", false},
		{"${XMODIFIERS/%s/S}", "@im=ibuS", false},
		{"${XMODIFIERS/%s}", "@im=ibu", false},
		{"${XMODIFIERS//i/aye}", "@ayem=ayebus", false},
		{"${XMODIFIERS//b/BEE}", "@im=iBEEus", false},
		{"${EDITOR/${EDITOR}/}", "", false},
		{"${EDITOR/${EDITOR}}", "", false},
		{"${EDITOR//${EDITOR}/}", "", false},
		{"${EDITOR//${EDITOR}}", "", false},
		{"${FOUREYES/ii/${EDITOR}}", "vimii", false},
		{"${FOUREYES//i/${EDITOR}}", "vimvimvimvim", false},
		{"${FOUREYES//ii/${EDITOR}}", "vimvim", false},
		{"${FOUREYES//iii/${EDITOR}}", "vimi", false},
		{"${FOUREYES//iii/${EDITOR}", "vimi", true},
	}
	for _, testCase := range testCases {
		t.Run(testCase.pattern, func(t *testing.T) {
			actual, err := ProcessWord(testCase.pattern, envs)
			if testCase.expectError {
				require.Error(t, err)
				t.Logf("got expected error %v", err)
			} else {
				require.NoError(t, err)
				require.Equal(t, testCase.expected, actual)
			}
			// We're probably not as flexible as the shell, but at least don't be incompatible with it
			cmd := exec.Command("bash", "-c", "echo -n "+testCase.pattern)
			cmd.Env = append(cmd.Env, envs...)
			output, err := cmd.CombinedOutput()
			if testCase.expectError {
				require.Error(t, err)
				t.Logf("got expected error %v (%s)", err, string(output))
			} else {
				require.NoError(t, err)
				require.Equal(t, testCase.expected, string(output))
			}
		})
	}
}