util.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import {
  2. releaseBody,
  3. isTag,
  4. paths,
  5. parseConfig,
  6. parseInputFiles,
  7. unmatchedPatterns,
  8. uploadUrl,
  9. } from "../src/util";
  10. import * as assert from "assert";
  11. describe("util", () => {
  12. describe("uploadUrl", () => {
  13. it("strips template", () => {
  14. assert.equal(
  15. uploadUrl(
  16. "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}"
  17. ),
  18. "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets"
  19. );
  20. });
  21. });
  22. describe("parseInputFiles", () => {
  23. it("parses empty strings", () => {
  24. assert.deepStrictEqual(parseInputFiles(""), []);
  25. });
  26. it("parses comma-delimited strings", () => {
  27. assert.deepStrictEqual(parseInputFiles("foo,bar"), ["foo", "bar"]);
  28. });
  29. it("parses newline and comma-delimited (and then some)", () => {
  30. assert.deepStrictEqual(
  31. parseInputFiles("foo,bar\nbaz,boom,\n\ndoom,loom "),
  32. ["foo", "bar", "baz", "boom", "doom", "loom"]
  33. );
  34. });
  35. });
  36. describe("releaseBody", () => {
  37. it("uses input body", () => {
  38. assert.equal(
  39. "foo",
  40. releaseBody({
  41. github_ref: "",
  42. github_repository: "",
  43. github_token: "",
  44. input_body: "foo",
  45. input_body_path: undefined,
  46. input_draft: false,
  47. input_prerelease: false,
  48. input_files: [],
  49. input_name: undefined,
  50. input_tag_name: undefined,
  51. input_target_commitish: undefined,
  52. input_discussion_category_name: undefined,
  53. input_generate_release_notes: false,
  54. })
  55. );
  56. });
  57. it("uses input body path", () => {
  58. assert.equal(
  59. "bar",
  60. releaseBody({
  61. github_ref: "",
  62. github_repository: "",
  63. github_token: "",
  64. input_body: undefined,
  65. input_body_path: "__tests__/release.txt",
  66. input_draft: false,
  67. input_prerelease: false,
  68. input_files: [],
  69. input_name: undefined,
  70. input_tag_name: undefined,
  71. input_target_commitish: undefined,
  72. input_discussion_category_name: undefined,
  73. input_generate_release_notes: false,
  74. })
  75. );
  76. });
  77. it("defaults to body path when both body and body path are provided", () => {
  78. assert.equal(
  79. "bar",
  80. releaseBody({
  81. github_ref: "",
  82. github_repository: "",
  83. github_token: "",
  84. input_body: "foo",
  85. input_body_path: "__tests__/release.txt",
  86. input_draft: false,
  87. input_prerelease: false,
  88. input_files: [],
  89. input_name: undefined,
  90. input_tag_name: undefined,
  91. input_target_commitish: undefined,
  92. input_discussion_category_name: undefined,
  93. input_generate_release_notes: false,
  94. })
  95. );
  96. });
  97. });
  98. describe("parseConfig", () => {
  99. it("parses basic config", () => {
  100. assert.deepStrictEqual(
  101. parseConfig({
  102. // note: inputs declared in actions.yml, even when declared not required,
  103. // are still provided by the actions runtime env as empty strings instead of
  104. // the normal absent env value one would expect. this breaks things
  105. // as an empty string !== undefined in terms of what we pass to the api
  106. // so we cover that in a test case here to ensure undefined values are actually
  107. // resolved as undefined and not empty strings
  108. INPUT_TARGET_COMMITISH: "",
  109. INPUT_DISCUSSION_CATEGORY_NAME: "",
  110. }),
  111. {
  112. github_ref: "",
  113. github_repository: "",
  114. github_token: "",
  115. input_append_body: false,
  116. input_body: undefined,
  117. input_body_path: undefined,
  118. input_draft: undefined,
  119. input_prerelease: undefined,
  120. input_files: [],
  121. input_name: undefined,
  122. input_tag_name: undefined,
  123. input_fail_on_unmatched_files: false,
  124. input_target_commitish: undefined,
  125. input_discussion_category_name: undefined,
  126. input_generate_release_notes: false,
  127. }
  128. );
  129. });
  130. it("parses basic config with commitish", () => {
  131. assert.deepStrictEqual(
  132. parseConfig({
  133. INPUT_TARGET_COMMITISH: "affa18ef97bc9db20076945705aba8c516139abd",
  134. }),
  135. {
  136. github_ref: "",
  137. github_repository: "",
  138. github_token: "",
  139. input_append_body: false,
  140. input_body: undefined,
  141. input_body_path: undefined,
  142. input_draft: undefined,
  143. input_prerelease: undefined,
  144. input_files: [],
  145. input_name: undefined,
  146. input_tag_name: undefined,
  147. input_fail_on_unmatched_files: false,
  148. input_target_commitish: "affa18ef97bc9db20076945705aba8c516139abd",
  149. input_discussion_category_name: undefined,
  150. input_generate_release_notes: false,
  151. }
  152. );
  153. });
  154. it("supports discussion category names", () => {
  155. assert.deepStrictEqual(
  156. parseConfig({
  157. INPUT_DISCUSSION_CATEGORY_NAME: "releases",
  158. }),
  159. {
  160. github_ref: "",
  161. github_repository: "",
  162. github_token: "",
  163. input_append_body: false,
  164. input_body: undefined,
  165. input_body_path: undefined,
  166. input_draft: undefined,
  167. input_prerelease: undefined,
  168. input_files: [],
  169. input_name: undefined,
  170. input_tag_name: undefined,
  171. input_fail_on_unmatched_files: false,
  172. input_target_commitish: undefined,
  173. input_discussion_category_name: "releases",
  174. input_generate_release_notes: false,
  175. }
  176. );
  177. });
  178. it("supports generating release notes", () => {
  179. assert.deepStrictEqual(
  180. parseConfig({
  181. INPUT_GENERATE_RELEASE_NOTES: "true",
  182. }),
  183. {
  184. github_ref: "",
  185. github_repository: "",
  186. github_token: "",
  187. input_append_body: false,
  188. input_body: undefined,
  189. input_body_path: undefined,
  190. input_draft: undefined,
  191. input_prerelease: undefined,
  192. input_files: [],
  193. input_name: undefined,
  194. input_tag_name: undefined,
  195. input_fail_on_unmatched_files: false,
  196. input_target_commitish: undefined,
  197. input_discussion_category_name: undefined,
  198. input_generate_release_notes: true,
  199. }
  200. );
  201. });
  202. it("prefers GITHUB_TOKEN over token input for backwards compatibility", () => {
  203. assert.deepStrictEqual(
  204. parseConfig({
  205. INPUT_DRAFT: "false",
  206. INPUT_PRERELEASE: "true",
  207. GITHUB_TOKEN: "env-token",
  208. INPUT_TOKEN: "input-token",
  209. }),
  210. {
  211. github_ref: "",
  212. github_repository: "",
  213. github_token: "env-token",
  214. input_append_body: false,
  215. input_body: undefined,
  216. input_body_path: undefined,
  217. input_draft: false,
  218. input_prerelease: true,
  219. input_files: [],
  220. input_name: undefined,
  221. input_tag_name: undefined,
  222. input_fail_on_unmatched_files: false,
  223. input_target_commitish: undefined,
  224. input_discussion_category_name: undefined,
  225. input_generate_release_notes: false,
  226. }
  227. );
  228. });
  229. it("uses input token as the source of GITHUB_TOKEN by default", () => {
  230. assert.deepStrictEqual(
  231. parseConfig({
  232. INPUT_DRAFT: "false",
  233. INPUT_PRERELEASE: "true",
  234. INPUT_TOKEN: "input-token",
  235. }),
  236. {
  237. github_ref: "",
  238. github_repository: "",
  239. github_token: "input-token",
  240. input_append_body: false,
  241. input_body: undefined,
  242. input_body_path: undefined,
  243. input_draft: false,
  244. input_prerelease: true,
  245. input_files: [],
  246. input_name: undefined,
  247. input_tag_name: undefined,
  248. input_fail_on_unmatched_files: false,
  249. input_target_commitish: undefined,
  250. input_discussion_category_name: undefined,
  251. input_generate_release_notes: false,
  252. }
  253. );
  254. });
  255. it("parses basic config with draft and prerelease", () => {
  256. assert.deepStrictEqual(
  257. parseConfig({
  258. INPUT_DRAFT: "false",
  259. INPUT_PRERELEASE: "true",
  260. }),
  261. {
  262. github_ref: "",
  263. github_repository: "",
  264. github_token: "",
  265. input_append_body: false,
  266. input_body: undefined,
  267. input_body_path: undefined,
  268. input_draft: false,
  269. input_prerelease: true,
  270. input_files: [],
  271. input_name: undefined,
  272. input_tag_name: undefined,
  273. input_fail_on_unmatched_files: false,
  274. input_target_commitish: undefined,
  275. input_discussion_category_name: undefined,
  276. input_generate_release_notes: false,
  277. }
  278. );
  279. });
  280. it("parses basic config with append_body", () => {
  281. assert.deepStrictEqual(
  282. parseConfig({
  283. INPUT_APPEND_BODY: "true",
  284. }),
  285. {
  286. github_ref: "",
  287. github_repository: "",
  288. github_token: "",
  289. input_append_body: true,
  290. input_body: undefined,
  291. input_body_path: undefined,
  292. input_draft: undefined,
  293. input_prerelease: undefined,
  294. input_files: [],
  295. input_name: undefined,
  296. input_tag_name: undefined,
  297. input_fail_on_unmatched_files: false,
  298. input_target_commitish: undefined,
  299. input_discussion_category_name: undefined,
  300. input_generate_release_notes: false,
  301. }
  302. );
  303. });
  304. });
  305. describe("isTag", () => {
  306. it("returns true for tags", async () => {
  307. assert.equal(isTag("refs/tags/foo"), true);
  308. });
  309. it("returns false for other kinds of refs", async () => {
  310. assert.equal(isTag("refs/heads/master"), false);
  311. });
  312. });
  313. describe("paths", () => {
  314. it("resolves files given a set of paths", async () => {
  315. assert.deepStrictEqual(
  316. paths(["tests/data/**/*", "tests/data/does/not/exist/*"]),
  317. ["tests/data/foo/bar.txt"]
  318. );
  319. });
  320. });
  321. describe("unmatchedPatterns", () => {
  322. it("returns the patterns that don't match any files", async () => {
  323. assert.deepStrictEqual(
  324. unmatchedPatterns(["tests/data/**/*", "tests/data/does/not/exist/*"]),
  325. ["tests/data/does/not/exist/*"]
  326. );
  327. });
  328. });
  329. });