simple_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "io/ioutil"
  9. "math"
  10. "path/filepath"
  11. "testing"
  12. "time"
  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, err := ioutil.TempDir("", "")
  48. //defer os.RemoveAll(dir)
  49. if err != nil {
  50. t.Error(err)
  51. }
  52. fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
  53. v := newSimple(fs, map[string]string{"keep": "2"})
  54. path := "test"
  55. for i := 1; i <= 3; i++ {
  56. f, err := fs.Create(path)
  57. if err != nil {
  58. t.Error(err)
  59. }
  60. f.Close()
  61. if err := v.Archive(path); err != nil {
  62. t.Error(err)
  63. }
  64. n, err := fs.DirNames(".stversions")
  65. if err != nil {
  66. t.Error(err)
  67. }
  68. if float64(len(n)) != math.Min(float64(i), 2) {
  69. t.Error("Wrong count")
  70. }
  71. time.Sleep(time.Second)
  72. }
  73. }