Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/local-app/cmd/root_test.go
2497 views
1
// Copyright (c) 2023 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
"bytes"
9
"errors"
10
"net/http"
11
"net/http/httptest"
12
"net/url"
13
"os"
14
"strings"
15
"testing"
16
17
"github.com/gitpod-io/gitpod/components/public-api/go/client"
18
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
19
"github.com/gitpod-io/local-app/pkg/config"
20
"github.com/gitpod-io/local-app/pkg/prettyprint"
21
"github.com/google/go-cmp/cmp"
22
)
23
24
type CommandTest struct {
25
Name string
26
Commandline []string
27
Config *config.Config
28
Expectation CommandTestExpectation
29
PrepServer func(mux *http.ServeMux)
30
}
31
32
type CommandTestExpectation struct {
33
Error string
34
SystemException bool
35
HasResolutions bool
36
37
Output string
38
}
39
40
// AddActiveTestContext sets the active context to "test" which makes sure we run against the test HTTP server
41
func AddActiveTestContext(cfg *config.Config) *config.Config {
42
cfg.ActiveContext = "test"
43
return cfg
44
}
45
46
func RunCommandTests(t *testing.T, tests []CommandTest) {
47
for _, test := range tests {
48
name := test.Name
49
if name == "" {
50
name = strings.Join(test.Commandline, " ")
51
}
52
t.Run(name, func(t *testing.T) {
53
actual := new(bytes.Buffer)
54
55
cfgfn, err := os.CreateTemp("", "local-app-test-cfg-*.json")
56
if err != nil {
57
t.Fatal(err)
58
}
59
cfgfn.Close()
60
defer os.Remove(cfgfn.Name())
61
62
if test.Config != nil {
63
if test.Config.ActiveContext == "test" {
64
mux := http.NewServeMux()
65
if test.PrepServer != nil {
66
test.PrepServer(mux)
67
}
68
69
apisrv := httptest.NewServer(mux)
70
t.Cleanup(apisrv.Close)
71
72
clnt, err := client.New(client.WithURL(apisrv.URL), client.WithCredentials("hello world"))
73
if err != nil {
74
t.Fatal(err)
75
}
76
rootTestingOpts.Client = clnt
77
78
testurl, err := url.Parse(apisrv.URL)
79
if err != nil {
80
t.Fatal(err)
81
}
82
test.Config.Contexts = map[string]*config.ConnectionContext{
83
"test": {
84
Host: &config.YamlURL{URL: testurl},
85
Token: "hello world",
86
},
87
}
88
}
89
90
err = config.SaveConfig(cfgfn.Name(), test.Config)
91
if err != nil {
92
t.Fatal(err)
93
}
94
}
95
96
rootCmd.SetArgs(test.Commandline)
97
rootCmd.SetOut(actual)
98
rootCmd.SetErr(actual)
99
rootTestingOpts.WriterOut = actual
100
rootOpts.ConfigLocation = cfgfn.Name()
101
err = rootCmd.Execute()
102
103
var act CommandTestExpectation
104
if err != nil {
105
act.Error = err.Error()
106
if se := new(prettyprint.ErrSystemException); errors.As(err, &se) {
107
act.SystemException = true
108
}
109
if re := new(prettyprint.ErrResolution); errors.As(err, &re) {
110
act.HasResolutions = true
111
}
112
}
113
act.Output = actual.String()
114
115
if diff := cmp.Diff(test.Expectation, act); diff != "" {
116
t.Errorf("expectation mismatch (-want +got):\n%s", diff)
117
}
118
})
119
}
120
}
121
122
// fixtureWorkspace returns a workspace fixture
123
func fixtureWorkspace() *v1.Workspace {
124
return &v1.Workspace{
125
WorkspaceId: "workspaceID",
126
OwnerId: "ownerId",
127
ProjectId: "projectId",
128
Context: &v1.WorkspaceContext{
129
ContextUrl: "contextUrl",
130
Details: &v1.WorkspaceContext_Git_{
131
Git: &v1.WorkspaceContext_Git{
132
Repository: &v1.WorkspaceContext_Repository{Name: "name", Owner: "owner"},
133
},
134
},
135
},
136
Description: "description",
137
Status: &v1.WorkspaceStatus{
138
Instance: &v1.WorkspaceInstance{
139
InstanceId: "instanceId",
140
WorkspaceId: "workspaceId",
141
Status: &v1.WorkspaceInstanceStatus{
142
StatusVersion: 1,
143
Phase: v1.WorkspaceInstanceStatus_PHASE_RUNNING,
144
Url: "url",
145
},
146
},
147
},
148
}
149
}
150
151