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

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

Issue 2703793003: vpython: Add filesystem operation / test packages. (Closed)
Patch Set: test now path 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
« no previous file with comments | « 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..96a05d5df71223620d080d0729eacfc17f0b372f
--- /dev/null
+++ b/vpython/filesystem/testfs/tempdir.go
@@ -0,0 +1,40 @@
+// 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"
+ "testing"
+
+ "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(t *testing.T, prefix string, fn func(string) error) error {
+ tdir, err := ioutil.TempDir("", prefix)
+ if err != nil {
+ return err
+ }
+ defer func() {
+ if rmErr := filesystem.RemoveAll(tdir); rmErr != nil {
+ t.Errorf("failed to remove temporary directory [%s]: %s", tdir, rmErr)
+ }
+ }()
+ return fn(tdir)
+}
+
+// MustWithTempDir calls WithTempDir and panics if any failures occur.
+func MustWithTempDir(t *testing.T, prefix string, fn func(string)) func() {
+ return func() {
+ err := WithTempDir(t, prefix, func(tdir string) error {
+ fn(tdir)
+ return nil
+ })
+ if err != nil {
+ panic(err)
+ }
+ }
+}
« no previous file with comments | « vpython/filesystem/testfs/build.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698