Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌈 Fixes a bug in the calculation of the print_wrapped method #719

Merged
merged 1 commit into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/thor/shell/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ def print_wrapped(message, options = {})
paras = message.split("\n\n")

paras.map! do |unwrapped|
counter = 0
unwrapped.split(" ").inject do |memo, word|
words = unwrapped.split(" ")
counter = words.first.length
words.inject do |memo, word|
word = word.gsub(/\n\005/, "\n").gsub(/\005/, "\n")
counter = 0 if word.include? "\n"
if (counter + word.length + 1) < width
Expand Down
40 changes: 40 additions & 0 deletions spec/shell/basic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,46 @@ def shell
end
end

describe "#print_wrapped" do
let(:message) do
"Creates a back-up of the given folder by compressing it in a .tar.gz\n"\
"file and then uploading it to the configured Amazon S3 Bucket.\n\n"\
"It does not verify the integrity of the generated back-up."
end

before do
allow(ENV).to receive(:[]).with("THOR_COLUMNS").and_return(80)
end

context "without indentation" do
subject(:wrap_text) { described_class.new.print_wrapped(message) }

let(:expected_output) do
"Creates a back-up of the given folder by compressing it in a .tar.gz file and\n"\
"then uploading it to the configured Amazon S3 Bucket.\n\n"\
"It does not verify the integrity of the generated back-up.\n"
end

it "properly wraps the text around the 80th column" do
expect { wrap_text }.to output(expected_output).to_stdout
end
end

context "with indentation" do
subject(:wrap_text) { described_class.new.print_wrapped(message, :indent => 4) }

let(:expected_output) do
" Creates a back-up of the given folder by compressing it in a .tar.gz file\n"\
" and then uploading it to the configured Amazon S3 Bucket.\n\n"\
" It does not verify the integrity of the generated back-up.\n"
end

it "properly wraps the text around the 80th column" do
expect { wrap_text }.to output(expected_output).to_stdout
end
end
end

describe "#say_status" do
it "prints a message to the user with status" do
expect($stdout).to receive(:print).with(" create ~/.thor/command.thor\n")
Expand Down