github.com/openimsdk/tools@v0.0.49/field/file_test.go (about) 1 // Copyright © 2024 OpenIM open source community. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package field 16 17 import ( 18 "os" 19 "path/filepath" 20 "reflect" 21 "sort" 22 "testing" 23 24 "github.com/openimsdk/tools/errs" 25 ) 26 27 func TestFileUtils(t *testing.T) { 28 // Create tmp dir 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 // create tmp file 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 // create tmp sym link 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 // create tmp sub dir 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 // record the current dir 54 currentDir, err := os.Getwd() 55 if err != nil { 56 t.Fatal("Failed to test: failed to get current dir.") 57 } 58 59 // change the work dir to temp dir 60 err = os.Chdir(tmpDir) 61 if err != nil { 62 t.Fatal("Failed to test: failed to change work dir.") 63 } 64 65 // recover test environment 66 defer func() { 67 if err := os.Chdir(currentDir); err != nil { 68 errs.ErrArgs.WrapMsg(err.Error()) 69 } 70 if err := os.RemoveAll(tmpDir); err != nil { 71 errs.ErrArgs.WrapMsg(err.Error()) 72 } 73 }() 74 75 t.Run("TestExists", func(t *testing.T) { 76 tests := []struct { 77 name string 78 fileName string 79 expectedError bool 80 expectedValue bool 81 }{ 82 {"file_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), false, false}, 83 {"file_exists", tmpFile.Name(), false, true}, 84 } 85 86 for _, test := range tests { 87 realValued, realError := Exists(CheckFollowSymlink, test.fileName) 88 if test.expectedError { 89 if realError == nil { 90 t.Fatalf("Expected error, got none, failed to test with '%s': %s", test.fileName, test.name) 91 } 92 } else if test.expectedValue != realValued { 93 t.Fatalf("Expected %#v==%#v, failed to test with '%s': %s", test.expectedValue, realValued, test.fileName, test.name) 94 } 95 } 96 }) 97 98 t.Run("TestFileOrSymlinkExists", func(t *testing.T) { 99 tests := []struct { 100 name string 101 fileName string 102 expectedError bool 103 expectedValue bool 104 }{ 105 {"file_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), false, false}, 106 {"file_exists", tmpFile.Name(), false, true}, 107 {"symlink_exists", tmpSymlinkName, false, true}, 108 } 109 110 for _, test := range tests { 111 realValued, realError := Exists(CheckSymlinkOnly, test.fileName) 112 if test.expectedError { 113 if realError == nil { 114 t.Fatalf("Expected error, got none, failed to test with '%s': %s", test.fileName, test.name) 115 } 116 } else if test.expectedValue != realValued { 117 t.Fatalf("Expected %#v==%#v, failed to test with '%s': %s", test.expectedValue, realValued, test.fileName, test.name) 118 } 119 } 120 }) 121 122 t.Run("TestReadDirNoStat", func(t *testing.T) { 123 _, tmpFileSimpleName := filepath.Split(tmpFile.Name()) 124 _, tmpSymlinkSimpleName := filepath.Split(tmpSymlinkName) 125 _, tmpSubDirSimpleName := filepath.Split(tmpSubDir) 126 127 tests := []struct { 128 name string 129 dirName string 130 expectedError bool 131 expectedValue []string 132 }{ 133 {"dir_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), true, []string{}}, 134 {"dir_is_empty", "", false, []string{tmpFileSimpleName, tmpSymlinkSimpleName, tmpSubDirSimpleName}}, 135 {"dir_exists", tmpDir, false, []string{tmpFileSimpleName, tmpSymlinkSimpleName, tmpSubDirSimpleName}}, 136 } 137 138 for _, test := range tests { 139 realValued, realError := ReadDirNoStat(test.dirName) 140 141 // execute sort action before compare 142 sort.Strings(realValued) 143 sort.Strings(test.expectedValue) 144 145 if test.expectedError { 146 if realError == nil { 147 t.Fatalf("Expected error, got none, failed to test with '%s': %s", test.dirName, test.name) 148 } 149 } else if !reflect.DeepEqual(test.expectedValue, realValued) { 150 t.Fatalf("Expected %#v==%#v, failed to test with '%s': %s", test.expectedValue, realValued, test.dirName, test.name) 151 } 152 } 153 }) 154 }