github.com/kubernetes/utils@v0.0.0-20190308190857-21c4ce38f2a7/path/file_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package path 18 19 import ( 20 "os" 21 "path/filepath" 22 "sort" 23 "testing" 24 25 "github.com/spf13/afero" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func TestFileUtils(t *testing.T) { 30 fs := &afero.Afero{Fs: afero.NewOsFs()} 31 // Create tmp dir 32 tmpDir, err := fs.TempDir(os.TempDir(), "util_file_test_") 33 if err != nil { 34 t.Fatal("Failed to test: failed to create temp dir.") 35 } 36 37 // create tmp file 38 tmpFile, err := fs.TempFile(tmpDir, "test_file_exists_") 39 if err != nil { 40 t.Fatal("Failed to test: failed to create temp file.") 41 } 42 43 // create tmp sym link 44 tmpSymlinkName := filepath.Join(tmpDir, "test_file_exists_sym_link") 45 err = os.Symlink(tmpFile.Name(), tmpSymlinkName) 46 if err != nil { 47 t.Fatal("Failed to test: failed to create sym link.") 48 } 49 50 // create tmp sub dir 51 tmpSubDir, err := fs.TempDir(tmpDir, "sub_") 52 if err != nil { 53 t.Fatal("Failed to test: failed to create temp sub dir.") 54 } 55 56 // record the current dir 57 currentDir, err := os.Getwd() 58 if err != nil { 59 t.Fatal("Failed to test: failed to get current dir.") 60 } 61 62 // change the work dir to temp dir 63 err = os.Chdir(tmpDir) 64 if err != nil { 65 t.Fatal("Failed to test: failed to change work dir.") 66 } 67 68 // recover test environment 69 defer func() { 70 os.Chdir(currentDir) 71 os.RemoveAll(tmpDir) 72 }() 73 74 t.Run("TestExists", func(t *testing.T) { 75 tests := []struct { 76 name string 77 fileName string 78 expectedError bool 79 expectedValue bool 80 }{ 81 {"file_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), false, false}, 82 {"file_exists", tmpFile.Name(), false, true}, 83 } 84 85 for _, test := range tests { 86 realValued, realError := Exists(CheckFollowSymlink, test.fileName) 87 if test.expectedError { 88 assert.Errorf(t, realError, "Failed to test with '%s': %s", test.fileName, test.name) 89 } else { 90 assert.EqualValuesf(t, test.expectedValue, realValued, "Failed to test with '%s': %s", test.fileName, test.name) 91 } 92 } 93 }) 94 95 t.Run("TestFileOrSymlinkExists", func(t *testing.T) { 96 tests := []struct { 97 name string 98 fileName string 99 expectedError bool 100 expectedValue bool 101 }{ 102 {"file_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), false, false}, 103 {"file_exists", tmpFile.Name(), false, true}, 104 {"symlink_exists", tmpSymlinkName, false, true}, 105 } 106 107 for _, test := range tests { 108 realValued, realError := Exists(CheckSymlinkOnly, test.fileName) 109 if test.expectedError { 110 assert.Errorf(t, realError, "Failed to test with '%s': %s", test.fileName, test.name) 111 } else { 112 assert.EqualValuesf(t, test.expectedValue, realValued, "Failed to test with '%s': %s", test.fileName, test.name) 113 } 114 } 115 }) 116 117 t.Run("TestReadDirNoStat", func(t *testing.T) { 118 _, tmpFileSimpleName := filepath.Split(tmpFile.Name()) 119 _, tmpSymlinkSimpleName := filepath.Split(tmpSymlinkName) 120 _, tmpSubDirSimpleName := filepath.Split(tmpSubDir) 121 122 tests := []struct { 123 name string 124 dirName string 125 expectedError bool 126 expectedValue []string 127 }{ 128 {"dir_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), true, []string{}}, 129 {"dir_is_empty", "", false, []string{tmpFileSimpleName, tmpSymlinkSimpleName, tmpSubDirSimpleName}}, 130 {"dir_exists", tmpDir, false, []string{tmpFileSimpleName, tmpSymlinkSimpleName, tmpSubDirSimpleName}}, 131 } 132 133 for _, test := range tests { 134 realValued, realError := ReadDirNoStat(test.dirName) 135 136 // execute sort action before compare 137 sort.Strings(realValued) 138 sort.Strings(test.expectedValue) 139 140 if test.expectedError { 141 assert.Errorf(t, realError, "Failed to test with '%s': %s", test.dirName, test.name) 142 } else { 143 assert.EqualValuesf(t, test.expectedValue, realValued, "Failed to test with '%s': %s", test.dirName, test.name) 144 } 145 } 146 }) 147 }