File: trap_command_exit_status_test.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (198 lines) | stat: -rw-r--r-- 6,223 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//go:build !integration
// +build !integration

package shells

import (
	"encoding/json"
	"testing"

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

func TestTryDecode(t *testing.T) {
	exitCode := 0
	script := "script"

	correct, err := json.Marshal(trapCommandExitStatusImpl{
		CommandExitCode: &exitCode,
		Script:          &script,
	})
	require.NoError(t, err)

	missingCommandExitCode, err := json.Marshal(trapCommandExitStatusImpl{
		CommandExitCode: nil,
		Script:          &script,
	})
	require.NoError(t, err)

	missingScripts, err := json.Marshal(trapCommandExitStatusImpl{
		CommandExitCode: &exitCode,
		Script:          nil,
	})
	require.NoError(t, err)

	noFields, err := json.Marshal(trapCommandExitStatusImpl{
		CommandExitCode: nil,
		Script:          nil,
	})
	require.NoError(t, err)

	tests := map[string]struct {
		from                    string
		verifyCommandExitFn     func(t *testing.T, err error, c trapCommandExitStatusImpl)
		verifyTrapCommandExitFn func(t *testing.T, decoded bool, c TrapCommandExitStatus)
	}{
		"TryUnmarshal correct": {
			from: string(correct),
			verifyCommandExitFn: func(t *testing.T, err error, c trapCommandExitStatusImpl) {
				assert.NoError(t, err)
				assert.Equal(t, exitCode, *c.CommandExitCode)
				assert.Equal(t, script, *c.Script)
			},
			verifyTrapCommandExitFn: func(t *testing.T, decoded bool, c TrapCommandExitStatus) {
				assert.True(t, decoded)
				assert.Equal(t, exitCode, c.CommandExitCode)
			},
		},
		"TryUnmarshal only json prefix incorrect": {
			from:                    string(correct[:len(correct)-1]),
			verifyCommandExitFn:     verifyDecodingError,
			verifyTrapCommandExitFn: verifyNotDecoded,
		},
		"TryUnmarshal no json prefix incorrect": {
			from:                    string(correct[1:]),
			verifyCommandExitFn:     verifyDecodingError,
			verifyTrapCommandExitFn: verifyNotDecoded,
		},
		"TryUnmarshal empty": {
			from:                    "",
			verifyCommandExitFn:     verifyDecodingError,
			verifyTrapCommandExitFn: verifyNotDecoded,
		},
		"TryUnmarshal missing exit code": {
			from: string(missingCommandExitCode),
			verifyCommandExitFn: func(t *testing.T, err error, c trapCommandExitStatusImpl) {
				assert.NoError(t, err)
				assert.Nil(t, c.CommandExitCode)
				assert.Equal(t, script, *c.Script)
			},
			verifyTrapCommandExitFn: func(t *testing.T, decoded bool, c TrapCommandExitStatus) {
				assert.False(t, decoded)
				assert.Zero(t, c.CommandExitCode)
			},
		},
		"TryUnmarshal missing scripts": {
			from: string(missingScripts),
			verifyCommandExitFn: func(t *testing.T, err error, c trapCommandExitStatusImpl) {
				assert.NoError(t, err)
				assert.Equal(t, exitCode, *c.CommandExitCode)
				assert.Nil(t, c.Script)
			},
			verifyTrapCommandExitFn: func(t *testing.T, decoded bool, c TrapCommandExitStatus) {
				assert.True(t, decoded)
				assert.Equal(t, exitCode, c.CommandExitCode)
			},
		},
		"TryUnmarshal no fields": {
			from: string(noFields),
			verifyCommandExitFn: func(t *testing.T, err error, c trapCommandExitStatusImpl) {
				assert.NoError(t, err)
				assert.Nil(t, c.CommandExitCode)
				assert.Nil(t, c.Script)
			},
			verifyTrapCommandExitFn: func(t *testing.T, decoded bool, c TrapCommandExitStatus) {
				assert.False(t, decoded)
				assert.Zero(t, c.CommandExitCode)
			},
		},
		"TryUnmarshal hand crafted json with all fields": {
			from: `{"command_exit_code": 0, "script": "script"}`,
			verifyCommandExitFn: func(t *testing.T, err error, c trapCommandExitStatusImpl) {
				assert.NoError(t, err)
				assert.Equal(t, exitCode, *c.CommandExitCode)
				assert.Equal(t, script, *c.Script)
			},
			verifyTrapCommandExitFn: func(t *testing.T, decoded bool, c TrapCommandExitStatus) {
				assert.True(t, decoded)
				assert.Equal(t, exitCode, c.CommandExitCode)
			},
		},
		"TryUnmarshal hand crafted json missing exit code": {
			from: `{"script": "script"}`,
			verifyCommandExitFn: func(t *testing.T, err error, c trapCommandExitStatusImpl) {
				assert.NoError(t, err)
				assert.Nil(t, c.CommandExitCode)
				assert.Equal(t, script, *c.Script)
			},
			verifyTrapCommandExitFn: func(t *testing.T, decoded bool, c TrapCommandExitStatus) {
				assert.False(t, decoded)
				assert.Zero(t, c.CommandExitCode)
			},
		},
		"TryUnmarshal hand crafted json missing script": {
			from: `{"command_exit_code": 0}`,
			verifyCommandExitFn: func(t *testing.T, err error, c trapCommandExitStatusImpl) {
				assert.NoError(t, err)
				assert.Equal(t, exitCode, *c.CommandExitCode)
				assert.Nil(t, c.Script)
			},
			verifyTrapCommandExitFn: func(t *testing.T, decoded bool, c TrapCommandExitStatus) {
				assert.True(t, decoded)
				assert.Equal(t, exitCode, c.CommandExitCode)
			},
		},
		"TryUnmarshal hand crafted empty json": {
			from: "{}",
			verifyCommandExitFn: func(t *testing.T, err error, c trapCommandExitStatusImpl) {
				assert.NoError(t, err)
				assert.Nil(t, c.CommandExitCode)
				assert.Nil(t, c.Script)
			},
			verifyTrapCommandExitFn: func(t *testing.T, decoded bool, c TrapCommandExitStatus) {
				assert.False(t, decoded)
				assert.Zero(t, c.CommandExitCode)
			},
		},
		"TryUnmarshal hand crafted invalid json": {
			from: "{invalid json",
			verifyCommandExitFn: func(t *testing.T, err error, c trapCommandExitStatusImpl) {
				assert.Error(t, err)
				assert.Nil(t, c.CommandExitCode)
				assert.Nil(t, c.Script)
			},
			verifyTrapCommandExitFn: func(t *testing.T, decoded bool, c TrapCommandExitStatus) {
				assert.False(t, decoded)
				assert.Zero(t, c.CommandExitCode)
			},
		},
	}

	for tn, tt := range tests {
		t.Run(tn, func(t *testing.T) {
			var cmd trapCommandExitStatusImpl
			err := cmd.tryUnmarshal(tt.from)
			tt.verifyCommandExitFn(t, err, cmd)

			var c TrapCommandExitStatus
			decoded := c.TryUnmarshal(tt.from)
			tt.verifyTrapCommandExitFn(t, decoded, c)
		})
	}
}

func verifyDecodingError(t *testing.T, err error, c trapCommandExitStatusImpl) {
	t.Helper()

	assert.Error(t, err)
	assert.Nil(t, c.CommandExitCode)
	assert.Nil(t, c.Script)
}

func verifyNotDecoded(t *testing.T, decoded bool, c TrapCommandExitStatus) {
	t.Helper()

	assert.False(t, decoded)
	assert.Zero(t, c.CommandExitCode)
}