Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Unified Diff: vpython/filesystem/testfs/tempdir.go

Issue 2703793003: vpython: Add filesystem operation / test packages. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« vpython/filesystem/testfs/build.go ('K') | « vpython/filesystem/testfs/build.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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)
+ }
+ }
+}
« vpython/filesystem/testfs/build.go ('K') | « vpython/filesystem/testfs/build.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698