file_test.go 1.9 KB

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