-
Notifications
You must be signed in to change notification settings - Fork 0
/
bad_test.go
79 lines (67 loc) · 1.87 KB
/
bad_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2023 by Harald Albrecht
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package tiap
import (
"errors"
"io/fs"
)
// badYAMLValue causes the YAML marshaller to throw up.
type badYAMLValue nada
func (b badYAMLValue) MarshalYAML() (interface{}, error) { return nil, errors.New("bad YAML value") }
// badWriter only throws errors on any write attempt.
type badWriter struct{}
func (w *badWriter) Write(p []byte) (n int, err error) { return 0, errors.New("snafu") }
// fsFailureMode controls which badFS operation will fail.
type fsFailureMode int
const (
fsFailOpenDir = iota
fsFailOpen
fsFailRead
)
// badFS fails on eihter opening a directory, opening a file, or alternatively
// on reading a file (not directory).
type badFS struct {
fs.FS
fail fsFailureMode
}
func (f *badFS) Open(name string) (fs.File, error) {
fsf, err := f.FS.Open(name)
if err != nil {
return nil, err
}
stat, err := fsf.Stat()
if err != nil {
fsf.Close()
return nil, err
}
if stat.IsDir() {
if f.fail == fsFailOpenDir {
fsf.Close()
return nil, errors.New("badfs open dir error")
}
return fsf, nil
}
if f.fail == fsFailOpen {
fsf.Close()
return nil, errors.New("badfs open error")
}
return &badFile{fsf}, nil
}
// badFile throws error on reading.
type badFile struct {
fs.File
}
func (f *badFile) Read([]byte) (int, error) {
return 0, errors.New("badfile read error")
}