|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "utils/shell_completion" |
| 4 | + |
| 5 | +RSpec.describe Utils::ShellCompletion do |
| 6 | + describe ".default_completion_shells" do |
| 7 | + it "returns bash, zsh, and fish for nil format" do |
| 8 | + expect(described_class.default_completion_shells(nil)).to eq([:bash, :zsh, :fish]) |
| 9 | + end |
| 10 | + |
| 11 | + it "returns bash, zsh, and fish for unrecognized format" do |
| 12 | + expect(described_class.default_completion_shells(:unknown)).to eq([:bash, :zsh, :fish]) |
| 13 | + end |
| 14 | + |
| 15 | + it "includes pwsh for cobra format" do |
| 16 | + expect(described_class.default_completion_shells(:cobra)).to eq([:bash, :zsh, :fish, :pwsh]) |
| 17 | + end |
| 18 | + |
| 19 | + it "includes pwsh for typer format" do |
| 20 | + expect(described_class.default_completion_shells(:typer)).to eq([:bash, :zsh, :fish, :pwsh]) |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + describe ".completion_shell_parameter" do |
| 25 | + let(:env) { {} } |
| 26 | + |
| 27 | + it "returns shell name for nil format" do |
| 28 | + expect(described_class.completion_shell_parameter(nil, :bash, "/usr/bin/foo", env)).to eq("bash") |
| 29 | + end |
| 30 | + |
| 31 | + it "returns --shell=<name> for :arg format" do |
| 32 | + expect(described_class.completion_shell_parameter(:arg, :zsh, "/usr/bin/foo", env)).to eq("--shell=zsh") |
| 33 | + end |
| 34 | + |
| 35 | + it "sets env and returns nil for :clap format" do |
| 36 | + result = described_class.completion_shell_parameter(:clap, :fish, "/usr/bin/foo", env) |
| 37 | + expect(result).to be_nil |
| 38 | + expect(env["COMPLETE"]).to eq("fish") |
| 39 | + end |
| 40 | + |
| 41 | + it "sets env with uppercased program name for :click format" do |
| 42 | + result = described_class.completion_shell_parameter(:click, :bash, "/usr/local/bin/my-tool", env) |
| 43 | + expect(result).to be_nil |
| 44 | + expect(env["_MY_TOOL_COMPLETE"]).to eq("bash_source") |
| 45 | + end |
| 46 | + |
| 47 | + it "returns subcommand array for :cobra format" do |
| 48 | + result = described_class.completion_shell_parameter(:cobra, :zsh, "/usr/bin/foo", env) |
| 49 | + expect(result).to eq(["completion", "zsh"]) |
| 50 | + end |
| 51 | + |
| 52 | + it "returns --<shell> for :flag format" do |
| 53 | + expect(described_class.completion_shell_parameter(:flag, :fish, "/usr/bin/foo", env)).to eq("--fish") |
| 54 | + end |
| 55 | + |
| 56 | + it "returns nil for :none format" do |
| 57 | + expect(described_class.completion_shell_parameter(:none, :bash, "/usr/bin/foo", env)).to be_nil |
| 58 | + end |
| 59 | + |
| 60 | + it "returns subcommand array for :typer format and sets env" do |
| 61 | + result = described_class.completion_shell_parameter(:typer, :bash, "/usr/bin/foo", env) |
| 62 | + expect(result).to eq(["--show-completion", "bash"]) |
| 63 | + expect(env["_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION"]).to eq("1") |
| 64 | + end |
| 65 | + |
| 66 | + it "maps :pwsh to 'powershell'" do |
| 67 | + expect(described_class.completion_shell_parameter(nil, :pwsh, "/usr/bin/foo", env)).to eq("powershell") |
| 68 | + end |
| 69 | + |
| 70 | + it "interpolates custom format string" do |
| 71 | + expect(described_class.completion_shell_parameter("--complete-", :zsh, "/usr/bin/foo", env)) |
| 72 | + .to eq("--complete-zsh") |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + describe ".generate_completion_output" do |
| 77 | + it "calls safe_popen_read with commands and shell parameter" do |
| 78 | + expect(Utils).to receive(:safe_popen_read).with( |
| 79 | + {}, "/usr/bin/foo", "completions", "bash", err: :err |
| 80 | + ).and_return("completion output") |
| 81 | + |
| 82 | + result = described_class.generate_completion_output( |
| 83 | + ["/usr/bin/foo", "completions"], "bash", {} |
| 84 | + ) |
| 85 | + |
| 86 | + expect(result).to eq("completion output") |
| 87 | + end |
| 88 | + |
| 89 | + it "flattens array shell parameters" do |
| 90 | + expect(Utils).to receive(:safe_popen_read).with( |
| 91 | + {}, "/usr/bin/foo", "completion", "zsh", err: :err |
| 92 | + ).and_return("output") |
| 93 | + |
| 94 | + described_class.generate_completion_output( |
| 95 | + ["/usr/bin/foo"], ["completion", "zsh"], {} |
| 96 | + ) |
| 97 | + end |
| 98 | + |
| 99 | + it "handles nil shell parameter" do |
| 100 | + expect(Utils).to receive(:safe_popen_read).with( |
| 101 | + {}, "/usr/bin/foo", err: :err |
| 102 | + ).and_return("output") |
| 103 | + |
| 104 | + described_class.generate_completion_output(["/usr/bin/foo"], nil, {}) |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments