|
| 1 | +/** |
| 2 | + * Tests for `bundle-changelog.ts`. |
| 3 | + */ |
| 4 | + |
| 5 | +import * as assert from "node:assert/strict"; |
| 6 | +import * as fs from "node:fs"; |
| 7 | +import * as os from "node:os"; |
| 8 | +import * as path from "node:path"; |
| 9 | +import { afterEach, beforeEach, describe, it } from "node:test"; |
| 10 | + |
| 11 | +import { |
| 12 | + CLI_VERSION_ENV_VAR, |
| 13 | + getCLIVersion, |
| 14 | + getPRNumber, |
| 15 | + getPRUrl, |
| 16 | + PR_URL_ENV_VAR, |
| 17 | + updateChangelog, |
| 18 | +} from "./bundle-changelog"; |
| 19 | +import { |
| 20 | + EMPTY_CHANGELOG, |
| 21 | + NO_CHANGES_STR, |
| 22 | + UNRELEASED_PLACEHOLDER, |
| 23 | +} from "./changelog"; |
| 24 | + |
| 25 | +let testDir: string; |
| 26 | + |
| 27 | +beforeEach(() => { |
| 28 | + // Set up a temporary directory for testing |
| 29 | + testDir = fs.mkdtempSync(path.join(os.tmpdir(), "bundle-changelog-test-")); |
| 30 | +}); |
| 31 | + |
| 32 | +afterEach(() => { |
| 33 | + /** Clean up temporary directories. */ |
| 34 | + fs.rmSync(testDir, { recursive: true, force: true }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe("getCLIVersion", async () => { |
| 38 | + await it("throws if the environment variable is not set", async () => { |
| 39 | + delete process.env[CLI_VERSION_ENV_VAR]; |
| 40 | + assert.throws(() => getCLIVersion()); |
| 41 | + }); |
| 42 | + |
| 43 | + await it("throws if the environment variable is empty", async () => { |
| 44 | + process.env[CLI_VERSION_ENV_VAR] = " "; |
| 45 | + assert.throws(() => getCLIVersion()); |
| 46 | + }); |
| 47 | + |
| 48 | + await it("returns value of the environment variable if set", async () => { |
| 49 | + const testValue = "1.2.3"; |
| 50 | + process.env[CLI_VERSION_ENV_VAR] = testValue; |
| 51 | + assert.deepEqual(getCLIVersion(), testValue); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +const testPrUrl = "https://github.com/github/codeql-action/pulls/42"; |
| 56 | + |
| 57 | +describe("getPRUrl", async () => { |
| 58 | + await it("throws if the environment variable is not set", async () => { |
| 59 | + delete process.env[PR_URL_ENV_VAR]; |
| 60 | + assert.throws(() => getPRUrl()); |
| 61 | + }); |
| 62 | + |
| 63 | + await it("throws if the environment variable is empty", async () => { |
| 64 | + process.env[PR_URL_ENV_VAR] = " "; |
| 65 | + assert.throws(() => getPRUrl()); |
| 66 | + }); |
| 67 | + |
| 68 | + await it("returns value of the environment variable if set", async () => { |
| 69 | + process.env[PR_URL_ENV_VAR] = testPrUrl; |
| 70 | + assert.deepEqual(getPRUrl(), testPrUrl); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe("getPRNumber", async () => { |
| 75 | + await it("throws if the last part of the input is not a number", async () => { |
| 76 | + assert.throws(() => getPRNumber(`${testPrUrl}/foo`)); |
| 77 | + }); |
| 78 | + |
| 79 | + await it("throws if the last part of the input is not a positive number", async () => { |
| 80 | + assert.throws(() => getPRNumber(`${testPrUrl}/-100`)); |
| 81 | + }); |
| 82 | + |
| 83 | + await it("returns the PR number from an URL", async () => { |
| 84 | + assert.equal(getPRNumber(testPrUrl), 42); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +const testChangelog = `${EMPTY_CHANGELOG.trimEnd()} |
| 89 | +
|
| 90 | +## 4.23.7 |
| 91 | +
|
| 92 | +- Other change |
| 93 | +
|
| 94 | +## 4.23.6 |
| 95 | +
|
| 96 | +${NO_CHANGES_STR}`; |
| 97 | + |
| 98 | +const expectedChangelog = `# CodeQL Action Changelog |
| 99 | +
|
| 100 | +## ${UNRELEASED_PLACEHOLDER} |
| 101 | +
|
| 102 | +- Update default CodeQL bundle version to |
| 103 | +
|
| 104 | +## 4.23.7 |
| 105 | +
|
| 106 | +- Other change |
| 107 | +
|
| 108 | +## 4.23.6 |
| 109 | +
|
| 110 | +${NO_CHANGES_STR}`; |
| 111 | + |
| 112 | +describe("updateChangelog", async () => { |
| 113 | + await it("removes `NO_CHANGES_STR` if present in [UNRELEASED] section", async () => { |
| 114 | + const result = updateChangelog(EMPTY_CHANGELOG, ""); |
| 115 | + assert.ok(!result.includes(NO_CHANGES_STR.trim())); |
| 116 | + }); |
| 117 | + |
| 118 | + await it("doesn't remove `NO_CHANGES_STR` if present in versioned section", async () => { |
| 119 | + const result = updateChangelog( |
| 120 | + EMPTY_CHANGELOG.replace(UNRELEASED_PLACEHOLDER, "1.2.3"), |
| 121 | + "", |
| 122 | + ); |
| 123 | + assert.ok(result.includes(NO_CHANGES_STR.trim())); |
| 124 | + }); |
| 125 | + |
| 126 | + await it("throws if there are no sections", async () => { |
| 127 | + assert.throws(() => { |
| 128 | + updateChangelog( |
| 129 | + "# CodeQL Action Changelog", |
| 130 | + "- Update default CodeQL bundle version to", |
| 131 | + ); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + await it("adds note at the end of the first section", async () => { |
| 136 | + const result = updateChangelog( |
| 137 | + testChangelog, |
| 138 | + "- Update default CodeQL bundle version to", |
| 139 | + ); |
| 140 | + assert.deepEqual(result, expectedChangelog); |
| 141 | + }); |
| 142 | +}); |
0 commit comments