Skip to content

Commit 1c24e4d

Browse files
authored
Merge pull request #97 from tmm1/tag-relative-compat
Support `--tag-relative=always|never`
2 parents 5a4c7e6 + 0d97dae commit 1c24e4d

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

lib/ripper-tags.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ def self.option_parser(options)
9494
opts.on("-a", "--append[=yes|no]", "Append tags to existing file") do |value|
9595
options.tag_file_append = value != "no"
9696
end
97-
opts.on("--tag-relative[=OPTIONAL]", "Make file paths relative to the directory of the tag file") do |value|
98-
options.tag_relative = value != "no"
97+
opts.on("--tag-relative[=yes|no|always|never]", "Make file paths relative to the directory of the tag file") do |value|
98+
options.tag_relative = value || true
9999
end
100100
opts.on("-L", "--input-file=FILE", "File to read paths to process trom (use `-` for stdin)") do |file|
101101
options.input_file = file
@@ -194,6 +194,12 @@ def self.process_args(argv, run = method(:run))
194194
options.format ||= File.basename(options.tag_file_name) == 'TAGS' ? 'emacs' : 'vim'
195195
options.tag_relative = options.format == "emacs" if options.tag_relative.nil?
196196

197+
case options.tag_relative
198+
when true, false, 'yes', 'no', 'always', 'never'
199+
else
200+
raise OptionParser::InvalidOption, 'unsupported value for --tag-relative: %p' % options.tag_relative
201+
end
202+
197203
return run.call(options)
198204
end
199205
end

lib/ripper-tags/default_formatter.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,23 @@ def with_output
5656
end
5757

5858
def tag_file_dir
59-
@tag_file_dir ||= Pathname.new(options.tag_file_name).dirname.expand_path
59+
@tag_file_dir ||= stdout? ?
60+
Pathname.pwd :
61+
Pathname.new(options.tag_file_name).dirname.expand_path
6062
end
6163

6264
def relative_path(tag)
6365
path = tag.fetch(:path)
64-
if options.tag_relative && !stdout? && path.index('/') != 0
65-
Pathname.new(path).expand_path.relative_path_from(tag_file_dir).to_s
66-
else
67-
path
66+
case options.tag_relative
67+
when true, 'yes', 'always'
68+
filepath = Pathname.new(path)
69+
if options.tag_relative == 'always' || filepath.relative?
70+
path = filepath.expand_path.relative_path_from(tag_file_dir).to_s
71+
end
72+
when 'never'
73+
path = File.expand_path(path)
6874
end
75+
path
6976
end
7077

7178
def constant?(tag)

test/test_cli.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,26 @@ def test_tag_relative_off_by_default
118118
def test_tag_relative_on
119119
options = process_args(%w[--tag-relative hello.rb])
120120
assert_equal true, options.tag_relative
121-
assert_equal %w[hello.rb], options.files
122121
end
123122

124123
def test_tag_relative_explicit_yes
125124
options = process_args(%w[-R --tag-relative=yes])
126-
assert_equal true, options.tag_relative
125+
assert_equal 'yes', options.tag_relative
127126
end
128127

129128
def test_tag_relative_explicit_no
130129
options = process_args(%w[-R --tag-relative=no])
131-
assert_equal false, options.tag_relative
130+
assert_equal 'no', options.tag_relative
131+
end
132+
133+
def test_tag_relative_explicit_always
134+
options = process_args(%w[-R --tag-relative=always])
135+
assert_equal 'always', options.tag_relative
136+
end
137+
138+
def test_tag_relative_explicit_never
139+
options = process_args(%w[-R --tag-relative=never])
140+
assert_equal 'never', options.tag_relative
132141
end
133142

134143
def test_tag_relative_on_for_emacs

test/test_formatters.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,24 @@ def test_relative_with_common_prefix
218218
assert_equal 'to/script.rb', formatter.relative_path(tag)
219219
end
220220

221+
def test_relative_with_absolute_source_path
222+
formatter = formatter_for(:format => 'custom', :tag_file_name => '/tmp/tags', :tag_relative => true)
223+
tag = build_tag(:path => '/path/to/script.rb')
224+
assert_equal '/path/to/script.rb', formatter.relative_path(tag)
225+
end
226+
227+
def test_relative_always_with_absolute_source_path
228+
formatter = formatter_for(:format => 'custom', :tag_file_name => '/tmp/tags', :tag_relative => 'always')
229+
tag = build_tag(:path => '/path/to/script.rb')
230+
assert_equal '../path/to/script.rb', formatter.relative_path(tag)
231+
end
232+
233+
def test_relative_never_with_relative_source_path
234+
formatter = formatter_for(:format => 'custom', :tag_file_name => 'tags', :tag_relative => 'never')
235+
tag = build_tag(:path => 'path/to/script.rb')
236+
assert_equal "#{Dir.pwd}/path/to/script.rb", formatter.relative_path(tag)
237+
end
238+
221239
def test_no_relative
222240
formatter = formatter_for(:format => 'custom', :tag_file_name => '.git/tags')
223241
tag = build_tag(:path => 'path/to/script.rb')

0 commit comments

Comments
 (0)