simple_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. "path/filepath"
  10. "testing"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/config"
  13. "github.com/syncthing/syncthing/lib/fs"
  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: fs.FilesystemTypeBasic,
  50. Path: dir,
  51. Versioning: config.VersioningConfiguration{
  52. Params: map[string]string{
  53. "keep": "2",
  54. },
  55. },
  56. }
  57. fs := cfg.Filesystem(nil)
  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(".stversions")
  70. if err != nil {
  71. t.Error(err)
  72. }
  73. if float64(len(n)) != math.Min(float64(i), 2) {
  74. t.Error("Wrong count")
  75. }
  76. time.Sleep(time.Second)
  77. }
  78. }