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