|
| 1 | +/** |
| 2 | + * Tests for help-as-positional-arg interception in buildCommand. |
| 3 | + * |
| 4 | + * When users type `sentry issue list help` (bare `help` as a positional |
| 5 | + * argument), the buildCommand wrapper detects it and shows the command's |
| 6 | + * help via the introspection system instead of treating it as a target. |
| 7 | + * |
| 8 | + * These tests run commands through Stricli's `run()` with `help` as a |
| 9 | + * trailing positional and verify the output matches the introspection |
| 10 | + * system's help text. |
| 11 | + */ |
| 12 | + |
| 13 | +import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test"; |
| 14 | +import { run } from "@stricli/core"; |
| 15 | +import { app } from "../../src/app.js"; |
| 16 | +import type { SentryContext } from "../../src/context.js"; |
| 17 | +import { useTestConfigDir } from "../helpers.js"; |
| 18 | + |
| 19 | +useTestConfigDir("help-positional-"); |
| 20 | + |
| 21 | +/** Captured output from a command run */ |
| 22 | +type CapturedOutput = { |
| 23 | + stdout: string; |
| 24 | + stderr: string; |
| 25 | +}; |
| 26 | + |
| 27 | +/** Original process.exit before mocking */ |
| 28 | +let originalExit: typeof process.exit; |
| 29 | + |
| 30 | +/** Whether process.exit was called */ |
| 31 | +let exitCalled: { code: number | undefined } | undefined; |
| 32 | + |
| 33 | +beforeEach(() => { |
| 34 | + exitCalled = undefined; |
| 35 | + originalExit = process.exit; |
| 36 | + // Mock process.exit to prevent actually exiting during tests. |
| 37 | + // Throws to stop execution (like the real process.exit would). |
| 38 | + process.exit = mock((code?: number) => { |
| 39 | + exitCalled = { code: code ?? 0 }; |
| 40 | + throw new Error(`process.exit(${code})`); |
| 41 | + }) as typeof process.exit; |
| 42 | +}); |
| 43 | + |
| 44 | +afterEach(() => { |
| 45 | + process.exit = originalExit; |
| 46 | +}); |
| 47 | + |
| 48 | +/** |
| 49 | + * Run a command through Stricli and capture stdout/stderr. |
| 50 | + * |
| 51 | + * Since the help interception calls `process.exit(0)`, this catches |
| 52 | + * the mock exit error and returns the captured output. |
| 53 | + */ |
| 54 | +async function runCommand(args: string[]): Promise<CapturedOutput> { |
| 55 | + let stdout = ""; |
| 56 | + let stderr = ""; |
| 57 | + |
| 58 | + const stdoutWriter = { |
| 59 | + write(data: string | Uint8Array) { |
| 60 | + stdout += |
| 61 | + typeof data === "string" ? data : new TextDecoder().decode(data); |
| 62 | + return true; |
| 63 | + }, |
| 64 | + }; |
| 65 | + const stderrWriter = { |
| 66 | + write(data: string | Uint8Array) { |
| 67 | + stderr += |
| 68 | + typeof data === "string" ? data : new TextDecoder().decode(data); |
| 69 | + return true; |
| 70 | + }, |
| 71 | + }; |
| 72 | + |
| 73 | + const baseContext: SentryContext = { |
| 74 | + process, |
| 75 | + env: process.env, |
| 76 | + cwd: process.cwd(), |
| 77 | + homeDir: "/tmp", |
| 78 | + configDir: "/tmp", |
| 79 | + stdout: stdoutWriter, |
| 80 | + stderr: stderrWriter, |
| 81 | + stdin: process.stdin, |
| 82 | + }; |
| 83 | + |
| 84 | + // Stricli calls forCommand({ prefix }) before running the command. |
| 85 | + // We must provide it so commandPrefix is set on the context. |
| 86 | + const mockContext = { |
| 87 | + ...baseContext, |
| 88 | + forCommand: ({ prefix }: { prefix: readonly string[] }): SentryContext => ({ |
| 89 | + ...baseContext, |
| 90 | + commandPrefix: prefix, |
| 91 | + }), |
| 92 | + }; |
| 93 | + |
| 94 | + try { |
| 95 | + await run(app, args, mockContext); |
| 96 | + } catch { |
| 97 | + // process.exit mock throws — expected |
| 98 | + } |
| 99 | + |
| 100 | + return { stdout, stderr }; |
| 101 | +} |
| 102 | + |
| 103 | +describe("help as positional argument", () => { |
| 104 | + test("sentry issue list help → shows help for issue list", async () => { |
| 105 | + const { stdout, stderr } = await runCommand(["issue", "list", "help"]); |
| 106 | + |
| 107 | + // Should show command help output (from introspection) |
| 108 | + expect(stdout).toContain("sentry issue list"); |
| 109 | + // Should show the tip about --help |
| 110 | + expect(stderr).toContain("--help"); |
| 111 | + expect(stderr).toContain("Tip"); |
| 112 | + // Should exit with code 0 |
| 113 | + expect(exitCalled).toEqual({ code: 0 }); |
| 114 | + }); |
| 115 | + |
| 116 | + test("sentry log list help → shows help for log list", async () => { |
| 117 | + const { stdout, stderr } = await runCommand(["log", "list", "help"]); |
| 118 | + |
| 119 | + expect(stdout).toContain("sentry log list"); |
| 120 | + expect(stderr).toContain("--help"); |
| 121 | + expect(exitCalled).toEqual({ code: 0 }); |
| 122 | + }); |
| 123 | + |
| 124 | + test("sentry trace view help → shows help for trace view", async () => { |
| 125 | + const { stdout, stderr } = await runCommand(["trace", "view", "help"]); |
| 126 | + |
| 127 | + expect(stdout).toContain("sentry trace view"); |
| 128 | + expect(stderr).toContain("--help"); |
| 129 | + expect(exitCalled).toEqual({ code: 0 }); |
| 130 | + }); |
| 131 | + |
| 132 | + test("sentry project list help → shows help for project list", async () => { |
| 133 | + const { stdout, stderr } = await runCommand(["project", "list", "help"]); |
| 134 | + |
| 135 | + expect(stdout).toContain("sentry project list"); |
| 136 | + expect(stderr).toContain("--help"); |
| 137 | + expect(exitCalled).toEqual({ code: 0 }); |
| 138 | + }); |
| 139 | + |
| 140 | + test("sentry issue view help → shows help for issue view", async () => { |
| 141 | + const { stdout, stderr } = await runCommand(["issue", "view", "help"]); |
| 142 | + |
| 143 | + expect(stdout).toContain("sentry issue view"); |
| 144 | + expect(stderr).toContain("--help"); |
| 145 | + expect(exitCalled).toEqual({ code: 0 }); |
| 146 | + }); |
| 147 | + |
| 148 | + test("stderr hint includes the correct command path", async () => { |
| 149 | + const { stderr } = await runCommand(["span", "list", "help"]); |
| 150 | + |
| 151 | + expect(stderr).toContain("sentry span list --help"); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +describe("help command unchanged", () => { |
| 156 | + test("sentry help still shows branded help", async () => { |
| 157 | + const { stdout } = await runCommand(["help"]); |
| 158 | + |
| 159 | + // Custom help command shows branded output |
| 160 | + expect(stdout).toContain("sentry"); |
| 161 | + // Should NOT have exited via our interception |
| 162 | + expect(exitCalled).toBeUndefined(); |
| 163 | + }); |
| 164 | + |
| 165 | + test("sentry help issue list still shows introspected help", async () => { |
| 166 | + const { stdout } = await runCommand(["help", "issue", "list"]); |
| 167 | + |
| 168 | + expect(stdout).toContain("sentry issue list"); |
| 169 | + // Should NOT have exited via our interception |
| 170 | + expect(exitCalled).toBeUndefined(); |
| 171 | + }); |
| 172 | +}); |
0 commit comments