restic/internal/fs/helpers.go

44 lines
798 B
Go
Raw Normal View History

2017-12-23 12:12:36 +01:00
package fs
2017-12-16 00:01:28 +01:00
import (
"os"
"testing"
"github.com/restic/restic/internal/test"
2017-12-16 00:01:28 +01:00
)
2017-12-23 12:12:36 +01:00
// IsRegularFile returns true if fi belongs to a normal file. If fi is nil,
// false is returned.
func IsRegularFile(fi os.FileInfo) bool {
if fi == nil {
return false
}
return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0
}
2017-12-16 00:01:28 +01:00
// TestChdir changes the current directory to dest, the function back returns to the previous directory.
func TestChdir(t testing.TB, dest string) (back func()) {
test.Helper(t).Helper()
2017-12-16 00:01:28 +01:00
prev, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
t.Logf("chdir to %v", dest)
err = os.Chdir(dest)
if err != nil {
t.Fatal(err)
}
return func() {
test.Helper(t).Helper()
2017-12-16 00:01:28 +01:00
t.Logf("chdir back to %v", prev)
err = os.Chdir(prev)
if err != nil {
t.Fatal(err)
}
}
}