-
Notifications
You must be signed in to change notification settings - Fork 10
/
file_test.go
97 lines (79 loc) · 1.71 KB
/
file_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package utils
import (
"fmt"
"io/fs"
"net/http"
"os"
"runtime"
"testing"
"github.com/stretchr/testify/require"
)
func Test_ReadFile(t *testing.T) {
t.Parallel()
testFS := http.FS(os.DirFS(".github/tests"))
file, err := ReadFile("john.txt", testFS)
switch runtime.GOOS {
case "windows":
require.Equal(t, "doe\r\n", string(file))
default:
require.Equal(t, "doe\n", string(file))
}
require.NoError(t, err)
}
func Test_Walk(t *testing.T) {
t.Parallel()
type file struct {
path string
name string
isDir bool
}
var files []file
expectedResults := []file{
{
path: "example",
name: "example",
isDir: true,
},
{
path: "example/example1.txt",
name: "example1.txt",
isDir: false,
},
{
path: "john.txt",
name: "john.txt",
isDir: false,
},
}
testFS := http.FS(os.DirFS(".github/tests"))
err := Walk(testFS, ".", func(path string, info fs.FileInfo, _ error) error {
if path != "." {
files = append(files, file{
path: path,
name: info.Name(),
isDir: info.IsDir(),
})
}
return nil
})
require.NoError(t, err)
require.Equal(t, expectedResults, files)
}
func Test_Walk_Error(t *testing.T) {
t.Parallel()
testFS := http.FS(os.DirFS(".github/tests"))
err := Walk(testFS, "nonexistent", func(path string, _ fs.FileInfo, _ error) error {
return fmt.Errorf("file not found: %s", path)
})
require.Error(t, err)
}
func Test_ReadFile_Error(t *testing.T) {
t.Parallel()
// Test error when file does not exist
testFS := http.FS(os.DirFS(".github/tests"))
_, err := ReadFile("nonexistent.txt", testFS)
require.Error(t, err)
// Test error when file does not exist and fs is nil
_, err = ReadFile("nonexistent.txt", nil)
require.Error(t, err)
}