-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain_test.go
83 lines (75 loc) · 2.09 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/rogpeppe/go-internal/testscript"
"mvdan.cc/fdroidcl/adb"
)
func TestMain(m *testing.M) {
if os.Getenv("TESTSCRIPT_COMMAND") == "" {
// start the static http server once
path := filepath.Join("testdata", "staticrepo")
fs := http.FileServer(http.Dir(path))
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// The files are static, so add a unique etag for each file.
w.Header().Set("Etag", strconv.Quote(r.URL.Path))
fs.ServeHTTP(w, r)
})
ln, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
server := &http.Server{Handler: handler}
go server.Serve(ln)
// Save it to a global, which will be added as an env var to be
// picked up by the children processes.
staticRepoHost = ln.Addr().String()
} else {
httpClient.Transport = repoTransport{os.Getenv("REPO_HOST")}
}
os.Exit(testscript.RunMain(m, map[string]func() int{
"fdroidcl": main1,
}))
}
type repoTransport struct {
repoHost string
}
func (t repoTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// replace https://f-droid.org/repo/foo with http://localhost:1234/foo
req.URL.Scheme = "http"
req.URL.Host = t.repoHost
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/repo")
return http.DefaultClient.Do(req)
}
var staticRepoHost string
func TestScripts(t *testing.T) {
t.Parallel()
testscript.Run(t, testscript.Params{
Dir: filepath.Join("testdata", "scripts"),
Setup: func(e *testscript.Env) error {
home := e.WorkDir + "/home"
if err := os.MkdirAll(home, 0o777); err != nil {
return err
}
e.Vars = append(e.Vars, "HOME="+home)
e.Vars = append(e.Vars, "AppData="+home)
e.Vars = append(e.Vars, "LocalAppData="+home)
e.Vars = append(e.Vars, "REPO_HOST="+staticRepoHost)
return nil
},
Condition: func(cond string) (bool, error) {
switch cond {
case "device":
devices, err := adb.Devices()
return err == nil && len(devices) == 1, nil
}
return false, fmt.Errorf("unknown condition %q", cond)
},
})
}