spec/pager_spec.rb in command_kit-0.1.0 vs spec/pager_spec.rb in command_kit-0.2.0

- old
+ new

@@ -17,21 +17,21 @@ context "when the PAGER env variable is set" do let(:pager) { 'foo' } subject { command_class.new(env: {'PAGER' => pager}) } - it "must set @pager to the PAGER env variable" do - expect(subject.instance_variable_get('@pager')).to eq(pager) + it "must set @pager_command to the PAGER env variable" do + expect(subject.instance_variable_get('@pager_command')).to eq(pager) end end context "when the PAGER env variable is not set" do context "but the PATH env variable is" do subject { command_class.new(env: {'PATH' => ENV['PATH']}) } it "must search PATH for one of the pagers" do - expect(subject.instance_variable_get('@pager')).to eq("less -r") + expect(subject.instance_variable_get('@pager_command')).to eq("less -r") end end end end @@ -44,23 +44,23 @@ subject.pager(&b) }.to yield_with_args(subject.stdout) end end - context "when @pager isn't initialized" do + context "when @pager_command isn't initialized" do before do - subject.instance_variable_set('@pager',nil) + subject.instance_variable_set('@pager_command',nil) end it "must yield stdout" do expect { |b| subject.pager(&b) }.to yield_with_args(subject.stdout) end end - context "when stdout is a TTY and @pager is initialized" do + context "when stdout is a TTY and @pager_command is initialized" do let(:pager) { 'less -r' } subject { command_class.new(env: {'PAGER' => pager}) } let(:pager_io) { double('less') } @@ -143,9 +143,74 @@ pager = double('pager') expect(subject).to receive(:pager).and_yield(pager) expect(pager).to receive(:puts).with(array) subject.print_or_page(array) + end + end + end + end + + describe "#pipe_to_pager" do + context "when @pager_command is initialized" do + let(:pager) { 'less' } + + subject do + command_class.new(env: {'PAGER' => pager}) + end + + context "and when given a single String" do + let(:command) { "find ." } + + it "must run the command but piped into the pager" do + expect(subject).to receive(:system).with("#{command} | #{pager}") + + subject.pipe_to_pager(command) + end + end + + context "and when given a String and additional arguments" do + let(:command) { 'find' } + let(:arguments) { %[. -name *.md] } + + let(:escaped_command) do + Shellwords.shelljoin([command,*arguments]) + end + + it "must shell escape the command and arguments" do + expect(subject).to receive(:system).with( + "#{escaped_command} | #{pager}" + ) + + subject.pipe_to_pager(command,*arguments) + end + end + end + + context "when @pager_command is not initialized" do + before do + subject.instance_variable_set('@pager_command',nil) + end + + let(:command) { 'find' } + let(:arguments) { %[. -name *.md] } + + it "must pass the command and any additional arguments to #system" do + expect(subject).to receive(:system).with(command,*arguments) + + subject.pipe_to_pager(command,*arguments) + end + + context "when one of the arguments is not a String" do + let(:command) { :find } + let(:arguments) { ['.', :"-name", "*.md"] } + + it "must convert the command to a String before calling #system" do + expect(subject).to receive(:system).with( + command.to_s, *arguments.map(&:to_s) + ) + + subject.pipe_to_pager(command,*arguments) end end end end end