package temptest
import "k8s.io/utils/temp/temptest"
Package temptest provides utilities for testing temp
files/directories testing.
Code:play
Output:Example¶
package main
import (
"errors"
"fmt"
"io"
"k8s.io/utils/temp"
)
func TestedCode(dir temp.Directory) error {
f, err := dir.NewFile("filename")
if err != nil {
return err
}
_, err = io.WriteString(f, "Bonjour!")
if err != nil {
return err
}
return dir.Delete()
}
func main() {
dir := FakeDir{}
err := TestedCode(&dir)
if err != nil {
panic(err)
}
if dir.Deleted == false {
panic(errors.New("Directory should have been deleted"))
}
if dir.Files["filename"] == nil {
panic(errors.New(`"filename" should have been created`))
}
fmt.Println(dir.Files["filename"].Buffer.String())
}
Bonjour!
Index ¶
Examples ¶
Types ¶
type FakeDir ¶
FakeDir implements a Directory that is not backed on the filesystem. This is useful for testing since the created "files" are simple bytes.Buffer that can be inspected.
func (*FakeDir) Delete ¶
Delete doesn't remove anything, but records that the directory has been deleted.
func (*FakeDir) NewFile ¶
func (d *FakeDir) NewFile(name string) (io.WriteCloser, error)
NewFile returns a new FakeFile if the filename doesn't exist already. This function will fail if the directory has already been deleted.
type FakeFile ¶
FakeFile is an implementation of a WriteCloser, that records what has been written in the file (in a bytes.Buffer) and if the file has been closed.
func (*FakeFile) Close ¶
Close records that the file has been closed. If the file has already been closed, an error is returned.
func (*FakeFile) Write ¶
Write appends the contents of p to the Buffer. If the file has already been closed, an error is returned.
Source Files ¶
dir.go doc.go file.go
- Version
- v0.0.0-20250321185631-1f6e0b77f77e (latest)
- Published
- Mar 21, 2025
- Platform
- linux/amd64
- Imports
- 5 packages
- Last checked
- 1 month ago –
Tools for package owners.