Clean up after the tool binary.
parent
6cc47f10ed
commit
12b184c011
|
@ -75,28 +75,36 @@ func (t *SubprocessTest) SetUp(ti *ogletest.TestInfo) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private state for getToolPath.
|
// Private state for getToolPath.
|
||||||
var getToolPath_Path string
|
var getToolContents_Contents []byte
|
||||||
var getToolPath_Err error
|
var getToolContents_Err error
|
||||||
var getToolPath_Once sync.Once
|
var getToolContents_Once sync.Once
|
||||||
|
|
||||||
// Implementation detail of getToolPath.
|
// Implementation detail of getToolPath.
|
||||||
func getToolPathImpl() (toolPath string, err error) {
|
func getToolContentsImpl() (contents []byte, err error) {
|
||||||
// Fast path: has the user set the flag?
|
// Fast path: has the user set the flag?
|
||||||
if *fToolPath != "" {
|
if *fToolPath != "" {
|
||||||
toolPath = *fToolPath
|
contents, err = ioutil.ReadFile(*fToolPath)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("Reading mount_sample contents: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a temporary directory.
|
// Create a temporary directory into which we will compile the tool.
|
||||||
tempDir, err := ioutil.TempDir("", "")
|
tempDir, err := ioutil.TempDir("", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("TempDir: %v", err)
|
err = fmt.Errorf("TempDir: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
toolPath = path.Join(tempDir, "mount_sample")
|
toolPath := path.Join(tempDir, "mount_sample")
|
||||||
|
|
||||||
// Build the command.
|
// Ensure that we kill the temporary directory when we're finished here.
|
||||||
|
defer os.RemoveAll(tempDir)
|
||||||
|
|
||||||
|
// Run "go build".
|
||||||
cmd := exec.Command(
|
cmd := exec.Command(
|
||||||
"go",
|
"go",
|
||||||
"build",
|
"build",
|
||||||
|
@ -114,18 +122,25 @@ func getToolPathImpl() (toolPath string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Slurp the tool contents.
|
||||||
|
contents, err = ioutil.ReadFile(toolPath)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("ReadFile: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the mount_sample tool if it has not yet been built for this process.
|
// Build the mount_sample tool if it has not yet been built for this process.
|
||||||
// Return a path to the binary.
|
// Return its contents.
|
||||||
func getToolPath() (toolPath string, err error) {
|
func getToolContents() (contents []byte, err error) {
|
||||||
// Build if we haven't yet.
|
// Get hold of the binary contents, if we haven't yet.
|
||||||
getToolPath_Once.Do(func() {
|
getToolContents_Once.Do(func() {
|
||||||
getToolPath_Path, getToolPath_Err = getToolPathImpl()
|
getToolContents_Contents, getToolContents_Err = getToolContentsImpl()
|
||||||
})
|
})
|
||||||
|
|
||||||
toolPath, err = getToolPath_Path, getToolPath_Err
|
contents, err = getToolContents_Contents, getToolContents_Err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,10 +195,42 @@ func (t *SubprocessTest) initialize() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the mount_sample tool.
|
// Build/read the mount_sample tool.
|
||||||
toolPath, err := getToolPath()
|
toolContents, err := getToolContents()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("getToolPath: %v", err)
|
err = fmt.Errorf("getTooltoolContents: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a temporary file to hold the contents of the tool.
|
||||||
|
toolFile, err := ioutil.TempFile("", "subprocess_test")
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("TempFile: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer toolFile.Close()
|
||||||
|
|
||||||
|
// Ensure that it is deleted when we leave.
|
||||||
|
toolPath := toolFile.Name()
|
||||||
|
defer os.Remove(toolPath)
|
||||||
|
|
||||||
|
// Write out the tool contents and make them executable.
|
||||||
|
if _, err = toolFile.Write(toolContents); err != nil {
|
||||||
|
err = fmt.Errorf("toolFile.Write: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = toolFile.Chmod(0500); err != nil {
|
||||||
|
err = fmt.Errorf("toolFile.Chmod: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the tool file to prevent "text file busy" errors below.
|
||||||
|
err = toolFile.Close()
|
||||||
|
toolFile = nil
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("toolFile.Close: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,6 +310,8 @@ func (t *SubprocessTest) TearDown() {
|
||||||
|
|
||||||
// Like TearDown, but doesn't panic.
|
// Like TearDown, but doesn't panic.
|
||||||
func (t *SubprocessTest) destroy() (err error) {
|
func (t *SubprocessTest) destroy() (err error) {
|
||||||
|
// Make sure we clean up after ourselves after everything else below.
|
||||||
|
|
||||||
// Close what is necessary.
|
// Close what is necessary.
|
||||||
for _, c := range t.ToClose {
|
for _, c := range t.ToClose {
|
||||||
if c == nil {
|
if c == nil {
|
||||||
|
@ -298,8 +347,8 @@ func (t *SubprocessTest) destroy() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to unlink the mount point.
|
// Clean up.
|
||||||
os.Remove(t.Dir)
|
ogletest.ExpectEq(nil, os.Remove(t.Dir))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Wait for the subprocess.
|
// Wait for the subprocess.
|
||||||
|
|
Loading…
Reference in New Issue