mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-14 02:57:52 +08:00
1c133f6979
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.
27 lines
490 B
Go
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)
|
|
}
|