Debian Codegraph @

then/io.go

raw
1 package then
2
3 import (
4 "io"
5 "os"
6 "path/filepath"
7 "testing"
8 )
9
10 type Saver interface {
11 Save() error
12 }
13
14 // WithTempDir creates a temp directory and changes into it for a test.
15 func WithTempDir(t *testing.T) {
16 t.Helper()
17
18 tempDir := t.TempDir()
19 t.Log("creating tempdir:", tempDir)
20 t.Chdir(tempDir)
21 }
22
23 // WithTempDirConfig creates a temp directory and changes into it for a test.
24 // This will also save a config file for use in the test.
25 func WithTempDirConfig(t *testing.T, cfg Saver) {
26 t.Helper()
27 WithTempDir(t)
28
29 err := cfg.Save()
30 Nil(t, err)
31 }
32
33 func CreateFile(t *testing.T, paths ...string) {
34 WriteFile(t, nil, paths...)
35 }
36
37 // FileExists checks whether a file exists at the path defined.
38 // Paths will be joined together using `filepath.Join`.
39 func FileExists(t *testing.T, paths ...string) {
40 t.Helper()
41
42 fullPath := filepath.Join(paths...)
43
44 _, err := os.Stat(fullPath)
45 if err == nil {
46 return
47 }
48
49 switch {
50 case os.IsNotExist(err):
51 t.Logf("expected file '%v' to exist", fullPath)
52 t.FailNow()
53 default:
54 t.Logf("error getting info for '%v': %v", fullPath, err)
55 t.FailNow()
56 }
57 }
58
59 // FileNotExists checks whether a file does not exist at the path defined.
60 // Paths will be joined together using `filepath.Join`.
61 func FileNotExists(t *testing.T, paths ...string) {
62 t.Helper()
63
64 fullPath := filepath.Join(paths...)
65
66 _, err := os.Stat(fullPath)
67 if err == nil {
68 t.Logf("expected file '%v' not to exist", fullPath)
69 t.FailNow()
70 }
71
72 if !os.IsNotExist(err) {
73 t.Logf("expected to get is note exist error at '%v': %v", fullPath, err)
74 t.FailNow()
75 }
76 }
77
78 // FileContents will check the contents of a file.
79 func FileContents(t *testing.T, contents string, paths ...string) {
80 t.Helper()
81
82 fullPath := filepath.Join(paths...)
83
84 bs, err := os.ReadFile(fullPath)
85 if err != nil {
86 t.Logf("reading file: '%v'", fullPath)
87 t.FailNow()
88 }
89
90 expected := string(bs)
91 Equals(t, expected, contents)
92 }
93
94 func WriteFile(t *testing.T, data []byte, paths ...string) {
95 t.Helper()
96
97 fullPath := filepath.Join(paths...)
98 t.Log("writing file to", fullPath)
99
100 err := os.MkdirAll(filepath.Dir(fullPath), 0777)
101 Nil(t, err)
102
103 f, err := os.Create(fullPath)
104 Nil(t, err)
105
106 defer f.Close()
107
108 if len(data) > 0 {
109 _, err = f.Write(data)
110 Nil(t, err)
111 }
112 }
113
114 func WriteFileTo(t *testing.T, w io.WriterTo, paths ...string) {
115 t.Helper()
116
117 cd, _ := os.Getwd()
118 t.Log("cd", cd)
119
120 fullPath := filepath.Join(paths...)
121 err := os.MkdirAll(filepath.Dir(fullPath), 0777)
122 Nil(t, err)
123
124 f, err := os.Create(fullPath)
125 Nil(t, err)
126
127 defer f.Close()
128
129 _, err = w.WriteTo(f)
130 Nil(t, err)
131 }
132
133 func DirectoryFileCount(t *testing.T, count int, paths ...string) {
134 t.Helper()
135
136 infos, err := os.ReadDir(filepath.Join(paths...))
137 Nil(t, err)
138
139 if count != len(infos) {
140 t.Logf("expected %v files but found %v", count, len(infos))
141
142 for _, fi := range infos {
143 t.Log("file:", fi.Name())
144 }
145
146 t.FailNow()
147 }
148 }