| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package temptest |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
|
| 24 |
"k8s.io/utils/temp" |
| 25 |
) |
| 26 |
|
| 27 |
func TestedCode(dir temp.Directory) error { |
| 28 |
f, err := dir.NewFile("filename") |
| 29 |
if err != nil { |
| 30 |
return err |
| 31 |
} |
| 32 |
_, err = io.WriteString(f, "Bonjour!") |
| 33 |
if err != nil { |
| 34 |
return err |
| 35 |
} |
| 36 |
return dir.Delete() |
| 37 |
} |
| 38 |
|
| 39 |
func Example() { |
| 40 |
dir := FakeDir{} |
| 41 |
|
| 42 |
err := TestedCode(&dir) |
| 43 |
if err != nil { |
| 44 |
panic(err) |
| 45 |
} |
| 46 |
|
| 47 |
if dir.Deleted == false { |
| 48 |
panic(errors.New("Directory should have been deleted")) |
| 49 |
} |
| 50 |
|
| 51 |
if dir.Files["filename"] == nil { |
| 52 |
panic(errors.New(`"filename" should have been created`)) |
| 53 |
} |
| 54 |
|
| 55 |
fmt.Println(dir.Files["filename"].Buffer.String()) |
| 56 |
// Output: Bonjour! |
| 57 |
} |