Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/env_test.go
2498 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"testing"
9
10
"github.com/google/go-cmp/cmp"
11
12
serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"
13
)
14
15
func TestParseArgs(t *testing.T) {
16
tests := []struct {
17
Desc string
18
Input []string
19
Expectation []*serverapi.UserEnvVarValue
20
Error string
21
}{
22
{"empty string", []string{""}, nil, "empty string (correct format is key=value)"},
23
{"invalid - key without value", []string{"K"}, nil, "K has no equal character (correct format is K=some_value)"},
24
{"no key with value", []string{"=value"}, nil, "variable must have a name"},
25
{"simple key value", []string{"key=value"}, []*serverapi.UserEnvVarValue{
26
{Name: "key", Value: "value"},
27
},
28
"",
29
},
30
{"key with empty value", []string{"key=\" \""}, []*serverapi.UserEnvVarValue{
31
{Name: "key", Value: " "},
32
},
33
"",
34
},
35
{"key value with equals", []string{"key=something=else"}, []*serverapi.UserEnvVarValue{
36
{Name: "key", Value: "something=else"},
37
},
38
"",
39
},
40
{"key value empty quoted value", []string{"key=\"\""}, nil, "variable must have a value; use -u to unset a variable"},
41
{"key value containing spaces", []string{"key=\"hello world\""}, []*serverapi.UserEnvVarValue{
42
{Name: "key", Value: "hello world"},
43
},
44
"",
45
},
46
{"key value containing space, quotes stripped by shell", []string{"key=hello world"}, []*serverapi.UserEnvVarValue{
47
{Name: "key", Value: "hello world"},
48
},
49
"",
50
},
51
{"key value containing newline", []string{"key=hello\nworld"}, []*serverapi.UserEnvVarValue{
52
{Name: "key", Value: "hello\nworld"},
53
},
54
"",
55
},
56
{"value containing equals sign", []string{"key=hello=world"}, []*serverapi.UserEnvVarValue{
57
{Name: "key", Value: "hello=world"},
58
},
59
"",
60
},
61
{"value containing quoted equals sign", []string{"key=\"hello=world\""}, []*serverapi.UserEnvVarValue{
62
{Name: "key", Value: "hello=world"},
63
},
64
"",
65
},
66
}
67
68
for _, test := range tests {
69
t.Run(test.Desc, func(t *testing.T) {
70
kv, err := parseArgs(test.Input, "")
71
if err != nil {
72
if test.Error != err.Error() {
73
t.Errorf("expected error '%v' but '%v' was returned", test.Error, err)
74
}
75
76
return
77
}
78
79
if diff := cmp.Diff(test.Expectation, kv); diff != "" {
80
t.Errorf("parseArgs() mismatch (-want +got):\n%s", diff)
81
}
82
})
83
}
84
}
85
86