locations_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (C) 2023 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //go:build !windows
  7. package locations
  8. import (
  9. "path/filepath"
  10. "slices"
  11. "testing"
  12. "time"
  13. )
  14. func TestUnixConfigDir(t *testing.T) {
  15. t.Parallel()
  16. cases := []struct {
  17. userHome string
  18. xdgConfigHome string
  19. xdgStateHome string
  20. filesExist []string
  21. expected string
  22. }{
  23. // First some "new installations", no files exist previously.
  24. // No variables set, use our current default
  25. {"/home/user", "", "", nil, "/home/user/.local/state/syncthing"},
  26. // Config home set, doesn't matter
  27. {"/home/user", "/somewhere/else", "", nil, "/home/user/.local/state/syncthing"},
  28. // State home set, use that
  29. {"/home/user", "", "/var/state", nil, "/var/state/syncthing"},
  30. // State home set, again config home doesn't matter
  31. {"/home/user", "/somewhere/else", "/var/state", nil, "/var/state/syncthing"},
  32. // Now some "upgrades", where we have files in the old locations.
  33. // Config home set, a file exists in the default location
  34. {"/home/user", "/somewhere/else", "", []string{"/home/user/.config/syncthing/config.xml"}, "/home/user/.config/syncthing"},
  35. // State home set, a file exists in the default location
  36. {"/home/user", "", "/var/state", []string{"/home/user/.config/syncthing/config.xml"}, "/home/user/.config/syncthing"},
  37. // Both config home and state home set, a file exists in the default location
  38. {"/home/user", "/somewhere/else", "/var/state", []string{"/home/user/.config/syncthing/config.xml"}, "/home/user/.config/syncthing"},
  39. // Config home set, and a file exists at that place
  40. {"/home/user", "/somewhere/else", "", []string{"/somewhere/else/syncthing/config.xml"}, "/somewhere/else/syncthing"},
  41. // Config home and state home set, and a file exists in config home
  42. {"/home/user", "/somewhere/else", "/var/state", []string{"/somewhere/else/syncthing/config.xml"}, "/somewhere/else/syncthing"},
  43. }
  44. for _, c := range cases {
  45. fileExists := func(path string) bool { return slices.Contains(c.filesExist, path) }
  46. actual := unixConfigDir(c.userHome, c.xdgConfigHome, c.xdgStateHome, fileExists)
  47. if actual != c.expected {
  48. t.Errorf("unixConfigDir(%q, %q, %q) == %q, expected %q", c.userHome, c.xdgConfigHome, c.xdgStateHome, actual, c.expected)
  49. }
  50. }
  51. }
  52. func TestUnixDataDir(t *testing.T) {
  53. t.Parallel()
  54. cases := []struct {
  55. userHome string
  56. configDir string
  57. xdgDataHome string
  58. xdgStateHome string
  59. filesExist []string
  60. expected string
  61. }{
  62. // First some "new installations", no files exist previously.
  63. // No variables set, use our current default
  64. {"/home/user", "", "", "", nil, "/home/user/.local/state/syncthing"},
  65. // Data home set, doesn't matter
  66. {"/home/user", "", "/somewhere/else", "", nil, "/home/user/.local/state/syncthing"},
  67. // State home set, use that
  68. {"/home/user", "", "", "/var/state", nil, "/var/state/syncthing"},
  69. // Now some "upgrades", where we have files in the old locations.
  70. // A database exists in the old default location, use that
  71. {"/home/user", "", "", "", []string{"/home/user/.config/syncthing/index-v0.14.0.db"}, "/home/user/.config/syncthing"},
  72. {"/home/user", "/config/dir", "/xdg/data/home", "/xdg/state/home", []string{"/home/user/.config/syncthing/index-v0.14.0.db"}, "/home/user/.config/syncthing"},
  73. // A database exists in the config dir, use that
  74. {"/home/user", "/config/dir", "/xdg/data/home", "/xdg/state/home", []string{"/config/dir/index-v0.14.0.db"}, "/config/dir"},
  75. // A database exists in the old xdg data home, use that
  76. {"/home/user", "/config/dir", "/xdg/data/home", "/xdg/state/home", []string{"/xdg/data/home/syncthing/index-v0.14.0.db"}, "/xdg/data/home/syncthing"},
  77. }
  78. for _, c := range cases {
  79. fileExists := func(path string) bool { return slices.Contains(c.filesExist, path) }
  80. actual := unixDataDir(c.userHome, c.configDir, c.xdgDataHome, c.xdgStateHome, fileExists)
  81. if actual != c.expected {
  82. t.Errorf("unixDataDir(%q, %q, %q, %q) == %q, expected %q", c.userHome, c.configDir, c.xdgDataHome, c.xdgStateHome, actual, c.expected)
  83. }
  84. }
  85. }
  86. func TestGetTimestamped(t *testing.T) {
  87. s := getTimestampedAt(PanicLog, time.Date(2023, 10, 25, 21, 47, 0, 0, time.UTC))
  88. exp := "panic-20231025-214700.log"
  89. if file := filepath.Base(s); file != exp {
  90. t.Errorf("got %q, expected %q", file, exp)
  91. }
  92. }