Skip to content

Commit 05e4387

Browse files
committed
(FACT-3202) Add sep support to safe_readlines
Prior to this commit the safe_readlines wrapper did not support the sep argument as supported by readlines.
1 parent 7cced2e commit 05e4387

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/facter/util/file_helper.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ def safe_read(path, default_return = '')
1515
default_return
1616
end
1717

18-
def safe_readlines(path, default_return = [])
19-
return File.readlines(path, encoding: Encoding::UTF_8) if File.readable?(path)
18+
# rubocop:disable Style/SpecialGlobalVars
19+
def safe_readlines(path, default_return = [], sep = $/, chomp: false)
20+
return File.readlines(path, sep, chomp: chomp, encoding: Encoding::UTF_8) if File.readable?(path)
2021

2122
log_failed_to_read(path)
2223
default_return
2324
end
25+
# rubocop:enable Style/SpecialGlobalVars
2426

2527
# This previously acted as a helper method for versions of Ruby older
2628
# than 2.5, before Dir.children was added. As it isn't a private

spec/facter/util/file_helper_spec.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
let(:path) { '/Users/admin/file.txt' }
77
let(:entries) { ['file.txt', 'a'] }
88
let(:content) { 'file content' }
9+
# rubocop:disable Style/SpecialGlobalVars
10+
let(:sep) { $/ }
11+
# rubocop:enable Style/SpecialGlobalVars
912
let(:error_message) do
1013
"Facter::Util::FileHelper - #{Facter::CYAN}File at: /Users/admin/file.txt is not accessible.#{Facter::RESET}"
1114
end
@@ -114,7 +117,7 @@
114117

115118
describe '#safe_read_lines' do
116119
before do
117-
allow(File).to receive(:readlines).with(path, anything).and_return(array_content)
120+
allow(File).to receive(:readlines).with(path, sep, anything).and_return(array_content)
118121
end
119122

120123
context 'when successfully read the file lines' do
@@ -133,7 +136,7 @@
133136
it 'File.readlines is called with the correct path' do
134137
file_helper.safe_readlines(path)
135138

136-
expect(File).to have_received(:readlines).with(path, anything)
139+
expect(File).to have_received(:readlines).with(path, sep, anything)
137140
end
138141

139142
it "doesn't log anything" do

0 commit comments

Comments
 (0)