-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathscript_test.go
More file actions
171 lines (140 loc) · 4.5 KB
/
Copy pathscript_test.go
File metadata and controls
171 lines (140 loc) · 4.5 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
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
//go:build script
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"github.com/rogpeppe/go-internal/diff"
"github.com/rogpeppe/go-internal/testscript"
"github.com/stretchr/testify/require"
"go.abhg.dev/gs/internal/forge/shamhub"
"go.abhg.dev/gs/internal/git/gittest"
"go.abhg.dev/gs/internal/secret/secrettest"
)
var (
_debug = flag.Bool("debug", false, "enable debug logging")
_shardIndex = flag.Int("shard-index", 0, "index of the test shard to run")
_shardCount = flag.Int("shard-count", 1, "total number of test shards")
)
func TestScript(t *testing.T) {
defaultEnv := gittest.DefaultConfig().EnvMap()
defaultEnv["EDITOR"] = "mockedit"
// Add a default author to all commits.
// Tests can override with 'as' and 'at'.
defaultEnv["GIT_AUTHOR_NAME"] = "Test"
defaultEnv["GIT_AUTHOR_EMAIL"] = "test@example.com"
defaultEnv["GIT_COMMITTER_NAME"] = "Test"
defaultEnv["GIT_COMMITTER_EMAIL"] = "test@example.com"
if *_debug {
defaultEnv["GIT_SPICE_VERBOSE"] = "true"
}
var shamhubSetupCmd shamhub.SetupCmd
scriptDir := filepath.Join("testdata", "script")
if *_shardCount > 1 {
// If we're running in sharded mode,
// copy a subset of the test scripts
// into a temporary directory based on the shard index.
scripts, err := filepath.Glob(filepath.Join(scriptDir, "*.txt"))
require.NoError(t, err)
shardScriptDir := t.TempDir()
for i, script := range scripts {
if i%*_shardCount != *_shardIndex {
// This script does not belong to this shard.
continue
}
bs, err := os.ReadFile(script)
require.NoError(t, err)
dst := filepath.Join(shardScriptDir, filepath.Base(script))
require.NoError(t, os.WriteFile(dst, bs, 0o644))
}
t.Logf("Using shard script directory: %s", shardScriptDir)
scriptDir = shardScriptDir
}
testscript.Run(t, testscript.Params{
Dir: scriptDir,
UpdateScripts: updateFlag(),
RequireUniqueNames: true,
Setup: func(e *testscript.Env) error {
t := e.T().(testing.TB)
homeDir := filepath.Join(e.WorkDir, "home")
require.NoError(t, os.Mkdir(homeDir, 0o755))
e.Setenv("HOME", homeDir)
e.Setenv("XDG_CONFIG_HOME", filepath.Join(homeDir, ".config"))
for k, v := range defaultEnv {
e.Setenv(k, v)
}
secretServer := secrettest.NewServer(t)
t.Logf("Secret server URL: %s", secretServer.URL())
e.Setenv("SECRET_SERVER_URL", secretServer.URL())
shamhubSetupCmd.Setup(t, e)
return nil
},
Condition: func(cond string) (bool, error) {
if wantVersion, ok := strings.CutPrefix(cond, "git:"); ok {
return gittest.CondGitVersion(wantVersion)
}
return false, fmt.Errorf("unknown condition: %q", cond)
},
Cmds: map[string]func(*testscript.TestScript, bool, []string){
"git": gittest.CmdGit,
"as": gittest.CmdAs,
"at": func(ts *testscript.TestScript, b bool, s []string) {
gittest.CmdAt(ts, b, s)
// Set the Git-speciifc environment variables,
// as well as git-spice's own GIT_SPICE_NOW.
// Tests that want a different behavior for log
// can set GIT_SPICE_NOW to a different value.
ts.Setenv("GIT_SPICE_NOW", ts.Getenv("GIT_COMMITTER_DATE"))
},
"cmpenvJSON": cmdCmpenvJSON,
"shamhub-setup": shamhubSetupCmd.Run,
"home": func(ts *testscript.TestScript, _ bool, args []string) {
if len(args) != 1 {
ts.Fatalf("usage: sethome <path>")
}
home := ts.MkAbs(args[0])
ts.Setenv("HOME", home)
ts.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
if runtime.GOOS == "windows" {
ts.Setenv("USERPROFILE", home)
}
},
},
})
}
func cmdCmpenvJSON(ts *testscript.TestScript, neg bool, args []string) {
if len(args) != 2 {
ts.Fatalf("usage: cmpjson file1 file2")
}
name1, name2 := args[0], args[1]
data1 := []byte(ts.ReadFile(name1))
data2, err := os.ReadFile(ts.MkAbs(name2))
ts.Check(err)
// Expand environment variables in data2.
data2 = []byte(os.Expand(string(data2), ts.Getenv))
var json1, json2 any
ts.Check(json.Unmarshal(data1, &json1))
ts.Check(json.Unmarshal(data2, &json2))
if reflect.DeepEqual(json1, json2) == !neg {
// Matches expectation.
return
}
prettyJSON1, err := json.MarshalIndent(json1, "", " ")
ts.Check(err)
if neg {
ts.Logf("%s", prettyJSON1)
ts.Fatalf("%s and %s do not differ", name1, name2)
return
}
prettyJSON2, err := json.MarshalIndent(json2, "", " ")
ts.Check(err)
unifiedDiff := diff.Diff(name1, prettyJSON1, name2, prettyJSON2)
ts.Logf("%s", unifiedDiff)
ts.Fatalf("%s and %s differ", name1, name2)
}