1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
# frozen_string_literal: true
RSpec.describe Pry::CLI do
before { described_class.reset }
describe ".add_options" do
it "returns self" do
expect(described_class.add_options).to eq(described_class)
end
context "when options is nil and a block is provided" do
before { described_class.options = nil }
it "sets the block as options" do
block = proc {}
described_class.add_options(&block)
expect(described_class.options).to eql(block)
end
end
context "when options were previously set" do
it "overwrites the options proc that executes the provided block" do
described_class.options = proc {}
executed = false
described_class.add_options { executed = true }
described_class.options.call
expect(executed).to be_truthy
end
it "overwrites the options proc that executes original options" do
original_executed = false
described_class.options = proc { original_executed = true }
described_class.add_options {}
described_class.options.call
expect(original_executed).to be_truthy
end
end
end
describe ".add_plugin_options" do
it "returns self" do
expect(described_class.add_plugin_options).to eq(described_class)
end
it "loads cli options of plugins" do
plugin_mock = double
expect(plugin_mock).to receive(:load_cli_options)
plugins = { 'pry-testplugin' => plugin_mock }
expect(Pry).to receive(:plugins).and_return(plugins)
described_class.add_plugin_options
end
end
describe ".add_option_processor" do
it "returns self" do
expect(described_class.add_option_processor {}).to eq(described_class)
end
it "adds an option processor" do
option_processor = proc {}
described_class.add_option_processor(&option_processor)
expect(described_class.option_processors).to eql([option_processor])
end
end
describe ".parse_options" do
context "when option exists" do
before { described_class.options = proc { on(:v, 'test') } }
it "removes the existing option from ARGV" do
argv = %w[filename -v]
described_class.parse_options(argv)
expect(argv).not_to include('-v')
end
it "initializes session setup" do
expect(Pry).to receive(:initial_session_setup)
described_class.parse_options(%w[-v])
end
it "finalizes session setup" do
expect(Pry).to receive(:final_session_setup)
described_class.parse_options(%w[-v])
end
end
context "when multiple options exist" do
it "processes only called options" do
processor_a_called = false
processor_b_called = false
processor_c_called = false
described_class.options = proc do
on('option-a', 'test a') { processor_a_called = true }
on('option-b', 'test b') { processor_b_called = true }
on('option-c', 'test c') { processor_c_called = true }
end
described_class.parse_options(%w[--option-a --option-b])
expect(processor_a_called).to be_truthy
expect(processor_b_called).to be_truthy
expect(processor_c_called).to be_falsey
end
end
context "when option doesn't exist" do
it "raises error" do
expect { described_class.parse_options(['--nothing']) }
.to raise_error(Pry::CLI::NoOptionsError)
end
end
context "when argv is passed with a dash (-)" do
before { described_class.options = proc {} }
it "sets everything after the dash as input args" do
argv = %w[filename - foo bar]
described_class.parse_options(argv)
expect(described_class.input_args).to eq(%w[foo bar])
end
end
context "when argv is passed with a double dash (--)" do
before { described_class.options = proc {} }
it "sets everything after the double dash as input args" do
argv = %w[filename -- foo bar]
described_class.parse_options(argv)
expect(described_class.input_args).to eq(%w[foo bar])
end
end
context "when invalid option is provided" do
before { described_class.options = proc { on(:valid, 'valid') } }
it "exits program" do
expect(Kernel).to receive(:exit)
expect(STDOUT).to receive(:puts)
described_class.parse_options(%w[--invalid])
end
end
end
describe ".start" do
before do
# Don't start Pry session in the middle of tests.
allow(Pry).to receive(:start)
described_class.options = proc {}
end
it "sets Pry.cli to true" do
opts = described_class.parse_options(%w[])
described_class.start(opts)
expect(Pry.cli).to be_truthy
end
context "when the help option is provided" do
before { described_class.options = proc { on(:help, 'help') } }
it "exits" do
expect(Kernel).to receive(:exit)
opts = described_class.parse_options(%w[--help])
described_class.start(opts)
end
end
context "when the context option is provided" do
before { described_class.options = proc { on(:context=, 'context') } }
it "initializes session setup" do
expect(Pry).to receive(:initial_session_setup).twice
opts = described_class.parse_options(%w[--context=Object])
described_class.start(opts)
end
it "finalizes session setup" do
expect(Pry).to receive(:final_session_setup).twice
opts = described_class.parse_options(%w[--context=Object])
described_class.start(opts)
end
it "starts Pry in the provided context" do
expect(Pry).to receive(:start).with(
instance_of(Binding), input: instance_of(StringIO)
) do |binding, _opts|
expect(binding.eval('self')).to be_an(Object)
end
opts = described_class.parse_options(%w[--context=Object])
described_class.start(opts)
end
end
context "when the context option is not provided" do
before { described_class.options = proc {} }
it "starts Pry in the top level" do
expect(Pry).to receive(:start).with(
instance_of(Binding), input: instance_of(StringIO)
) do |binding, _opts|
expect(binding.eval('self')).to eq(Pry.main)
end
opts = described_class.parse_options(%w[])
described_class.start(opts)
end
end
context "when there are some input args" do
before { described_class.options = proc {} }
it "loads files through repl and exits" do
expect(Pry).to receive(:load_file_through_repl).with(match(%r{/foo}))
expect(Kernel).to receive(:exit)
opts = described_class.parse_options(%w[foo])
described_class.start(opts)
end
end
context "when 'pry' is passed as an input arg" do
before { described_class.options = proc {} }
it "does not load files through repl" do
expect(Pry).not_to receive(:load_file_through_repl)
opts = described_class.parse_options(%w[pry])
described_class.start(opts)
end
end
end
end
|