simple_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 http://mozilla.org/MPL/2.0/.
  6. package versioner
  7. import (
  8. "io/ioutil"
  9. "math"
  10. "os"
  11. "path/filepath"
  12. "testing"
  13. "time"
  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 := taggedFilename(tc[0], tc[1])
  32. if tf != tc[2] {
  33. t.Errorf("%s != %s", tf, tc[2])
  34. }
  35. }
  36. // Test parser
  37. tag := filenameTag(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. v := NewSimple("", dir, map[string]string{"keep": "2"})
  53. versionDir := filepath.Join(dir, ".stversions")
  54. path := filepath.Join(dir, "test")
  55. for i := 1; i <= 3; i++ {
  56. f, err := os.Create(path)
  57. if err != nil {
  58. t.Error(err)
  59. }
  60. f.Close()
  61. v.Archive(path)
  62. d, err := os.Open(versionDir)
  63. if err != nil {
  64. t.Error(err)
  65. }
  66. n, err := d.Readdirnames(-1)
  67. if err != nil {
  68. t.Error(err)
  69. }
  70. if float64(len(n)) != math.Min(float64(i), 2) {
  71. t.Error("Wrong count")
  72. }
  73. d.Close()
  74. time.Sleep(time.Second)
  75. }
  76. }