Files
ConfigKit/source_file_test.go

49 lines
1.5 KiB
Go

package configkit
import (
"io/fs"
"os"
"path/filepath"
"testing"
)
// Files disappearing between filesystem operations are not deterministic with
// real concurrent deletion. Exercise the private seam that controls optionality.
func TestRegularFileMissingFailurePhases(t *testing.T) {
name := filepath.Join(t.TempDir(), "input.yml")
if err := os.WriteFile(name, []byte("{}"), 0o600); err != nil {
t.Fatal(err)
}
for _, test := range []struct {
name string
stat func(string) (fs.FileInfo, error)
open func(string) (*os.File, error)
missing bool
}{
{"preflight", func(string) (fs.FileInfo, error) { return nil, fs.ErrNotExist }, os.Open, true},
{"open", os.Stat, func(string) (*os.File, error) { return nil, fs.ErrNotExist }, true},
{"post-open stat", os.Stat, func(name string) (*os.File, error) {
file, err := os.Open(name)
if err == nil {
err = file.Close()
}
return file, err
}, false},
} {
t.Run(test.name, func(t *testing.T) {
data, failure := readRegularFile(name, test.stat, test.open, 16)
if data != nil || failure == nil || failure.kind != sourceReadIO || failure.missingTarget != test.missing {
t.Fatalf("data = %v, failure = %#v", data, failure)
}
})
}
_, failure := readBoundedSource(missingDuringRead{}, 16)
if failure == nil || failure.kind != sourceReadIO || failure.missingTarget {
t.Fatalf("late read failure = %#v", failure)
}
}
type missingDuringRead struct{}
func (missingDuringRead) Read([]byte) (int, error) { return 0, fs.ErrNotExist }