-
Notifications
You must be signed in to change notification settings - Fork 2
/
testhelper_test.go
64 lines (56 loc) · 1.64 KB
/
testhelper_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
package bindown
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/udhos/equalfile"
)
func downloadablesPath(path string) string {
return filepath.Join(projectPath("testdata", "downloadables"), filepath.FromSlash(path))
}
// projectPath exchanges a path relative to the project root for an absolute path
func projectPath(path ...string) string {
return filepath.Join(projectRoot(), filepath.Join(path...))
}
// projectRoot returns the absolute path of the project root
func projectRoot() string {
_, file, _, _ := runtime.Caller(0)
return filepath.Dir(file)
}
// tmpDir returns the path to a newly created tmp dir and a function for deleting that dir
func tmpDir(t *testing.T) (string, func()) {
t.Helper()
projectTmp := projectPath("tmp")
err := os.MkdirAll(projectTmp, 0750)
require.NoError(t, err)
tmpdir, err := ioutil.TempDir(projectTmp, "")
require.NoError(t, err)
return tmpdir, func() {
require.NoError(t, os.RemoveAll(tmpdir))
}
}
func serveFile(file, path, query string) *httptest.Server {
file = filepath.FromSlash(file)
mux := http.NewServeMux()
mux.HandleFunc(path, func(w http.ResponseWriter, req *http.Request) {
if req.URL.RawQuery != query {
http.Error(w, "not found", http.StatusNotFound)
return
}
http.ServeFile(w, req, file)
})
return httptest.NewServer(mux)
}
func assertEqualFiles(t testing.TB, want, actual string) bool {
t.Helper()
cmp := equalfile.New(nil, equalfile.Options{})
equal, err := cmp.CompareFile(want, actual)
assert.NoError(t, err)
return assert.True(t, equal)
}