github.com/gofunct/common@v0.0.0-20190131174352-fd058c7fbf22/pkg/path/file_test.go (about) 1 package path 2 3 import ( 4 "os" 5 "path/filepath" 6 "sort" 7 "testing" 8 9 "github.com/spf13/afero" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestFileUtils(t *testing.T) { 14 fs := &afero.Afero{Fs: afero.NewOsFs()} 15 // Create tmp dir 16 tmpDir, err := fs.TempDir(os.TempDir(), "util_file_test_") 17 if err != nil { 18 t.Fatal("Failed to test: failed to create temp dir.") 19 } 20 21 // create tmp file 22 tmpFile, err := fs.TempFile(tmpDir, "test_file_exists_") 23 if err != nil { 24 t.Fatal("Failed to test: failed to create temp file.") 25 } 26 27 // create tmp sym link 28 tmpSymlinkName := filepath.Join(tmpDir, "test_file_exists_sym_link") 29 err = os.Symlink(tmpFile.Name(), tmpSymlinkName) 30 if err != nil { 31 t.Fatal("Failed to test: failed to create sym link.") 32 } 33 34 // create tmp sub dir 35 tmpSubDir, err := fs.TempDir(tmpDir, "sub_") 36 if err != nil { 37 t.Fatal("Failed to test: failed to create temp sub dir.") 38 } 39 40 // record the current dir 41 currentDir, err := os.Getwd() 42 if err != nil { 43 t.Fatal("Failed to test: failed to get current dir.") 44 } 45 46 // change the work dir to temp dir 47 err = os.Chdir(tmpDir) 48 if err != nil { 49 t.Fatal("Failed to test: failed to change work dir.") 50 } 51 52 // recover test environment 53 defer func() { 54 os.Chdir(currentDir) 55 os.RemoveAll(tmpDir) 56 }() 57 58 t.Run("TestExists", func(t *testing.T) { 59 tests := []struct { 60 name string 61 fileName string 62 expectedError bool 63 expectedValue bool 64 }{ 65 {"file_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), false, false}, 66 {"file_exists", tmpFile.Name(), false, true}, 67 } 68 69 for _, test := range tests { 70 realValued, realError := Exists(CheckFollowSymlink, test.fileName) 71 if test.expectedError { 72 assert.Errorf(t, realError, "Failed to test with '%s': %s", test.fileName, test.name) 73 } else { 74 assert.EqualValuesf(t, test.expectedValue, realValued, "Failed to test with '%s': %s", test.fileName, test.name) 75 } 76 } 77 }) 78 79 t.Run("TestFileOrSymlinkExists", func(t *testing.T) { 80 tests := []struct { 81 name string 82 fileName string 83 expectedError bool 84 expectedValue bool 85 }{ 86 {"file_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), false, false}, 87 {"file_exists", tmpFile.Name(), false, true}, 88 {"symlink_exists", tmpSymlinkName, false, true}, 89 } 90 91 for _, test := range tests { 92 realValued, realError := Exists(CheckSymlinkOnly, test.fileName) 93 if test.expectedError { 94 assert.Errorf(t, realError, "Failed to test with '%s': %s", test.fileName, test.name) 95 } else { 96 assert.EqualValuesf(t, test.expectedValue, realValued, "Failed to test with '%s': %s", test.fileName, test.name) 97 } 98 } 99 }) 100 101 t.Run("TestReadDirNoStat", func(t *testing.T) { 102 _, tmpFileSimpleName := filepath.Split(tmpFile.Name()) 103 _, tmpSymlinkSimpleName := filepath.Split(tmpSymlinkName) 104 _, tmpSubDirSimpleName := filepath.Split(tmpSubDir) 105 106 tests := []struct { 107 name string 108 dirName string 109 expectedError bool 110 expectedValue []string 111 }{ 112 {"dir_not_exists", filepath.Join(tmpDir, "file_not_exist_case"), true, []string{}}, 113 {"dir_is_empty", "", false, []string{tmpFileSimpleName, tmpSymlinkSimpleName, tmpSubDirSimpleName}}, 114 {"dir_exists", tmpDir, false, []string{tmpFileSimpleName, tmpSymlinkSimpleName, tmpSubDirSimpleName}}, 115 } 116 117 for _, test := range tests { 118 realValued, realError := ReadDirNoStat(test.dirName) 119 120 // execute sort action before compare 121 sort.Strings(realValued) 122 sort.Strings(test.expectedValue) 123 124 if test.expectedError { 125 assert.Errorf(t, realError, "Failed to test with '%s': %s", test.dirName, test.name) 126 } else { 127 assert.EqualValuesf(t, test.expectedValue, realValued, "Failed to test with '%s': %s", test.dirName, test.name) 128 } 129 } 130 }) 131 }