elvish/util/tempdir.go
Qi Xiao 1c133f6979 util: Change the API of WithTempDir and InTempDir.
They have been renamed to TestDir and InTestDir, and instead of taking a
callback, they return a cleanup function. This saves one level of
indentation in the caller.
2018-10-13 14:04:13 +01:00

27 lines
490 B
Go

package util
import (
"os"
)
// InTempDir is like WithTempDir, but also cd into the directory before running
// the function, and cd backs after running the function if possible.
//
// It panics if it could not get the working directory or change directory.
//
// It is useful in tests.
func InTempDir(f func(string)) {
tmpdir, cleanup := TestDir()
defer cleanup()
oldpwd, err := os.Getwd()
if err != nil {
panic(err)
}
mustChdir(tmpdir)
defer mustChdir(oldpwd)
f(tmpdir)
}