file_test.go (1995B)
1 package dati 2 3 /* 4 Copyright (C) 2023 gearsix <gearsix@tuta.io> 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <https://www.gnu.org/licenses/>. 18 */ 19 20 import ( 21 "os" 22 "path/filepath" 23 "strconv" 24 "testing" 25 "time" 26 ) 27 28 func TestSortFileList(t *testing.T) { 29 var err error 30 tdir := os.TempDir() 31 paths := []string{tdir + "/1", tdir + "/3", tdir + "/2"} 32 var sorted []string 33 34 sorted, err = SortFileList(paths, "filename") 35 if err != nil { 36 t.Fatal(err) 37 } 38 for i, p := range sorted { 39 if filepath.Base(p) != strconv.Itoa(i+1) { 40 t.Fatalf("invalid order returned sorted[%d] is %s", i, p) 41 } 42 } 43 44 sorted, err = SortFileList(paths, "filename-desc") 45 if err != nil { 46 t.Fatal(err) 47 } 48 j := 3 49 for i := 0; i < len(sorted); i++ { 50 if filepath.Base(sorted[i]) != strconv.Itoa(j) { 51 t.Fatalf("invalid order returned sorted[%d] is %s", i, sorted[i]) 52 } 53 j-- 54 } 55 56 for _, path := range paths { 57 var f *os.File 58 if f, err = os.Create(path); err != nil { 59 t.Skip(err) 60 } 61 defer f.Close() 62 time.Sleep(100 * time.Millisecond) 63 } 64 65 sorted, err = SortFileList(paths, "modified") 66 if err != nil { 67 t.Fatal(err) 68 } 69 for i := range paths { 70 if sorted[i] != paths[i] { 71 t.Fatalf("invalid order returned %s - %s", sorted, paths) 72 } 73 } 74 75 sorted, err = SortFileList(paths, "modified-desc") 76 if err != nil { 77 t.Fatal(err) 78 } 79 j = 2 80 for i := 0; i < len(paths); i++ { 81 if sorted[i] != paths[j] { 82 t.Fatalf("invalid order returned %s - %s", sorted, paths) 83 } 84 j-- 85 } 86 }