1
0

simple_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "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, err := os.MkdirTemp("", "")
  49. //defer os.RemoveAll(dir)
  50. if err != nil {
  51. t.Error(err)
  52. }
  53. cfg := config.FolderConfiguration{
  54. FilesystemType: fs.FilesystemTypeBasic,
  55. Path: dir,
  56. Versioning: config.VersioningConfiguration{
  57. Params: map[string]string{
  58. "keep": "2",
  59. },
  60. },
  61. }
  62. fs := cfg.Filesystem(nil)
  63. v := newSimple(cfg)
  64. path := "test"
  65. for i := 1; i <= 3; i++ {
  66. f, err := fs.Create(path)
  67. if err != nil {
  68. t.Error(err)
  69. }
  70. f.Close()
  71. if err := v.Archive(path); err != nil {
  72. t.Error(err)
  73. }
  74. n, err := fs.DirNames(".stversions")
  75. if err != nil {
  76. t.Error(err)
  77. }
  78. if float64(len(n)) != math.Min(float64(i), 2) {
  79. t.Error("Wrong count")
  80. }
  81. time.Sleep(time.Second)
  82. }
  83. }