File: smb2_fs_test.go

package info (click to toggle)
golang-github-cloudsoda-go-smb2 0.0~git20231124.f3ec8ae-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 972 kB
  • sloc: makefile: 2
file content (138 lines) | stat: -rw-r--r-- 2,607 bytes parent folder | download
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//go:build go1.16
// +build go1.16

package smb2_test

import (
	"fmt"
	iofs "io/fs"
	"os"
	"path"
	"reflect"
	"testing"
)

func TestDirFS(t *testing.T) {
	if fs == nil {
		t.Skip()
	}

	testDir := fmt.Sprintf("testDir-%d-TestDirFS", os.Getpid())
	err := fs.Mkdir(testDir, 0755)
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		_ = fs.RemoveAll(testDir)
	}()

	err = fs.WriteFile(path.Join(testDir, "hello.txt"), []byte("hello world!"), 0666)
	if err != nil {
		t.Fatal(err)
	}
	err = fs.Mkdir(path.Join(testDir, "hello"), 0755)
	if err != nil {
		t.Fatal(err)
	}
	err = fs.WriteFile(path.Join(testDir, "hello", "hello2.txt"), []byte("hello world!"), 0444)
	if err != nil {
		t.Fatal(err)
	}

	{
		var entries []string

		_ = iofs.WalkDir(fs.DirFS(testDir), ".", func(path string, d iofs.DirEntry, err error) error {
			if err != nil {
				t.Fatal(err)
			}

			entries = append(entries, path)

			return nil
		})

		if !reflect.DeepEqual(entries, []string{".", "hello", "hello/hello2.txt", "hello.txt"}) {
			t.Error("unexpected result")
		}
	}

	{
		var entries []string

		_ = iofs.WalkDir(fs.DirFS(testDir), "hello", func(path string, d iofs.DirEntry, err error) error {
			if err != nil {
				t.Fatal(err)
			}

			entries = append(entries, path)

			return nil
		})

		if !reflect.DeepEqual(entries, []string{"hello", "hello/hello2.txt"}) {
			t.Error("unexpected result")
		}
	}
}

func TestGlobFS(t *testing.T) {
	if fs == nil {
		t.Skip()
	}

	testDir := fmt.Sprintf("testDir-%d-TestGlobFS", os.Getpid())
	err := fs.Mkdir(testDir, 0755)
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		_ = fs.RemoveAll(testDir)
	}()

	err = fs.WriteFile(path.Join(testDir, "hello.txt"), []byte("hello world!"), 0666)
	if err != nil {
		t.Fatal(err)
	}
	err = fs.Mkdir(path.Join(testDir, "hello"), 0755)
	if err != nil {
		t.Fatal(err)
	}
	err = fs.WriteFile(path.Join(testDir, "hello", "hello2.txt"), []byte("hello world!"), 0444)
	if err != nil {
		t.Fatal(err)
	}

	cases := []struct {
		pattern  string
		expected []string
	}{
		{
			pattern:  "hello.txt",
			expected: []string{"hello.txt"},
		},
		{
			pattern:  "hel?o.txt",
			expected: []string{"hello.txt"},
		},
		{
			pattern:  "*",
			expected: []string{"hello", "hello.txt"},
		},
		{
			pattern:  "*/*",
			expected: []string{`hello\hello2.txt`},
		},
	}

	for _, tt := range cases {
		matches, err := iofs.Glob(fs.DirFS(testDir), tt.pattern)
		if err != nil {
			t.Fatal(err)
		}
		if !reflect.DeepEqual(matches, tt.expected) {
			t.Errorf("Glob(%q) = %q, want %q", tt.pattern, matches, tt.expected)
			t.Error("unexpected result")
		}
	}
}