| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package testfs |
| 6 |
| 7 import ( |
| 8 "io/ioutil" |
| 9 "testing" |
| 10 |
| 11 "github.com/luci/luci-go/vpython/filesystem" |
| 12 ) |
| 13 |
| 14 // WithTempDir creates a temporary directory and passes it to fn. After fn |
| 15 // exits, the directory is cleaned up. |
| 16 func WithTempDir(t *testing.T, prefix string, fn func(string) error) error { |
| 17 tdir, err := ioutil.TempDir("", prefix) |
| 18 if err != nil { |
| 19 return err |
| 20 } |
| 21 defer func() { |
| 22 if rmErr := filesystem.RemoveAll(tdir); rmErr != nil { |
| 23 t.Errorf("failed to remove temporary directory [%s]: %s"
, tdir, rmErr) |
| 24 } |
| 25 }() |
| 26 return fn(tdir) |
| 27 } |
| 28 |
| 29 // MustWithTempDir calls WithTempDir and panics if any failures occur. |
| 30 func MustWithTempDir(t *testing.T, prefix string, fn func(string)) func() { |
| 31 return func() { |
| 32 err := WithTempDir(t, prefix, func(tdir string) error { |
| 33 fn(tdir) |
| 34 return nil |
| 35 }) |
| 36 if err != nil { |
| 37 panic(err) |
| 38 } |
| 39 } |
| 40 } |
| OLD | NEW |