Skip to content

Commit

Permalink
Improve method to sanitize values
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Jul 3, 2017
1 parent 2272dfb commit 3e81ef3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/modules/NtpClient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -989,11 +989,18 @@ def AutoPackages

# Remove blank spaces in values
#
# @note comments are kept untouched to avoid possible errors with
# inline comments that could produce entries like: trustedkey 1# comment
# @note to avoid augeas parsing errors, comments should be sanitized by
# removing blank spaces at the beginning and adding line break.
def sanitize_record(record)
sanitized = record.dup
sanitized.each_key { |k| sanitized[k].strip! unless k.include?("comment") }
sanitized.each do |key, value|
if key.include?("comment")
value.sub!(/^ */, "")
value << "\n" unless value.include?("\n")
elsif value.respond_to?(:strip!)
value.strip!
end
end
sanitized
end

Expand Down
4 changes: 2 additions & 2 deletions test/ntp_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
expect(record["address"]).to eq "0.opensuse.pool.ntp.org"
end

it "keeps comments untouched" do
it "sanitizes comments by removing blank spaces at the beginning and adding line break" do
record = subject.ntp_records.first
expect(record["comment"]).to eq " # a comment with spaces "
expect(record["comment"]).to eq "# a comment with spaces \n"
end

it "reads the list of peers" do
Expand Down

0 comments on commit 3e81ef3

Please sign in to comment.