Skip to content

Commit

Permalink
Fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lsegal committed Nov 23, 2017
1 parent 10e84bd commit 3bcccf6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/yard/cli/yardoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ def output_options(opts)
opts.on('--asset FROM[:TO]', 'A file or directory to copy over to output ',
' directory after generating') do |asset|
re = %r{^(?:\.\./|/)}
from, to = *asset.split(':').map {|f| File.cleanpath(f) }
from, to = *asset.split(':').map {|f| File.cleanpath(f, true) }
to ||= from
if from =~ re || to =~ re
log.warn "Invalid file '#{asset}'"
Expand Down
5 changes: 3 additions & 2 deletions lib/yard/core_ext/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ def self.relative_path(from, to)
# @example Clean a path
# File.cleanpath('a/b//./c/../e') # => "a/b/e"
# @param [String] path the path to clean
# @param [Boolean] rel_root allows relative path above root value
# @return [String] the sanitized path
def self.cleanpath(path)
def self.cleanpath(path, rel_root = false)
path = path.split(SEPARATOR)
path = path.inject([]) do |acc, comp|
next acc if comp == RELATIVE_SAMEDIR
if comp == RELATIVE_PARENTDIR && !acc.empty? && acc.last != RELATIVE_PARENTDIR
acc.pop
next acc
elsif comp == RELATIVE_PARENTDIR && acc.empty?
elsif !rel_root && comp == RELATIVE_PARENTDIR && acc.empty?
next acc
end
acc << comp
Expand Down
4 changes: 4 additions & 0 deletions spec/core_ext/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
expect(File.cleanpath('A/B/C/D/..')).to eq "A/B/C"
end

it "allows '../' at the beginning if rel_root=true" do
expect(File.cleanpath('A/../../B', true)).to eq '../B'
end

it "does not allow relative path above root" do
expect(File.cleanpath('A/../../../../../D')).to eq "D"
end
Expand Down

0 comments on commit 3bcccf6

Please sign in to comment.