simple_test.go 3.6 KB

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