Chromium Code Reviews| Index: vpython/filesystem/testfs/tempdir.go |
| diff --git a/vpython/filesystem/testfs/tempdir.go b/vpython/filesystem/testfs/tempdir.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f285b9d599280e60a9fe021295c792d71d231b15 |
| --- /dev/null |
| +++ b/vpython/filesystem/testfs/tempdir.go |
| @@ -0,0 +1,37 @@ |
| +// Copyright 2017 The LUCI Authors. All rights reserved. |
| +// Use of this source code is governed under the Apache License, Version 2.0 |
| +// that can be found in the LICENSE file. |
| + |
| +package testfs |
| + |
| +import ( |
| + "io/ioutil" |
| + |
| + "github.com/luci/luci-go/vpython/filesystem" |
| +) |
| + |
| +// WithTempDir creates a temporary directory and passes it to fn. After fn |
| +// exits, the directory is cleaned up. |
| +func WithTempDir(prefix string, fn func(string) error) error { |
| + tdir, err := ioutil.TempDir("", prefix) |
| + if err != nil { |
| + return err |
| + } |
| + defer func() { |
| + _ = filesystem.RemoveAll(tdir) |
|
iannucci
2017/02/21 08:25:09
worth logging?
dnj
2017/02/21 18:07:49
Done.
|
| + }() |
| + return fn(tdir) |
| +} |
| + |
| +// MustWithTempDir calls WithTempDir and panics if any failures occur. |
| +func MustWithTempDir(prefix string, fn func(string)) func() { |
| + return func() { |
| + err := WithTempDir(prefix, func(tdir string) error { |
| + fn(tdir) |
| + return nil |
| + }) |
| + if err != nil { |
| + panic(err) |
| + } |
| + } |
| +} |