提交 7e2bbe10 编写于 作者: M Mislav Marohnić

Merge branch 'git-editor'

......@@ -437,6 +437,7 @@ module Hub
def git_editor
# possible: ~/bin/vi, $SOME_ENVIRONMENT_VARIABLE, "C:\Program Files\Vim\gvim.exe" --nofork
editor = git_command 'var GIT_EDITOR'
editor.gsub!(/\$(\w+|\{\w+\})/) { ENV[$1.tr('{}', '')] }
editor = ENV[$1] if editor =~ /^\$(\w+)$/
editor = File.expand_path editor if (editor =~ /^[~.]/ or editor.index('/')) and editor !~ /["']/
# avoid shellsplitting "C:\Program Files"
......
require 'helper'
class ContextTest < Test::Unit::TestCase
class Context
include Hub::Context
def initialize(&block)
@git_reader = Hub::Context::GitReader.new('git', &block)
end
public :git_editor
end
attr_reader :context
def setup
super
@stubs = {}
@context = Context.new do |_, cmd|
@stubs.fetch(cmd)
end
end
def test_editor
stub_command_output 'var GIT_EDITOR', 'vim'
assert_equal %w'vim', context.git_editor
end
def test_editor_with_argument
stub_command_output 'var GIT_EDITOR', 'subl -w'
assert_equal %w'subl -w', context.git_editor
end
def test_editor_with_spaces
stub_command_output 'var GIT_EDITOR', '"my editor" -w arg2'
assert_equal %w'my\ editor -w arg2', context.git_editor
end
def test_editor_with_tilde
stub_command_output 'var GIT_EDITOR', '~/bin/vi'
with_env('HOME', '/home/mislav') do
assert_equal %w'/home/mislav/bin/vi', context.git_editor
end
end
def test_editor_with_env_variable
stub_command_output 'var GIT_EDITOR', '$EDITOR'
with_env('EDITOR', 'subl -w') do
assert_equal %w'subl -w', context.git_editor
end
end
def test_editor_with_embedded_env_variable
stub_command_output 'var GIT_EDITOR', '$EDITOR -w'
with_env('EDITOR', 'subl') do
assert_equal %w'subl -w', context.git_editor
end
end
def test_editor_with_curly_brackets_embedded_env_variable
stub_command_output 'var GIT_EDITOR', 'my${EDITOR}2 -w'
with_env('EDITOR', 'subl') do
assert_equal %w'mysubl2 -w', context.git_editor
end
end
private
def stub_command_output(cmd, value)
@stubs[cmd] = value.nil? ? nil : value.to_s
end
def with_env(name, value)
dir, ENV[name] = ENV[name], value
yield
ensure
ENV[name] = dir
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册