From 0bcfdedbb41f92381f562efb2d2d28bd96f1be56 Mon Sep 17 00:00:00 2001 From: Yuri Smirnov Date: Thu, 12 Dec 2024 13:58:11 +0300 Subject: [PATCH] preserve newlines --- lib/lamian/log_device.rb | 3 ++- spec/lamian/log_device_spec.rb | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/lamian/log_device.rb b/lib/lamian/log_device.rb index b7fa305..77bc32f 100644 --- a/lib/lamian/log_device.rb +++ b/lib/lamian/log_device.rb @@ -26,7 +26,8 @@ def string # :nodoc: def truncate(msg) return msg unless msg.size > max_log_length - suffix = "..." + suffix = +"..." + suffix << "\n" if msg.end_with?("\n") msg = msg[0, max_log_length - suffix.size] "#{msg}#{suffix}" diff --git a/spec/lamian/log_device_spec.rb b/spec/lamian/log_device_spec.rb index fce8298..5ba7de8 100644 --- a/spec/lamian/log_device_spec.rb +++ b/spec/lamian/log_device_spec.rb @@ -4,9 +4,9 @@ subject(:logdev) { described_class.new(max_log_lines: 5, max_log_length: 20) } it "saves log" do - logdev.write("Hello ") - logdev.write("world!") - expect(logdev.string).to eq("Hello world!") + logdev.write("Hello\n") + logdev.write("world!\n") + expect(logdev.string).to eq("Hello\nworld!\n") end it "only stores 5 latest log lines" do @@ -14,8 +14,17 @@ expect(logdev.string).to eq("45678") end - it "truncates long lines" do - logdev.write("Hello world, this is me!") - expect(logdev.string).to eq("Hello world, this...") + context "long log lines" do + it "truncates log" do + logdev.write("Hello world, this is me!") + expect(logdev.string).to eq("Hello world, this...") + end + + context "line with newline in the end" do + it "truncates log and adds newline" do + logdev.write("Hello world, this is me!\n") + expect(logdev.string).to eq("Hello world, thi...\n") + end + end end end