-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd_test.go
More file actions
107 lines (98 loc) · 2.49 KB
/
Copy pathcmd_test.go
File metadata and controls
107 lines (98 loc) · 2.49 KB
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
package main
import (
"bytes"
"context"
"flag"
"testing"
"github.com/jdevoo/gen/core"
)
// TestValidateEnv tests the credentials environment variables validation.
func TestValidateEnv(t *testing.T) {
clearEnv := func(t *testing.T) {
t.Setenv("GOOGLE_CLOUD_PROJECT", "")
t.Setenv("GOOGLE_API_KEY", "")
t.Setenv("GOOGLE_CLOUD_LOCATION", "")
t.Setenv("GOOGLE_GENAI_USE_VERTEXAI", "")
}
tests := []struct {
name string
setup func(t *testing.T)
wantErr bool
}{
{
name: "Neither set",
setup: func(t *testing.T) {
clearEnv(t)
},
wantErr: true,
},
{
name: "Only API key set",
setup: func(t *testing.T) {
clearEnv(t)
t.Setenv("GOOGLE_API_KEY", "secret-key")
},
wantErr: false,
},
{
name: "Cloud Project set but Location missing",
setup: func(t *testing.T) {
clearEnv(t)
t.Setenv("GOOGLE_CLOUD_PROJECT", "my-project")
},
wantErr: true,
},
{
name: "Cloud Project and Location set",
setup: func(t *testing.T) {
clearEnv(t)
t.Setenv("GOOGLE_CLOUD_PROJECT", "my-project")
t.Setenv("GOOGLE_CLOUD_LOCATION", "us-central1")
},
wantErr: false,
},
{
name: "Both key and project set, but vertexai missing",
setup: func(t *testing.T) {
clearEnv(t)
t.Setenv("GOOGLE_API_KEY", "secret-key")
t.Setenv("GOOGLE_CLOUD_PROJECT", "my-project")
t.Setenv("GOOGLE_CLOUD_LOCATION", "us-central1")
},
wantErr: true,
},
{
name: "Both key and project set, vertexai true",
setup: func(t *testing.T) {
clearEnv(t)
t.Setenv("GOOGLE_API_KEY", "secret-key")
t.Setenv("GOOGLE_CLOUD_PROJECT", "my-project")
t.Setenv("GOOGLE_CLOUD_LOCATION", "us-central1")
t.Setenv("GOOGLE_GENAI_USE_VERTEXAI", "true")
},
wantErr: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.setup(t)
err := validateEnv()
if (err != nil) != tc.wantErr {
t.Errorf("validateEnv() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}
// TestEmitUsage tests the customized CLI usage instructions output.
func TestEmitUsage(t *testing.T) {
var buf bytes.Buffer
ctx := context.WithValue(context.Background(), core.ParamsKey, &core.Parameters{})
oldOutput := flag.CommandLine.Output()
flag.CommandLine.SetOutput(&buf)
defer flag.CommandLine.SetOutput(oldOutput)
emitUsage(ctx, &buf, false)
output := buf.String()
if !bytes.Contains(buf.Bytes(), []byte("Usage: gen [options] <prompt>")) {
t.Errorf("emitUsage output did not contain Usage text, got %q", output)
}
}