summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorKyrylo Silin <silin@kyrylo.org>2019-05-26 10:42:52 +0300
committerKyrylo Silin <silin@kyrylo.org>2019-05-26 17:11:42 +0300
commit67b0b53f0bf9513d65fe7ad8df27e9da4a759c98 (patch)
treeb724d0c3b093488c450428b2588be692f49a4523 /spec
parent28cf3e9fa83ac5e602a18917fdb8b1539c06ca96 (diff)
downloadpry-67b0b53f0bf9513d65fe7ad8df27e9da4a759c98.tar.gz
Merge Pry::Terminal with Pry::Output
Fixes #1991 (Pry in a non-stdin/stdout PTY uses incorrect window size on non-JRuby platforms) `Pry::Terminal` was built without custom outputs in mind. We would always assume that `$stdout` is what the user wants. This contradicted the `output` config option. Thanks to `Pry::Output`, which we use internally, we can decorate the output that the user passes us with "size" methods. If we do that, we get improved output support for free, so that PTY's `slave` can be passed to Pry and would be able to determine its size correctly (example from #1991). I do suspect that there are still some gotchas. Some commands or portions of code may still be assuming that `$stdout` is the only possible option. This has to be addressed separately, in the scope of https://github.com/pry/pry/issues/1988. The more tests we add, the easier it will be to uncover those spots.
Diffstat (limited to 'spec')
-rw-r--r--spec/indent_spec.rb2
-rw-r--r--spec/output_spec.rb171
2 files changed, 172 insertions, 1 deletions
diff --git a/spec/indent_spec.rb b/spec/indent_spec.rb
index aa48379f..29c07a5f 100644
--- a/spec/indent_spec.rb
+++ b/spec/indent_spec.rb
@@ -5,7 +5,7 @@
# lines.
describe Pry::Indent do
before do
- @indent = Pry::Indent.new
+ @indent = Pry::Indent.new(Pry.new)
end
it 'should indent an array' do
diff --git a/spec/output_spec.rb b/spec/output_spec.rb
index 40ccddc9..d3f16dc6 100644
--- a/spec/output_spec.rb
+++ b/spec/output_spec.rb
@@ -194,4 +194,175 @@ RSpec.describe Pry::Output do
end
end
end
+
+ describe "#size" do
+ context "when the output is a tty and responds to winsize" do
+ before do
+ skip("io/console doesn't support JRuby") if Pry::Helpers::Platform.jruby?
+ expect(output).to receive(:tty?).and_return(true)
+ expect(output).to receive(:winsize).and_return([1, 1])
+ end
+
+ it "returns the io/console winsize" do
+ expect(subject.size).to eq([1, 1])
+ end
+ end
+
+ context "when the output is not a tty" do
+ before do
+ skip("io/console doesn't support JRuby") if Pry::Helpers::Platform.jruby?
+ expect(output).to receive(:tty?).and_return(false)
+ allow(ENV).to receive(:[])
+ end
+
+ context "and ENV has size info in ROWS and COLUMNS" do
+ before do
+ expect(ENV).to receive(:[]).with('ROWS').and_return(2)
+ expect(ENV).to receive(:[]).with('COLUMNS').and_return(2)
+ end
+
+ it "returns the ENV variable winsize" do
+ expect(subject.size).to eq([2, 2])
+ end
+ end
+
+ context "and ENV has size info in LINES and COLUMNS" do
+ before do
+ expect(ENV).to receive(:[]).with('LINES').and_return(3)
+ expect(ENV).to receive(:[]).with('COLUMNS').and_return(2)
+ end
+
+ it "returns ENV variable winsize" do
+ expect(subject.size).to eq([3, 2])
+ end
+ end
+ end
+
+ context "when the output is not a tty and no info in ENV" do
+ let(:readline) { Object.new }
+
+ before do
+ unless Pry::Helpers::Platform.jruby?
+ expect(output).to receive(:tty?).and_return(false)
+ end
+
+ allow(ENV).to receive(:[])
+
+ stub_const('Readline', readline)
+ end
+
+ context "when Readline's size has no zeroes" do
+ before do
+ expect(readline).to receive(:get_screen_size).and_return([1, 1])
+ end
+
+ it "returns the Readline winsize" do
+ expect(subject.size).to eq([1, 1])
+ end
+ end
+
+ context "when Readline's size has zero column" do
+ before do
+ expect(readline).to receive(:get_screen_size).and_return([1, 0])
+ end
+
+ it "returns the default size" do
+ expect(subject.size).to eq([27, 80])
+ end
+ end
+ end
+
+ context "when the output is not a tty, and no info in ENV and no Readline info" do
+ let(:readline) { Object.new }
+
+ before do
+ unless Pry::Helpers::Platform.jruby?
+ expect(output).to receive(:tty?).and_return(false)
+ end
+
+ allow(ENV).to receive(:[])
+ stub_const('Readline', readline)
+ expect(readline).to receive(:respond_to?)
+ .with(:get_screen_size).and_return(false)
+ end
+
+ context "and when there's ANSICON ENV variable" do
+ context "and when it can be matched" do
+ context "and when the size consists of positive integers" do
+ before do
+ expect(ENV).to receive(:[]).with('ANSICON').and_return('(5x5)')
+ end
+
+ it "returns the ansicon winsize" do
+ expect(subject.size).to eq([5, 5])
+ end
+ end
+
+ context "and when the size has a zero column" do
+ before do
+ expect(ENV).to receive(:[]).with('ANSICON').and_return('(0x0)')
+ end
+
+ it "returns the default winsize" do
+ expect(subject.size).to eq([27, 80])
+ end
+ end
+ end
+
+ context "and when it cannot be matched" do
+ before do
+ expect(ENV).to receive(:[]).with('ANSICON').and_return('5x5')
+ end
+
+ it "returns the default winsize" do
+ expect(subject.size).to eq([27, 80])
+ end
+ end
+ end
+
+ context "and when there's no ANSICON ENV variable" do
+ it "returns the default winsize" do
+ expect(subject.size).to eq([27, 80])
+ end
+ end
+ end
+ end
+
+ describe "#width" do
+ let(:readline) { Object.new }
+
+ before do
+ unless Pry::Helpers::Platform.jruby?
+ expect(output).to receive(:tty?).and_return(false)
+ end
+
+ allow(ENV).to receive(:[])
+ stub_const('Readline', readline)
+ expect(readline).to receive(:respond_to?)
+ .with(:get_screen_size).and_return(false)
+ end
+
+ it "returns the number of columns" do
+ expect(subject.width).to eq(80)
+ end
+ end
+
+ describe "#height" do
+ let(:readline) { Object.new }
+
+ before do
+ unless Pry::Helpers::Platform.jruby?
+ expect(output).to receive(:tty?).and_return(false)
+ end
+
+ allow(ENV).to receive(:[])
+ stub_const('Readline', readline)
+ expect(readline).to receive(:respond_to?)
+ .with(:get_screen_size).and_return(false)
+ end
+
+ it "returns the number of rows" do
+ expect(subject.height).to eq(27)
+ end
+ end
end