Allow re-using the mount_sample binary across runs.
parent
6729af8524
commit
b5e00243ba
|
@ -16,6 +16,7 @@ package samples
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -29,6 +30,11 @@ import (
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var fToolPath = flag.String(
|
||||||
|
"mount_sample",
|
||||||
|
"",
|
||||||
|
"Path to the mount_sample tool. If unset, we will compile it.")
|
||||||
|
|
||||||
// A struct that implements common behavior needed by tests in the samples/
|
// A struct that implements common behavior needed by tests in the samples/
|
||||||
// directory where the file system is mounted by a subprocess. Use it as an
|
// directory where the file system is mounted by a subprocess. Use it as an
|
||||||
// embedded field in your test fixture, calling its SetUp method from your
|
// embedded field in your test fixture, calling its SetUp method from your
|
||||||
|
@ -68,50 +74,58 @@ func (t *SubprocessTest) SetUp(ti *ogletest.TestInfo) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set by buildMountSample.
|
// Private state for getToolPath.
|
||||||
var mountSamplePath string
|
var getToolPath_Path string
|
||||||
var mountSampleErr error
|
var getToolPath_Err error
|
||||||
var mountSampleOnce sync.Once
|
var getToolPath_Once sync.Once
|
||||||
|
|
||||||
// Build the mount_sample tool if it has not yet been built for this process.
|
// Implementation detail of getToolPath.
|
||||||
// Return a path to the binary.
|
func getToolPathImpl() (toolPath string, err error) {
|
||||||
func buildMountSample() (toolPath string, err error) {
|
// Fast path: has the user set the flag?
|
||||||
// Build if we haven't yet.
|
if *fToolPath != "" {
|
||||||
mountSampleOnce.Do(func() {
|
toolPath = *fToolPath
|
||||||
// Create a temporary directory.
|
|
||||||
tempDir, err := ioutil.TempDir("", "")
|
|
||||||
if err != nil {
|
|
||||||
mountSampleErr = fmt.Errorf("TempDir: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
mountSamplePath = path.Join(tempDir, "mount_sample")
|
|
||||||
|
|
||||||
// Build the command.
|
|
||||||
cmd := exec.Command(
|
|
||||||
"go",
|
|
||||||
"build",
|
|
||||||
"-o",
|
|
||||||
mountSamplePath,
|
|
||||||
"github.com/jacobsa/fuse/samples/mount_sample")
|
|
||||||
|
|
||||||
output, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
mountSampleErr = fmt.Errorf(
|
|
||||||
"mount_sample exited with %v, output:\n%s",
|
|
||||||
err,
|
|
||||||
string(output))
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if mountSampleErr != nil {
|
|
||||||
err = mountSampleErr
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
toolPath = mountSamplePath
|
// Create a temporary directory.
|
||||||
|
tempDir, err := ioutil.TempDir("", "")
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("TempDir: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
toolPath = path.Join(tempDir, "mount_sample")
|
||||||
|
|
||||||
|
// Build the command.
|
||||||
|
cmd := exec.Command(
|
||||||
|
"go",
|
||||||
|
"build",
|
||||||
|
"-o",
|
||||||
|
toolPath,
|
||||||
|
"github.com/jacobsa/fuse/samples/mount_sample")
|
||||||
|
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf(
|
||||||
|
"mount_sample exited with %v, output:\n%s",
|
||||||
|
err,
|
||||||
|
string(output))
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the mount_sample tool if it has not yet been built for this process.
|
||||||
|
// Return a path to the binary.
|
||||||
|
func getToolPath() (toolPath string, err error) {
|
||||||
|
// Build if we haven't yet.
|
||||||
|
getToolPath_Once.Do(func() {
|
||||||
|
getToolPath_Path, getToolPath_Err = getToolPathImpl()
|
||||||
|
})
|
||||||
|
|
||||||
|
toolPath, err = getToolPath_Path, getToolPath_Err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,9 +181,9 @@ func (t *SubprocessTest) initialize() (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the mount_sample tool.
|
// Build the mount_sample tool.
|
||||||
toolPath, err := buildMountSample()
|
toolPath, err := getToolPath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("buildMountSample: %v", err)
|
err = fmt.Errorf("getToolPath: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue