|
| 1 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +import { Command } from "commander"; |
| 17 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 18 | +import { appendHelpHint, installHelpHint } from "./help-hint.js"; |
| 19 | + |
| 20 | +describe("appendHelpHint", () => { |
| 21 | + it("appends a commands hint when an unknown command has no suggestion", () => { |
| 22 | + const out = appendHelpHint("error: unknown command 'xyzzy'\n", "playground"); |
| 23 | + expect(out).toBe( |
| 24 | + "error: unknown command 'xyzzy'\nRun 'playground --help' to see available commands.\n", |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + it("appends an options hint when an unknown option has no suggestion", () => { |
| 29 | + const out = appendHelpHint("error: unknown option '--zzz'\n", "playground"); |
| 30 | + expect(out).toBe( |
| 31 | + "error: unknown option '--zzz'\nRun 'playground --help' to see available options.\n", |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it("leaves an unknown command untouched when commander already suggested one", () => { |
| 36 | + const input = "error: unknown command 'loign'\n(Did you mean login?)\n"; |
| 37 | + expect(appendHelpHint(input, "playground")).toBe(input); |
| 38 | + }); |
| 39 | + |
| 40 | + it("leaves an unknown option untouched when commander already suggested one", () => { |
| 41 | + const input = "error: unknown option '--yse'\n(Did you mean --yes?)\n"; |
| 42 | + expect(appendHelpHint(input, "playground")).toBe(input); |
| 43 | + }); |
| 44 | + |
| 45 | + it("leaves unrelated errors untouched", () => { |
| 46 | + const input = "error: required option '--name <value>' not specified\n"; |
| 47 | + expect(appendHelpHint(input, "playground")).toBe(input); |
| 48 | + }); |
| 49 | + |
| 50 | + it("uses the supplied program name in the hint", () => { |
| 51 | + const out = appendHelpHint("error: unknown command 'nope'\n", "pg"); |
| 52 | + expect(out).toBe( |
| 53 | + "error: unknown command 'nope'\nRun 'pg --help' to see available commands.\n", |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it("appends a trailing newline before the hint when the error text lacks one", () => { |
| 58 | + const out = appendHelpHint("error: unknown command 'nope'", "playground"); |
| 59 | + expect(out).toBe( |
| 60 | + "error: unknown command 'nope'\nRun 'playground --help' to see available commands.\n", |
| 61 | + ); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe("installHelpHint", () => { |
| 66 | + afterEach(() => vi.restoreAllMocks()); |
| 67 | + |
| 68 | + function buildProgram(): Command { |
| 69 | + const program = new Command().name("playground").exitOverride(); |
| 70 | + const login = new Command("login").option("--yes").exitOverride(); |
| 71 | + // addCommand (not .command()) mirrors src/index.ts; commander does NOT |
| 72 | + // propagate the root output config to addCommand'd subcommands, which is |
| 73 | + // exactly why installHelpHint must walk the tree. |
| 74 | + program.addCommand(login); |
| 75 | + installHelpHint(program); |
| 76 | + return program; |
| 77 | + } |
| 78 | + |
| 79 | + function captureStderr(): { read: () => string } { |
| 80 | + let buf = ""; |
| 81 | + vi.spyOn(process.stderr, "write").mockImplementation((chunk: string | Uint8Array) => { |
| 82 | + buf += chunk.toString(); |
| 83 | + return true; |
| 84 | + }); |
| 85 | + return { read: () => buf }; |
| 86 | + } |
| 87 | + |
| 88 | + it("adds the hint to a root-level unknown command with no suggestion", () => { |
| 89 | + const program = buildProgram(); |
| 90 | + const stderr = captureStderr(); |
| 91 | + expect(() => program.parse(["xyzzy"], { from: "user" })).toThrow(); |
| 92 | + expect(stderr.read()).toContain("Run 'playground --help' to see available commands."); |
| 93 | + }); |
| 94 | + |
| 95 | + it("adds the hint to a SUBCOMMAND unknown option with no suggestion", () => { |
| 96 | + const program = buildProgram(); |
| 97 | + const stderr = captureStderr(); |
| 98 | + expect(() => program.parse(["login", "--zzzzzz"], { from: "user" })).toThrow(); |
| 99 | + expect(stderr.read()).toContain("Run 'playground --help' to see available options."); |
| 100 | + }); |
| 101 | + |
| 102 | + it("does not add the hint when the subcommand option has a suggestion", () => { |
| 103 | + const program = buildProgram(); |
| 104 | + const stderr = captureStderr(); |
| 105 | + expect(() => program.parse(["login", "--yse"], { from: "user" })).toThrow(); |
| 106 | + expect(stderr.read()).toContain("Did you mean --yes?"); |
| 107 | + expect(stderr.read()).not.toContain("--help"); |
| 108 | + }); |
| 109 | +}); |
0 commit comments