-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain_test.go
65 lines (55 loc) · 1.54 KB
/
main_test.go
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
package main_test
import (
"os"
"path/filepath"
"testing"
"github.com/eliben/gemini-cli/internal/commands"
"github.com/rogpeppe/go-internal/testscript"
)
func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"gemini-cli": commands.Execute,
}))
}
func TestScript(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "test/scripts",
TestWork: true,
Setup: func(env *testscript.Env) error {
// Make all the files from test/datafiles available for tests in
// their datafiles/ directory.
rootdir, err := os.Getwd()
check(t, err)
copyDataFiles(t,
filepath.Join(rootdir, "test", "datafiles"),
filepath.Join(env.WorkDir, "datafiles"))
// Propagate the test env's GEMINI_API_KEY to scripts.
env.Setenv("GEMINI_API_KEY", os.Getenv("GEMINI_API_KEY"))
// This is to help testing some error scenarios.
env.Setenv("TEST_API_KEY", os.Getenv("GEMINI_API_KEY"))
return nil
},
})
}
func check(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}
// copyDataFiles copies all files from rootdir to targetdir, creating
// targetdir if needed
func copyDataFiles(t *testing.T, rootdir string, targetdir string) {
check(t, os.MkdirAll(targetdir, 0777))
entries, err := os.ReadDir(rootdir)
check(t, err)
for _, e := range entries {
if !e.IsDir() {
fullpath := filepath.Join(rootdir, e.Name())
targetpath := filepath.Join(targetdir, e.Name())
data, err := os.ReadFile(fullpath)
check(t, err)
err = os.WriteFile(targetpath, data, 0666)
check(t, err)
}
}
}