| 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 |
"bytes" |
| 21 |
"errors" |
| 22 |
"io" |
| 23 |
) |
| 24 |
|
| 25 |
// FakeFile is an implementation of a WriteCloser, that records what has |
| 26 |
// been written in the file (in a bytes.Buffer) and if the file has been |
| 27 |
// closed. |
| 28 |
type FakeFile struct { |
| 29 |
Buffer bytes.Buffer |
| 30 |
Closed bool |
| 31 |
} |
| 32 |
|
| 33 |
var _ io.WriteCloser = &FakeFile{} |
| 34 |
|
| 35 |
// Write appends the contents of p to the Buffer. If the file has |
| 36 |
// already been closed, an error is returned. |
| 37 |
func (f *FakeFile) Write(p []byte) (n int, err error) { |
| 38 |
if f.Closed { |
| 39 |
return 0, errors.New("can't write to closed FakeFile") |
| 40 |
} |
| 41 |
return f.Buffer.Write(p) |
| 42 |
} |
| 43 |
|
| 44 |
// Close records that the file has been closed. If the file has already |
| 45 |
// been closed, an error is returned. |
| 46 |
func (f *FakeFile) Close() error { |
| 47 |
if f.Closed { |
| 48 |
return errors.New("FakeFile was closed multiple times") |
| 49 |
} |
| 50 |
f.Closed = true |
| 51 |
return nil |
| 52 |
} |