forked from upspin/upspin
/
testutil.go
42 lines (37 loc) · 1.15 KB
/
testutil.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
// Copyright 2017 The Upspin Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// NOTE:
// Because testutil is invoked by tests throughout the Upspin tree,
// it cannot import any Upspin packages.
// Package testutil includes utility functions for Upspin tests.
package testutil // import "upspin.io/test/testutil"
import (
"go/build"
"log"
"net"
"path/filepath"
)
// Repo returns the local filename of a file in the Upspin repository.
func Repo(dir ...string) string {
p, err := build.Import("upspin.io", "", build.FindOnly)
if err != nil {
log.Fatal(err)
}
return filepath.Join(p.Dir, filepath.Join(dir...))
}
// PickPort listens to an available port on localhost, closes the listener, and
// returns the port number. This function may be used for finding an available
// port for tests that use the network.
func PickPort() (string, error) {
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
return "", err
}
defer listener.Close()
_, port, err := net.SplitHostPort(listener.Addr().String())
if err != nil {
return "", err
}
return port, err
}