forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.go
53 lines (43 loc) · 1.27 KB
/
testing.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
package bitbucketcloud
import (
"net/url"
"os"
"path/filepath"
"testing"
"github.com/sourcegraph/sourcegraph/internal/httpcli"
"github.com/sourcegraph/sourcegraph/internal/httptestutil"
"github.com/sourcegraph/sourcegraph/internal/lazyregexp"
)
func GetenvTestBitbucketCloudUsername() string {
username := os.Getenv("BITBUCKET_CLOUD_USERNAME")
if username == "" {
username = "unknwon"
}
return username
}
// NewTestClient returns a bitbucketcloud.Client that records its interactions
// to testdata/vcr/.
func NewTestClient(t testing.TB, name string, update bool, apiURL *url.URL) (*Client, func()) {
t.Helper()
cassete := filepath.Join("testdata/vcr/", normalize(name))
rec, err := httptestutil.NewRecorder(cassete, update)
if err != nil {
t.Fatal(err)
}
hc, err := httpcli.NewFactory(nil, httptestutil.NewRecorderOpt(rec)).Doer()
if err != nil {
t.Fatal(err)
}
cli := NewClient(apiURL, hc)
cli.Username = GetenvTestBitbucketCloudUsername()
cli.AppPassword = os.Getenv("BITBUCKET_CLOUD_APP_PASSWORD")
return cli, func() {
if err := rec.Stop(); err != nil {
t.Errorf("failed to update test data: %s", err)
}
}
}
var normalizer = lazyregexp.New("[^A-Za-z0-9-]+")
func normalize(path string) string {
return normalizer.ReplaceAllLiteralString(path, "-")
}