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
|
package atomictemp
import (
"errors"
"io"
"os"
"path/filepath"
"testing"
)
func TestCreate(t *testing.T) {
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Error(err)
}
// test good file
goodTests := []string { "foo", "bar", "baz" }
for _, test := range(goodTests) {
t.Run(test, func(t *testing.T) {
// build destination path
path := filepath.Join(dir, test)
// create temp file
err := Create(path, func(f io.Writer) error {
_, err := f.Write([]byte(test))
return err
})
if err != nil {
t.Error(err)
return
}
if got, err := os.ReadFile(path); err != nil {
t.Error(err)
} else if string(got) != test {
t.Errorf("got \"%s\", exp \"%s\"", string(got), test)
}
})
}
t.Run("badDir", func(t *testing.T) {
// build nonsense path to destination file
badPath := filepath.Join(dir, "does/not/exist")
err := Create(badPath, func(_ io.Writer) error {
return nil
})
if err == nil {
t.Errorf("got success, exp error")
}
})
t.Run("badFunc", func(t *testing.T) {
// build path
path := filepath.Join(dir, "badFunc")
err := Create(path, func(_ io.Writer) error {
return errors.New("ack!")
})
if err == nil {
t.Errorf("got success, exp error")
}
})
}
|