dependency_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package bundle_entities
  2. import (
  3. "testing"
  4. )
  5. func TestGithubDependencyPatternRegex(t *testing.T) {
  6. testCases := []struct {
  7. name string
  8. input string
  9. expected bool
  10. }{
  11. // Valid patterns
  12. {
  13. name: "basic version pattern",
  14. input: "owner/repo:1.0.0/manifest.yaml",
  15. expected: true,
  16. },
  17. {
  18. name: "version with patch",
  19. input: "owner/repo:1.0.1/manifest.yaml",
  20. expected: true,
  21. },
  22. {
  23. name: "version with pre-release",
  24. input: "owner/repo:1.0.0-beta/manifest.yaml",
  25. expected: true,
  26. },
  27. {
  28. name: "version with x pattern",
  29. input: "owner/repo:1.x.x/manifest.yaml",
  30. expected: true,
  31. },
  32. {
  33. name: "version with X pattern",
  34. input: "owner/repo:1.X.X/manifest.yaml",
  35. expected: true,
  36. },
  37. {
  38. name: "version with mixed x pattern",
  39. input: "owner/repo:1.2.x/manifest.yaml",
  40. expected: true,
  41. },
  42. {
  43. name: "version with tilde",
  44. input: "owner/repo:~1.0.0/manifest.yaml",
  45. expected: true,
  46. },
  47. {
  48. name: "version with caret",
  49. input: "owner/repo:^1.0.0/manifest.yaml",
  50. expected: true,
  51. },
  52. {
  53. name: "version range",
  54. input: "owner/repo:1.0.0-2.0.0/manifest.yaml",
  55. expected: true,
  56. },
  57. {
  58. name: "complex owner and repo names",
  59. input: "complex-owner/complex-repo-name:1.0.0/manifest.yaml",
  60. expected: true,
  61. },
  62. {
  63. name: "underscore in names",
  64. input: "owner_name/repo_name:1.0.0/manifest.yaml",
  65. expected: true,
  66. },
  67. // Invalid patterns
  68. {
  69. name: "four digit version",
  70. input: "owner/repo:1.0.0.1/manifest.yaml",
  71. expected: false,
  72. },
  73. {
  74. name: "empty owner",
  75. input: "/repo:1.0.0/manifest.yaml",
  76. expected: false,
  77. },
  78. {
  79. name: "empty repo",
  80. input: "owner//1.0.0/manifest.yaml",
  81. expected: false,
  82. },
  83. {
  84. name: "invalid version format",
  85. input: "owner/repo:1.0/manifest.yaml",
  86. expected: false,
  87. },
  88. {
  89. name: "missing manifest file",
  90. input: "owner/repo:1.0.0/",
  91. expected: false,
  92. },
  93. {
  94. name: "uppercase in owner",
  95. input: "Owner/repo:1.0.0/manifest.yaml",
  96. expected: false,
  97. },
  98. {
  99. name: "uppercase in repo",
  100. input: "owner/Repo:1.0.0/manifest.yaml",
  101. expected: false,
  102. },
  103. {
  104. name: "invalid characters in owner",
  105. input: "owner@/repo:1.0.0/manifest.yaml",
  106. expected: false,
  107. },
  108. {
  109. name: "invalid characters in repo",
  110. input: "owner/repo#:1.0.0/manifest.yaml",
  111. expected: false,
  112. },
  113. {
  114. name: "too long owner name",
  115. input: "ownerwithaverylongnamethatshouldnotbeallowedinthiscaseownerwithaverylongnamethatshouldnotbeallowedinthiscase/repo:1.0.0/manifest.yaml",
  116. expected: false,
  117. },
  118. {
  119. name: "too long repo name",
  120. input: "owner/repowithavrepowithaverylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirementsrepowithaverylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirementserylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirements:1.0.0/manifest.yaml",
  121. expected: false,
  122. },
  123. {
  124. name: "invalid version range format",
  125. input: "owner/repo:1.0.0-/manifest.yaml",
  126. expected: false,
  127. },
  128. {
  129. name: "invalid pre-release format",
  130. input: "owner/repo:1.0.0-toolongprerelease/manifest.yaml",
  131. expected: false,
  132. },
  133. }
  134. for _, testCase := range testCases {
  135. t.Run(testCase.name, func(t *testing.T) {
  136. result := GITHUB_DEPENDENCY_PATTERN_REGEX_COMPILED.MatchString(testCase.input)
  137. if result != testCase.expected {
  138. t.Errorf("Test case '%s' failed: input '%s' expected %v but got %v, pattern: %s",
  139. testCase.name, testCase.input, testCase.expected, result, GITHUB_DEPENDENCY_PATTERN_REGEX_COMPILED.String())
  140. }
  141. })
  142. }
  143. }
  144. func TestMarketplacePattern(t *testing.T) {
  145. testCases := []struct {
  146. name string
  147. input string
  148. expected bool
  149. }{
  150. {
  151. name: "valid pattern with exact version",
  152. input: "owner/plugin:1.0.0",
  153. expected: true,
  154. },
  155. {
  156. name: "valid pattern with caret version",
  157. input: "owner/plugin:^1.0.0",
  158. expected: true,
  159. },
  160. {
  161. name: "valid pattern with tilde version",
  162. input: "owner/plugin:~1.0.0",
  163. expected: true,
  164. },
  165. {
  166. name: "valid pattern with x version",
  167. input: "owner/plugin:1.x.x",
  168. expected: true,
  169. },
  170. {
  171. name: "valid pattern with version range",
  172. input: "owner/plugin:1.0.0-2.0.0",
  173. expected: true,
  174. },
  175. {
  176. name: "valid pattern with pre-release",
  177. input: "owner/plugin:1.0.0-beta",
  178. expected: true,
  179. },
  180. {
  181. name: "invalid pattern without version",
  182. input: "owner/plugin",
  183. expected: false,
  184. },
  185. {
  186. name: "invalid pattern with empty version",
  187. input: "owner/plugin:",
  188. expected: false,
  189. },
  190. {
  191. name: "uppercase in owner",
  192. input: "Owner/plugin:1.0.0",
  193. expected: false,
  194. },
  195. {
  196. name: "uppercase in plugin",
  197. input: "owner/Plugin:1.0.0",
  198. expected: false,
  199. },
  200. {
  201. name: "invalid characters in owner",
  202. input: "owner@/plugin:1.0.0",
  203. expected: false,
  204. },
  205. {
  206. name: "invalid characters in plugin",
  207. input: "owner/plugin#:1.0.0",
  208. expected: false,
  209. },
  210. {
  211. name: "too long owner name",
  212. input: "ownerwithaverylongnamethatshouldnotbeallowedinthiscaseownerwithaverylongnamethatshouldnotbeallowedinthiscase/plugin:1.0.0",
  213. expected: false,
  214. },
  215. {
  216. name: "too long plugin name",
  217. input: "owner/pluginwithaverylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirementspluginwithaverylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirementspluginwithaverylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirements:1.0.0",
  218. expected: false,
  219. },
  220. {
  221. name: "invalid version range format",
  222. input: "owner/plugin:1.0.0-",
  223. expected: false,
  224. },
  225. {
  226. name: "invalid pre-release format",
  227. input: "owner/plugin:1.0.0-toolongprerelease",
  228. expected: false,
  229. },
  230. }
  231. for _, testCase := range testCases {
  232. t.Run(testCase.name, func(t *testing.T) {
  233. result := MARKETPLACE_PATTERN_REGEX_COMPILED.MatchString(testCase.input)
  234. if result != testCase.expected {
  235. t.Errorf("Test case '%s' failed: input '%s' expected %v but got %v, pattern: %s",
  236. testCase.name, testCase.input, testCase.expected, result, MARKETPLACE_PATTERN_REGEX_COMPILED.String())
  237. }
  238. })
  239. }
  240. }