Debian Codegraph @

debian/go/src/golang.org/x/telemetry/internal/unionfs/unionfs_test.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package unionfs
6
7 import (
8 "io"
9 "os"
10 "reflect"
11 "testing"
12 )
13
14 func TestFS_Open(t *testing.T) {
15 fsys, err := Sub(os.DirFS("testdata"), "dir1", "dir2")
16 if err != nil {
17 t.Fatal(err)
18 }
19
20 type args struct {
21 name string
22 }
23 tests := []struct {
24 name string
25 args args
26 want string
27 wantErr bool
28 }{
29 {
30 name: "file1 from dir1",
31 args: args{
32 name: "file1",
33 },
34 want: "file 1 content from dir 1\n",
35 wantErr: false,
36 },
37 {
38 name: "file2 from dir2",
39 args: args{
40 name: "file2",
41 },
42 want: "file 2 content\n",
43 wantErr: false,
44 },
45 {
46 name: "file not found",
47 args: args{
48 name: "file3",
49 },
50 want: "file3",
51 wantErr: true,
52 },
53 }
54 for _, tt := range tests {
55 t.Run(tt.name, func(t *testing.T) {
56 file, err := fsys.Open(tt.args.name)
57 if (err != nil) != tt.wantErr {
58 t.Errorf("FS.Open() error = %v, wantErr %v", err, tt.wantErr)
59 return
60 }
61 if !tt.wantErr {
62 bytes, err := io.ReadAll(file)
63 if err != nil {
64 t.Fatal(err)
65 }
66 got := string(bytes)
67 if got != tt.want {
68 t.Errorf("FS.Open() = %v, want %v", got, tt.want)
69 }
70 }
71 })
72 }
73 }
74
75 func TestFS_ReadDir(t *testing.T) {
76 var err error
77 fsys, err := Sub(os.DirFS("testdata"), "dir1", "dir2")
78 if err != nil {
79 t.Fatal(err)
80 }
81 type args struct {
82 name string
83 }
84 tests := []struct {
85 name string
86 args args
87 fsys FS
88 wantFiles []string
89 }{
90 {
91 name: "",
92 args: args{"."},
93 fsys: fsys,
94 wantFiles: []string{"file1", "file2"},
95 },
96 }
97 for _, tt := range tests {
98 t.Run(tt.name, func(t *testing.T) {
99 dirs, err := tt.fsys.ReadDir(tt.args.name)
100 if err != nil {
101 t.Errorf("FS.ReadDir() error = %v", err)
102 return
103 }
104 var got []string
105 for _, v := range dirs {
106 got = append(got, v.Name())
107 }
108 if !reflect.DeepEqual(got, tt.wantFiles) {
109 t.Errorf("FS.ReadDir() = %v, want %v", got, tt.wantFiles)
110 }
111 })
112 }
113 }