Stable APIs for Go. https://go.code.as

refs_test.go 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. . "gopkg.in/check.v1"
  6. "sort"
  7. )
  8. var _ = Suite(&RefsSuite{})
  9. type RefsSuite struct{}
  10. type refsTest struct {
  11. summary string
  12. original string
  13. version string
  14. changed string
  15. versions []string
  16. }
  17. var refsTests = []refsTest{{
  18. "Version v0 works even without any references",
  19. reflines(
  20. "hash1 HEAD",
  21. ),
  22. "v0",
  23. reflines(
  24. "hash1 HEAD",
  25. ),
  26. nil,
  27. }, {
  28. "Preserve original capabilities",
  29. reflines(
  30. "hash1 HEAD\x00caps",
  31. ),
  32. "v0",
  33. reflines(
  34. "hash1 HEAD\x00caps",
  35. ),
  36. nil,
  37. }, {
  38. "Matching major version branch",
  39. reflines(
  40. "00000000000000000000000000000000000hash1 HEAD",
  41. "00000000000000000000000000000000000hash2 refs/heads/v0",
  42. "00000000000000000000000000000000000hash3 refs/heads/v1",
  43. "00000000000000000000000000000000000hash4 refs/heads/v2",
  44. ),
  45. "v1",
  46. reflines(
  47. "00000000000000000000000000000000000hash3 HEAD\x00symref=HEAD:refs/heads/v1",
  48. "00000000000000000000000000000000000hash3 refs/heads/master",
  49. "00000000000000000000000000000000000hash2 refs/heads/v0",
  50. "00000000000000000000000000000000000hash3 refs/heads/v1",
  51. "00000000000000000000000000000000000hash4 refs/heads/v2",
  52. ),
  53. []string{"v0", "v1", "v2"},
  54. }, {
  55. "Matching minor version branch",
  56. reflines(
  57. "00000000000000000000000000000000000hash1 HEAD",
  58. "00000000000000000000000000000000000hash2 refs/heads/v1.1",
  59. "00000000000000000000000000000000000hash3 refs/heads/v1.3",
  60. "00000000000000000000000000000000000hash4 refs/heads/v1.2",
  61. ),
  62. "v1",
  63. reflines(
  64. "00000000000000000000000000000000000hash3 HEAD\x00symref=HEAD:refs/heads/v1.3",
  65. "00000000000000000000000000000000000hash3 refs/heads/master",
  66. "00000000000000000000000000000000000hash2 refs/heads/v1.1",
  67. "00000000000000000000000000000000000hash3 refs/heads/v1.3",
  68. "00000000000000000000000000000000000hash4 refs/heads/v1.2",
  69. ),
  70. []string{"v1.1", "v1.2", "v1.3"},
  71. }, {
  72. "Disable original symref capability",
  73. reflines(
  74. "00000000000000000000000000000000000hash1 HEAD\x00foo symref=bar baz",
  75. "00000000000000000000000000000000000hash2 refs/heads/v1",
  76. ),
  77. "v1",
  78. reflines(
  79. "00000000000000000000000000000000000hash2 HEAD\x00symref=HEAD:refs/heads/v1 foo oldref=bar baz",
  80. "00000000000000000000000000000000000hash2 refs/heads/master",
  81. "00000000000000000000000000000000000hash2 refs/heads/v1",
  82. ),
  83. []string{"v1"},
  84. }, {
  85. "Replace original master branch",
  86. reflines(
  87. "00000000000000000000000000000000000hash1 HEAD",
  88. "00000000000000000000000000000000000hash1 refs/heads/master",
  89. "00000000000000000000000000000000000hash2 refs/heads/v1",
  90. ),
  91. "v1",
  92. reflines(
  93. "00000000000000000000000000000000000hash2 HEAD\x00symref=HEAD:refs/heads/v1",
  94. "00000000000000000000000000000000000hash2 refs/heads/master",
  95. "00000000000000000000000000000000000hash2 refs/heads/v1",
  96. ),
  97. []string{"v1"},
  98. }, {
  99. "Matching tag",
  100. reflines(
  101. "00000000000000000000000000000000000hash1 HEAD",
  102. "00000000000000000000000000000000000hash2 refs/tags/v0",
  103. "00000000000000000000000000000000000hash3 refs/tags/v1",
  104. "00000000000000000000000000000000000hash4 refs/tags/v2",
  105. ),
  106. "v1",
  107. reflines(
  108. "00000000000000000000000000000000000hash3 HEAD",
  109. "00000000000000000000000000000000000hash3 refs/heads/master",
  110. "00000000000000000000000000000000000hash2 refs/tags/v0",
  111. "00000000000000000000000000000000000hash3 refs/tags/v1",
  112. "00000000000000000000000000000000000hash4 refs/tags/v2",
  113. ),
  114. []string{"v0", "v1", "v2"},
  115. }, {
  116. "Tag peeling",
  117. reflines(
  118. "00000000000000000000000000000000000hash1 HEAD",
  119. "00000000000000000000000000000000000hash2 refs/heads/master",
  120. "00000000000000000000000000000000000hash3 refs/tags/v1",
  121. "00000000000000000000000000000000000hash4 refs/tags/v1^{}",
  122. "00000000000000000000000000000000000hash5 refs/tags/v2",
  123. ),
  124. "v1",
  125. reflines(
  126. "00000000000000000000000000000000000hash4 HEAD",
  127. "00000000000000000000000000000000000hash4 refs/heads/master",
  128. "00000000000000000000000000000000000hash3 refs/tags/v1",
  129. "00000000000000000000000000000000000hash4 refs/tags/v1^{}",
  130. "00000000000000000000000000000000000hash5 refs/tags/v2",
  131. ),
  132. []string{"v1", "v1", "v2"},
  133. }, {
  134. "Matching unstable versions",
  135. reflines(
  136. "00000000000000000000000000000000000hash1 HEAD",
  137. "00000000000000000000000000000000000hash2 refs/heads/master",
  138. "00000000000000000000000000000000000hash3 refs/heads/v1",
  139. "00000000000000000000000000000000000hash4 refs/heads/v1.1-unstable",
  140. "00000000000000000000000000000000000hash5 refs/heads/v1.3-unstable",
  141. "00000000000000000000000000000000000hash6 refs/heads/v1.2-unstable",
  142. "00000000000000000000000000000000000hash7 refs/heads/v2",
  143. ),
  144. "v1-unstable",
  145. reflines(
  146. "00000000000000000000000000000000000hash5 HEAD\x00symref=HEAD:refs/heads/v1.3-unstable",
  147. "00000000000000000000000000000000000hash5 refs/heads/master",
  148. "00000000000000000000000000000000000hash3 refs/heads/v1",
  149. "00000000000000000000000000000000000hash4 refs/heads/v1.1-unstable",
  150. "00000000000000000000000000000000000hash5 refs/heads/v1.3-unstable",
  151. "00000000000000000000000000000000000hash6 refs/heads/v1.2-unstable",
  152. "00000000000000000000000000000000000hash7 refs/heads/v2",
  153. ),
  154. []string{"v1", "v1.1-unstable", "v1.2-unstable", "v1.3-unstable", "v2"},
  155. }}
  156. func reflines(lines ...string) string {
  157. var buf bytes.Buffer
  158. buf.WriteString("001e# service=git-upload-pack\n0000")
  159. for _, l := range lines {
  160. buf.WriteString(fmt.Sprintf("%04x%s\n", len(l)+5, l))
  161. }
  162. buf.WriteString("0000")
  163. return buf.String()
  164. }
  165. func (s *RefsSuite) TestChangeRefs(c *C) {
  166. for _, test := range refsTests {
  167. c.Logf(test.summary)
  168. v, ok := parseVersion(test.version)
  169. if !ok {
  170. c.Fatalf("Test has an invalid version: %q", test.version)
  171. }
  172. changed, versions, err := changeRefs([]byte(test.original), v)
  173. c.Assert(err, IsNil)
  174. c.Assert(string(changed), Equals, test.changed)
  175. sort.Sort(versions)
  176. var vs []string
  177. for _, v := range versions {
  178. vs = append(vs, v.String())
  179. }
  180. c.Assert(vs, DeepEquals, test.versions)
  181. }
  182. }