|
| 1 | +/** |
| 2 | + * @Project: @cldmv/fix-headers |
| 3 | + * @Filename: /tests/gpg-signer-uid.test.vitest.mjs |
| 4 | + * @Date: 2026-09-27T23:43:47-07:00 (1790577827) |
| 5 | + * @Author: Nate Corcoran <CLDMV> |
| 6 | + |
| 7 | + * ----- |
| 8 | + * @Last modified by: Nate Corcoran <CLDMV> ([email protected]) |
| 9 | + * @Last modified time: 2026-09-27T23:43:47-07:00 (1790577827) |
| 10 | + * ----- |
| 11 | + * @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved. |
| 12 | + */ |
| 13 | + |
| 14 | +import { execFile } from "node:child_process"; |
| 15 | +import { chmod, writeFile } from "node:fs/promises"; |
| 16 | +import { promisify } from "node:util"; |
| 17 | +import { join } from "node:path"; |
| 18 | +import { describe, expect, it } from "vitest"; |
| 19 | +import { fixHeaders } from "../src/fix-header.mjs"; |
| 20 | +import { detectGitAuthor, parseSignerUid } from "../src/utils/git.mjs"; |
| 21 | +import { cleanupWorkspace, createWorkspace, writeWorkspaceFile } from "./helpers/workspace.mjs"; |
| 22 | + |
| 23 | +const execFileAsync = promisify(execFile); |
| 24 | + |
| 25 | +/** |
| 26 | + * Builds a bash git shim that answers the listed argument patterns and delegates everything else to the real git. |
| 27 | + * @param {Array<{match: string, output: string | null}>} rules - `$*` substring to match and the line to print (null prints nothing). |
| 28 | + * @returns {string} Shim script source. |
| 29 | + */ |
| 30 | +function buildGitShim(rules) { |
| 31 | + const branches = rules |
| 32 | + .map(({ match, output }) => { |
| 33 | + const body = output === null ? "" : ` printf '%s\\n' ${JSON.stringify(output)}\n`; |
| 34 | + return `if [[ "$*" == *${JSON.stringify(match)}* ]]; then\n${body} exit 0\nfi\n`; |
| 35 | + }) |
| 36 | + .join(""); |
| 37 | + return `#!/usr/bin/env bash\nset -e\n${branches}exec /usr/bin/git "$@"\n`; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Creates a committed git workspace and puts a git shim first on PATH. |
| 42 | + * @param {string} name - Workspace name suffix. |
| 43 | + * @param {Array<{match: string, output: string | null}>} rules - Shim rules passed to {@link buildGitShim}. |
| 44 | + * @returns {Promise<{workspace: string, restore: () => Promise<void>}>} Workspace path and a restore callback. |
| 45 | + */ |
| 46 | +async function createShimmedWorkspace(name, rules) { |
| 47 | + const workspace = await createWorkspace(name); |
| 48 | + const previousPath = process.env.PATH; |
| 49 | + await writeWorkspaceFile(join(workspace, "package.json"), JSON.stringify({ name }, null, 2)); |
| 50 | + await writeWorkspaceFile(join(workspace, "src", "main.mjs"), "export const x = true;\n"); |
| 51 | + await execFileAsync("git", ["init"], { cwd: workspace }); |
| 52 | + await execFileAsync("git", ["config", "user.name", "Configured Name"], { cwd: workspace }); |
| 53 | + await execFileAsync("git", ["config", "user.email", "[email protected]"], { cwd: workspace }); |
| 54 | + await execFileAsync("git", ["add", "."], { cwd: workspace }); |
| 55 | + await execFileAsync("git", ["-c", "commit.gpgsign=false", "commit", "-m", "init"], { cwd: workspace }); |
| 56 | + |
| 57 | + const shimPath = join(workspace, "git"); |
| 58 | + await writeFile(shimPath, buildGitShim(rules)); |
| 59 | + await chmod(shimPath, 0o755); |
| 60 | + process.env.PATH = `${workspace}:${previousPath}`; |
| 61 | + |
| 62 | + return { |
| 63 | + workspace, |
| 64 | + restore: async () => { |
| 65 | + process.env.PATH = previousPath; |
| 66 | + await cleanupWorkspace(workspace); |
| 67 | + } |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +describe("parseSignerUid", () => { |
| 72 | + it("drops the OpenPGP UID comment before the email", () => { |
| 73 | + expect(parseSignerUid("Nate Corcoran (2023 PC) <[email protected]>")).toEqual({ |
| 74 | + authorName: "Nate Corcoran", |
| 75 | + |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("parses a UID without a comment unchanged", () => { |
| 80 | + expect(parseSignerUid("Nate Corcoran <[email protected]>")).toEqual({ |
| 81 | + authorName: "Nate Corcoran", |
| 82 | + |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("parses an email-only UID with no name", () => { |
| 87 | + expect(parseSignerUid("<[email protected]>")).toEqual({ authorName: null, authorEmail: "[email protected]" }); |
| 88 | + }); |
| 89 | + |
| 90 | + it("parses a name-only UID", () => { |
| 91 | + expect(parseSignerUid("Signer Name")).toEqual({ authorName: "Signer Name", authorEmail: null }); |
| 92 | + }); |
| 93 | + |
| 94 | + it("drops a trailing comment from a name-only UID", () => { |
| 95 | + expect(parseSignerUid("Signer Name (Desktop)")).toEqual({ authorName: "Signer Name", authorEmail: null }); |
| 96 | + }); |
| 97 | + |
| 98 | + it("keeps parentheses that sit inside the name rather than at its end", () => { |
| 99 | + expect(parseSignerUid("Jane (JJ) Doe <[email protected]>")).toEqual({ authorName: "Jane (JJ) Doe", authorEmail: "[email protected]" }); |
| 100 | + expect(parseSignerUid("Jane (JJ) Doe (Work) <[email protected]>").authorName).toBe("Jane (JJ) Doe"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("drops a comment that contains nested parentheses", () => { |
| 104 | + expect(parseSignerUid("Signer Name (Home (old box)) <[email protected]>").authorName).toBe("Signer Name"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("only drops the last of several trailing comments", () => { |
| 108 | + expect(parseSignerUid("Signer Name (A) (B) <[email protected]>").authorName).toBe("Signer Name (A)"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("treats backslash-escaped parentheses as literal comment text", () => { |
| 112 | + expect(parseSignerUid(String.raw`Signer Name (a \) b \( c) <[email protected]>`).authorName).toBe("Signer Name"); |
| 113 | + expect(parseSignerUid(String.raw`Signer Name (a \\) <[email protected]>`).authorName).toBe("Signer Name"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("keeps a name ending in an escaped parenthesis", () => { |
| 117 | + expect(parseSignerUid(String.raw`Signer Name \)`).authorName).toBe(String.raw`Signer Name \)`); |
| 118 | + }); |
| 119 | + |
| 120 | + it("keeps a name whose trailing parenthesis is unbalanced", () => { |
| 121 | + expect(parseSignerUid("Signer Name) <[email protected]>").authorName).toBe("Signer Name)"); |
| 122 | + }); |
| 123 | + |
| 124 | + it("falls back to the raw name when the UID is only a comment", () => { |
| 125 | + expect(parseSignerUid("(Laptop) <[email protected]>")).toEqual({ authorName: "(Laptop)", authorEmail: "[email protected]" }); |
| 126 | + expect(parseSignerUid("(Laptop)")).toEqual({ authorName: "(Laptop)", authorEmail: null }); |
| 127 | + }); |
| 128 | + |
| 129 | + it("returns nulls for an empty or whitespace-only UID", () => { |
| 130 | + expect(parseSignerUid("")).toEqual({ authorName: null, authorEmail: null }); |
| 131 | + expect(parseSignerUid(" ")).toEqual({ authorName: null, authorEmail: null }); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +describe("detectGitAuthor with useGpgSignerAuthor", () => { |
| 136 | + it("uses the signer name without its UID comment", async () => { |
| 137 | + const { workspace, restore } = await createShimmedWorkspace("gpg-uid-comment", [ |
| 138 | + { match: "log -1 --format=%GS", output: "Nate Corcoran (2023 PC) <[email protected]>" } |
| 139 | + ]); |
| 140 | + try { |
| 141 | + const author = await detectGitAuthor(workspace, { useGpgSignerAuthor: true }); |
| 142 | + expect(author.authorName).toBe("Nate Corcoran"); |
| 143 | + expect(author.authorEmail).toBe("[email protected]"); |
| 144 | + } finally { |
| 145 | + await restore(); |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + it("falls back to user.name when the last commit is unsigned", async () => { |
| 150 | + const { workspace, restore } = await createShimmedWorkspace("gpg-uid-unsigned", [{ match: "log -1 --format=%GS", output: null }]); |
| 151 | + try { |
| 152 | + const author = await detectGitAuthor(workspace, { useGpgSignerAuthor: true }); |
| 153 | + expect(author.authorName).toBe("Configured Name"); |
| 154 | + expect(author.authorEmail).toBe("[email protected]"); |
| 155 | + } finally { |
| 156 | + await restore(); |
| 157 | + } |
| 158 | + }); |
| 159 | + |
| 160 | + it("falls back to the log author when the commit is unsigned and user.name is unset", async () => { |
| 161 | + const { workspace, restore } = await createShimmedWorkspace("gpg-uid-unsigned-no-config", [ |
| 162 | + { match: "config --get user.name", output: null }, |
| 163 | + { match: "config --get user.email", output: null }, |
| 164 | + { match: "log -1 --format=%GS", output: null }, |
| 165 | + { match: "log -1 --format=%an|%ae", output: "Log Author|[email protected]" } |
| 166 | + ]); |
| 167 | + try { |
| 168 | + const author = await detectGitAuthor(workspace, { useGpgSignerAuthor: true }); |
| 169 | + expect(author.authorName).toBe("Log Author"); |
| 170 | + expect(author.authorEmail).toBe("[email protected]"); |
| 171 | + } finally { |
| 172 | + await restore(); |
| 173 | + } |
| 174 | + }); |
| 175 | +}); |
| 176 | + |
| 177 | +describe("fixHeaders with useGpgSignerAuthor and company", () => { |
| 178 | + it("writes the signer name without its UID comment and with the company suffix", async () => { |
| 179 | + const { workspace, restore } = await createShimmedWorkspace("gpg-uid-fix-headers", [ |
| 180 | + { match: "log -1 --format=%GS", output: "Nate Corcoran (2023 PC) <[email protected]>" } |
| 181 | + ]); |
| 182 | + try { |
| 183 | + const result = await fixHeaders({ |
| 184 | + cwd: workspace, |
| 185 | + input: "src/main.mjs", |
| 186 | + useGpgSignerAuthor: true, |
| 187 | + company: "CLDMV", |
| 188 | + sampleOutput: true, |
| 189 | + dryRun: true |
| 190 | + }); |
| 191 | + |
| 192 | + expect(result.filesScanned).toBe(1); |
| 193 | + const header = result.changes[0]?.sample?.newValue; |
| 194 | + expect(header).toContain("@Author: Nate Corcoran <CLDMV>"); |
| 195 | + expect(header).not.toContain("2023 PC"); |
| 196 | + } finally { |
| 197 | + await restore(); |
| 198 | + } |
| 199 | + }); |
| 200 | +}); |
0 commit comments