Commit 4de0ee3b authored by Jacob Vosmaer's avatar Jacob Vosmaer

Use Tempfile instead of `sed -i`

The syntax for `sed -i` is incompatible between GNU sed and
BSD sed. By Tempfile from the Ruby standard library we can
avoid using the `-i` option of sed.
parent c4c0885a
require 'tempfile'
require_relative 'gitlab_config'
require_relative 'gitlab_logger'
......@@ -35,8 +36,10 @@ class GitlabKeys
def rm_key
$logger.info "Removing key #{@key_id}"
cmd = "sed -i '/shell #{@key_id}\"/d' #{auth_file}"
system(cmd)
Tempfile.open('authorized_keys') do |temp|
cmd = "sed '/shell #{@key_id}\"/d' #{auth_file} > #{temp.path} && mv #{temp.path} #{auth_file}"
system(cmd)
end
end
def clear
......
......@@ -31,9 +31,12 @@ describe GitlabKeys do
describe :rm_key do
let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
let(:temp_file) { mock(:temp_file, path: 'tmp_path') }
before { Tempfile.should_receive(:open).and_yield(temp_file) }
it "should receive valid cmd" do
valid_cmd = "sed -i '/shell key-741\"/d' #{GitlabConfig.new.auth_file}"
auth_file = GitlabConfig.new.auth_file
valid_cmd = "sed '/shell key-741\"/d' #{auth_file} > tmp_path && mv tmp_path #{auth_file}"
gitlab_keys.should_receive(:system).with(valid_cmd)
gitlab_keys.send :rm_key
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment