Chromium Code Reviews| 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 | |
| 10 "github.com/luci/luci-go/vpython/filesystem" | |
| 11 ) | |
| 12 | |
| 13 // WithTempDir creates a temporary directory and passes it to fn. After fn | |
| 14 // exits, the directory is cleaned up. | |
| 15 func WithTempDir(prefix string, fn func(string) error) error { | |
| 16 tdir, err := ioutil.TempDir("", prefix) | |
| 17 if err != nil { | |
| 18 return err | |
| 19 } | |
| 20 defer func() { | |
| 21 _ = filesystem.RemoveAll(tdir) | |
|
iannucci
2017/02/21 08:25:09
worth logging?
dnj
2017/02/21 18:07:49
Done.
| |
| 22 }() | |
| 23 return fn(tdir) | |
| 24 } | |
| 25 | |
| 26 // MustWithTempDir calls WithTempDir and panics if any failures occur. | |
| 27 func MustWithTempDir(prefix string, fn func(string)) func() { | |
| 28 return func() { | |
| 29 err := WithTempDir(prefix, func(tdir string) error { | |
| 30 fn(tdir) | |
| 31 return nil | |
| 32 }) | |
| 33 if err != nil { | |
| 34 panic(err) | |
| 35 } | |
| 36 } | |
| 37 } | |
| OLD | NEW |