/versions/versions_test.go

https://gitlab.com/JamesClonk/s3-resource · Go · 156 lines · 126 code · 30 blank · 0 comment · 0 complexity · e5b2b45202acdf90d01100a231de9a31 MD5 · raw file

  1. package versions_test
  2. import (
  3. "github.com/concourse/s3-resource/versions"
  4. . "github.com/onsi/ginkgo"
  5. . "github.com/onsi/gomega"
  6. )
  7. type MatchFunc func(paths []string, pattern string) ([]string, error)
  8. var ItMatchesPaths = func(matchFunc MatchFunc) {
  9. Describe("checking if paths in the bucket should be searched", func() {
  10. Context("when given an empty list of paths", func() {
  11. It("returns an empty list of matches", func() {
  12. result, err := versions.Match([]string{}, "regex")
  13. Ω(err).ShouldNot(HaveOccurred())
  14. Ω(result).Should(BeEmpty())
  15. })
  16. })
  17. Context("when given a single path", func() {
  18. It("returns it in a singleton list if it matches the regex", func() {
  19. paths := []string{"abc"}
  20. regex := "abc"
  21. result, err := versions.Match(paths, regex)
  22. Ω(err).ShouldNot(HaveOccurred())
  23. Ω(result).Should(ConsistOf("abc"))
  24. })
  25. It("returns an empty list if it does not match the regexp", func() {
  26. paths := []string{"abc"}
  27. regex := "ad"
  28. result, err := versions.Match(paths, regex)
  29. Ω(err).ShouldNot(HaveOccurred())
  30. Ω(result).Should(BeEmpty())
  31. })
  32. It("accepts full regexes", func() {
  33. paths := []string{"abc"}
  34. regex := "a.*c"
  35. result, err := versions.Match(paths, regex)
  36. Ω(err).ShouldNot(HaveOccurred())
  37. Ω(result).Should(ConsistOf("abc"))
  38. })
  39. It("errors when the regex is bad", func() {
  40. paths := []string{"abc"}
  41. regex := "a(c"
  42. _, err := versions.Match(paths, regex)
  43. Ω(err).Should(HaveOccurred())
  44. })
  45. })
  46. Context("when given a multiple paths", func() {
  47. It("returns the matches", func() {
  48. paths := []string{"abc", "bcd"}
  49. regex := ".*bc.*"
  50. result, err := versions.Match(paths, regex)
  51. Ω(err).ShouldNot(HaveOccurred())
  52. Ω(result).Should(ConsistOf("abc", "bcd"))
  53. })
  54. It("returns an empty list if none match the regexp", func() {
  55. paths := []string{"abc", "def"}
  56. regex := "ge.*h"
  57. result, err := versions.Match(paths, regex)
  58. Ω(err).ShouldNot(HaveOccurred())
  59. Ω(result).Should(BeEmpty())
  60. })
  61. })
  62. })
  63. }
  64. var _ = Describe("Match", func() {
  65. Describe("Match", func() {
  66. ItMatchesPaths(versions.Match)
  67. It("does not contain files that are in some subdirectory that is not explicitly mentioned", func() {
  68. paths := []string{"folder/abc", "abc"}
  69. regex := "abc"
  70. result, err := versions.Match(paths, regex)
  71. Ω(err).ShouldNot(HaveOccurred())
  72. Ω(result).Should(ConsistOf("abc"))
  73. })
  74. })
  75. Describe("MatchUnanchored", func() {
  76. ItMatchesPaths(versions.MatchUnanchored)
  77. })
  78. })
  79. var _ = Describe("PrefixHint", func() {
  80. It("turns a regexp into a limiter for s3", func() {
  81. Ω(versions.PrefixHint("hello/world")).Should(Equal("hello/world"))
  82. Ω(versions.PrefixHint("hello/*.tgz")).Should(Equal("hello"))
  83. Ω(versions.PrefixHint("")).Should(Equal(""))
  84. Ω(versions.PrefixHint("*")).Should(Equal(""))
  85. Ω(versions.PrefixHint("hello/*/what.txt")).Should(Equal("hello"))
  86. })
  87. })
  88. var _ = Describe("Extract", func() {
  89. Context("when the path does not contain extractable information", func() {
  90. It("doesn't extract it", func() {
  91. result, ok := versions.Extract("abc.tgz", "abc-(.*).tgz")
  92. Ω(ok).Should(BeFalse())
  93. Ω(result).Should(BeZero())
  94. })
  95. })
  96. Context("when the path contains extractable information", func() {
  97. It("extracts it", func() {
  98. result, ok := versions.Extract("abc-105.tgz", "abc-(.*).tgz")
  99. Ω(ok).Should(BeTrue())
  100. Ω(result.Path).Should(Equal("abc-105.tgz"))
  101. Ω(result.Version.String()).Should(Equal("105.0.0"))
  102. Ω(result.VersionNumber).Should(Equal("105"))
  103. })
  104. It("extracts semantics version numbers", func() {
  105. result, ok := versions.Extract("abc-1.0.5.tgz", "abc-(.*).tgz")
  106. Ω(ok).Should(BeTrue())
  107. Ω(result.Path).Should(Equal("abc-1.0.5.tgz"))
  108. Ω(result.Version.String()).Should(Equal("1.0.5"))
  109. Ω(result.VersionNumber).Should(Equal("1.0.5"))
  110. })
  111. It("takes the first match if there are many", func() {
  112. result, ok := versions.Extract("abc-1.0.5-def-2.3.4.tgz", "abc-(.*)-def-(.*).tgz")
  113. Ω(ok).Should(BeTrue())
  114. Ω(result.Path).Should(Equal("abc-1.0.5-def-2.3.4.tgz"))
  115. Ω(result.Version.String()).Should(Equal("1.0.5"))
  116. Ω(result.VersionNumber).Should(Equal("1.0.5"))
  117. })
  118. It("extracts a named group called 'version' above all others", func() {
  119. result, ok := versions.Extract("abc-1.0.5-def-2.3.4.tgz", "abc-(.*)-def-(?P<version>.*).tgz")
  120. Ω(ok).Should(BeTrue())
  121. Ω(result.Path).Should(Equal("abc-1.0.5-def-2.3.4.tgz"))
  122. Ω(result.Version.String()).Should(Equal("2.3.4"))
  123. Ω(result.VersionNumber).Should(Equal("2.3.4"))
  124. })
  125. })
  126. })