1
16
17 package path
18
19 import (
20 "os"
21 "path/filepath"
22 "reflect"
23 "sort"
24 "testing"
25 )
26
27 func TestFileUtils(t *testing.T) {
28
29 tmpDir, err := os.MkdirTemp(os.TempDir(), "util_file_test_")
30 if err != nil {
31 t.Fatal("Failed to test: failed to create temp dir.")
32 }
33
34
35 tmpFile, err := os.CreateTemp(tmpDir, "test_file_exists_")
36 if err != nil {
37 t.Fatal("Failed to test: failed to create temp file.")
38 }
39
40
41 tmpSymlinkName := filepath.Join(tmpDir, "test_file_exists_sym_link")
42 err = os.Symlink(tmpFile.Name(), tmpSymlinkName)
43 if err != nil {
44 t.Fatal("Failed to test: failed to create sym link.")
45 }
46
47
48 tmpSubDir, err := os.MkdirTemp(tmpDir, "sub_")
49 if err != nil {
50 t.Fatal("Failed to test: failed to create temp sub dir.")
51 }
52
53
54 currentDir, err := os.Getwd()
55 if err != nil {
56 t.Fatal("Failed to test: failed to get current dir.")
57 }
58
59
60 err = os.Chdir(tmpDir)
61 if err != nil {
62 t.Fatal("Failed to test: failed to change work dir.")
63 }
64
65
66 defer func() {
67 os.Chdir(currentDir)
68 os.RemoveAll(tmpDir)
69 }()
70
71 t.Run("TestExists", func(t *testing.T) {
72 tests := []struct {
73 name string
74 fileName string
75 expectedError bool
76 expectedValue bool
77 }{
78 {"file_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), false, false},
79 {"file_exists", tmpFile.Name(), false, true},
80 }
81
82 for _, test := range tests {
83 realValued, realError := Exists(CheckFollowSymlink, test.fileName)
84 if test.expectedError {
85 if realError == nil {
86 t.Fatalf("Expected error, got none, failed to test with '%s': %s", test.fileName, test.name)
87 }
88 } else if test.expectedValue != realValued {
89 t.Fatalf("Expected %#v==%#v, failed to test with '%s': %s", test.expectedValue, realValued, test.fileName, test.name)
90 }
91 }
92 })
93
94 t.Run("TestFileOrSymlinkExists", func(t *testing.T) {
95 tests := []struct {
96 name string
97 fileName string
98 expectedError bool
99 expectedValue bool
100 }{
101 {"file_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), false, false},
102 {"file_exists", tmpFile.Name(), false, true},
103 {"symlink_exists", tmpSymlinkName, false, true},
104 }
105
106 for _, test := range tests {
107 realValued, realError := Exists(CheckSymlinkOnly, test.fileName)
108 if test.expectedError {
109 if realError == nil {
110 t.Fatalf("Expected error, got none, failed to test with '%s': %s", test.fileName, test.name)
111 }
112 } else if test.expectedValue != realValued {
113 t.Fatalf("Expected %#v==%#v, failed to test with '%s': %s", test.expectedValue, realValued, test.fileName, test.name)
114 }
115 }
116 })
117
118 t.Run("TestReadDirNoStat", func(t *testing.T) {
119 _, tmpFileSimpleName := filepath.Split(tmpFile.Name())
120 _, tmpSymlinkSimpleName := filepath.Split(tmpSymlinkName)
121 _, tmpSubDirSimpleName := filepath.Split(tmpSubDir)
122
123 tests := []struct {
124 name string
125 dirName string
126 expectedError bool
127 expectedValue []string
128 }{
129 {"dir_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), true, []string{}},
130 {"dir_is_empty", "", false, []string{tmpFileSimpleName, tmpSymlinkSimpleName, tmpSubDirSimpleName}},
131 {"dir_exists", tmpDir, false, []string{tmpFileSimpleName, tmpSymlinkSimpleName, tmpSubDirSimpleName}},
132 }
133
134 for _, test := range tests {
135 realValued, realError := ReadDirNoStat(test.dirName)
136
137
138 sort.Strings(realValued)
139 sort.Strings(test.expectedValue)
140
141 if test.expectedError {
142 if realError == nil {
143 t.Fatalf("Expected error, got none, failed to test with '%s': %s", test.dirName, test.name)
144 }
145 } else if !reflect.DeepEqual(test.expectedValue, realValued) {
146 t.Fatalf("Expected %#v==%#v, failed to test with '%s': %s", test.expectedValue, realValued, test.dirName, test.name)
147 }
148 }
149 })
150 }
151
View as plain text