simple_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (C) 2014 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. package versioner
  7. import (
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/syncthing/syncthing/lib/config"
  14. )
  15. func TestTaggedFilename(t *testing.T) {
  16. cases := [][3]string{
  17. {filepath.Join("foo", "bar.baz"), "tag", filepath.Join("foo", "bar~tag.baz")},
  18. {"bar.baz", "tag", "bar~tag.baz"},
  19. {"bar", "tag", "bar~tag"},
  20. {"~$ufheft2.docx", "20140612-200554", "~$ufheft2~20140612-200554.docx"},
  21. {"alle~4.mgz", "20141106-094415", "alle~4~20141106-094415.mgz"},
  22. // Parsing test only
  23. {"", "tag-only", "foo/bar.baz~tag-only"},
  24. {"", "tag-only", "bar.baz~tag-only"},
  25. {"", "20140612-200554", "~$ufheft2.docx~20140612-200554"},
  26. {"", "20141106-094415", "alle~4.mgz~20141106-094415"},
  27. }
  28. for _, tc := range cases {
  29. if tc[0] != "" {
  30. // Test tagger
  31. tf := TagFilename(tc[0], tc[1])
  32. if tf != tc[2] {
  33. t.Errorf("%s != %s", tf, tc[2])
  34. }
  35. }
  36. // Test parser
  37. tag := extractTag(tc[2])
  38. if tag != tc[1] {
  39. t.Errorf("%s != %s", tag, tc[1])
  40. }
  41. }
  42. }
  43. func TestSimpleVersioningVersionCount(t *testing.T) {
  44. if testing.Short() {
  45. t.Skip("Test takes some time, skipping.")
  46. }
  47. dir := t.TempDir()
  48. cfg := config.FolderConfiguration{
  49. FilesystemType: config.FilesystemTypeBasic,
  50. Path: dir,
  51. Versioning: config.VersioningConfiguration{
  52. Params: map[string]string{
  53. "keep": "2",
  54. },
  55. },
  56. }
  57. fs := cfg.Filesystem()
  58. v := newSimple(cfg)
  59. path := "test"
  60. for i := 1; i <= 3; i++ {
  61. f, err := fs.Create(path)
  62. if err != nil {
  63. t.Error(err)
  64. }
  65. f.Close()
  66. if err := v.Archive(path); err != nil {
  67. t.Error(err)
  68. }
  69. n, err := fs.DirNames(DefaultPath)
  70. if err != nil {
  71. t.Error(err)
  72. }
  73. if len(n) != min(i, 2) {
  74. t.Error("Wrong count")
  75. }
  76. time.Sleep(time.Second)
  77. }
  78. }
  79. func TestPathTildes(t *testing.T) {
  80. // Test that folder and version paths with leading tildes are expanded
  81. // to the user's home directory. (issue #9241)
  82. home := t.TempDir()
  83. t.Setenv("HOME", home)
  84. if vn := filepath.VolumeName(home); vn != "" {
  85. // Legacy Windows home stuff
  86. t.Setenv("HomeDrive", vn)
  87. t.Setenv("HomePath", strings.TrimPrefix(home, vn))
  88. }
  89. os.Mkdir(filepath.Join(home, "folder"), 0o755)
  90. cfg := config.FolderConfiguration{
  91. FilesystemType: config.FilesystemTypeBasic,
  92. Path: "~/folder",
  93. Versioning: config.VersioningConfiguration{
  94. FSPath: "~/versions",
  95. FSType: config.FilesystemTypeBasic,
  96. Params: map[string]string{
  97. "keep": "2",
  98. },
  99. },
  100. }
  101. fs := cfg.Filesystem()
  102. v := newSimple(cfg)
  103. const testPath = "test"
  104. f, err := fs.Create(testPath)
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. f.Close()
  109. if err := v.Archive(testPath); err != nil {
  110. t.Fatal(err)
  111. }
  112. // Check that there are no entries in the folder directory; this is
  113. // specifically to check that there is no directory named "~" there.
  114. names, err := fs.DirNames(".")
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. if len(names) != 0 {
  119. t.Fatalf("found %d files in folder dir, want 0", len(names))
  120. }
  121. // Check that the versions directory contains one file that begins with
  122. // our test path.
  123. des, err := os.ReadDir(filepath.Join(home, "versions"))
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. for _, de := range des {
  128. names = append(names, de.Name())
  129. }
  130. if len(names) != 1 {
  131. t.Fatalf("found %d files in versions dir, want 1", len(names))
  132. }
  133. if got := names[0]; !strings.HasPrefix(got, testPath) {
  134. t.Fatalf("found versioned file %q, want one that begins with %q", got, testPath)
  135. }
  136. }