From d07c8e2388c2e4a902caf95ac336a3404eaa1508 Mon Sep 17 00:00:00 2001 From: Andy Lindeman Date: Sat, 17 Aug 2013 01:21:52 +0000 Subject: [PATCH] Adds a newline at the very end of the authorized_keys file * This is important because it allows new keys to be added via `cat ... >> ~/.ssh/authorized_keys` or `echo ... >> ~/.ssh/authorized_keys` Without a newline at the end of the file `>>` will concat text to the end of the last key, mangling the file. * These modifications should handle files that currently do *and* do not have newlines at the very end, though it will always write files that *do* have newlines at the very end. --- lib/github/auth/keys_file.rb | 6 +++--- spec/unit/github/auth/keys_file_spec.rb | 16 +++++++--------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/github/auth/keys_file.rb b/lib/github/auth/keys_file.rb index fda83f3..27be074 100644 --- a/lib/github/auth/keys_file.rb +++ b/lib/github/auth/keys_file.rb @@ -16,8 +16,8 @@ def write!(keys) Array(keys).each do |key| unless keys_file_content.include? key.key append_keys_file do |keys_file| - keys_file.write "\n" unless keys_file_content.empty? - keys_file.write key + keys_file.write "\n" unless keys_file_content.empty? || keys_file_content.end_with?("\n") + keys_file.write "#{key}\n" end end end @@ -57,7 +57,7 @@ def keys_file_content_without(keys) content.gsub! /#{Regexp.escape key.key}( .*)?$\n?/, '' end - content.strip! + content << "\n" unless content.empty? || content.end_with?("\n") end end end diff --git a/spec/unit/github/auth/keys_file_spec.rb b/spec/unit/github/auth/keys_file_spec.rb index 4edddf4..c450eb1 100644 --- a/spec/unit/github/auth/keys_file_spec.rb +++ b/spec/unit/github/auth/keys_file_spec.rb @@ -49,12 +49,10 @@ end end - it 'does not leave a blank line' do + it 'includes a newline after the last key' do subject.write! keys - expect( - keys_file.readlines.select { |line| line =~ /^$\n/ } - ).to be_empty + expect(keys_file.read).to end_with("\n") end end @@ -80,6 +78,7 @@ before do keys_file.write existing_keys.join("\n") + keys_file.write "\n" keys_file.rewind end @@ -132,6 +131,7 @@ before do keys_file.write keys.join("\n") + keys_file.write "\n" keys_file.rewind end @@ -152,12 +152,10 @@ end end - it 'does not leave a blank line' do - subject.delete! key + it 'includes a newline after the last key' do + subject.write! keys - expect( - keys_file.readlines.select { |line| line =~ /^$\n/ } - ).to be_empty + expect(keys_file.read).to end_with("\n") end end