Commit f948c007 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Do not depend on universe when checking parent in `StringPath`

parent a7f99b67
...@@ -7,6 +7,7 @@ module Gitlab ...@@ -7,6 +7,7 @@ module Gitlab
# #
class StringPath class StringPath
attr_reader :path, :universe attr_reader :path, :universe
attr_accessor :name
def initialize(path, universe, metadata = []) def initialize(path, universe, metadata = [])
@path = sanitize(path) @path = sanitize(path)
...@@ -35,7 +36,7 @@ module Gitlab ...@@ -35,7 +36,7 @@ module Gitlab
end end
def has_parent? def has_parent?
@universe.include?(@path.sub(basename, '')) nodes > 1
end end
def parent def parent
...@@ -48,7 +49,7 @@ module Gitlab ...@@ -48,7 +49,7 @@ module Gitlab
end end
def name def name
@path.split(::File::SEPARATOR).last @name || @path.split(::File::SEPARATOR).last
end end
def has_descendants? def has_descendants?
...@@ -75,7 +76,11 @@ module Gitlab ...@@ -75,7 +76,11 @@ module Gitlab
end end
def directories! def directories!
@path =~ %r{^\./[^/]/} ? directories.prepend(parent) : directories return directories unless has_parent? && directory?
dotted_parent = parent
dotted_parent.name = '..'
directories.prepend(dotted_parent)
end end
def files def files
...@@ -88,6 +93,10 @@ module Gitlab ...@@ -88,6 +93,10 @@ module Gitlab
@metadata[index] @metadata[index]
end end
def nodes
@path.count('/') + (file? ? 1 : 0)
end
def ==(other) def ==(other)
@path == other.path && @universe == other.universe @path == other.path && @universe == other.universe
end end
......
...@@ -30,7 +30,7 @@ describe Gitlab::StringPath do ...@@ -30,7 +30,7 @@ describe Gitlab::StringPath do
it { is_expected.to be_absolute } it { is_expected.to be_absolute }
it { is_expected.to_not be_relative } it { is_expected.to_not be_relative }
it { is_expected.to be_file } it { is_expected.to be_file }
it { is_expected.to_not have_parent } it { is_expected.to have_parent }
it { is_expected.to_not have_descendants } it { is_expected.to_not have_descendants }
describe '#basename' do describe '#basename' do
...@@ -140,6 +140,21 @@ describe Gitlab::StringPath do ...@@ -140,6 +140,21 @@ describe Gitlab::StringPath do
end end
end end
describe '#nodes', path: './' do
subject { |example| path(example).nodes }
it { is_expected.to eq 1 }
end
describe '#nodes', path: './test' do
subject { |example| path(example).nodes }
it { is_expected.to eq 2 }
end
describe '#nodes', path: './test/' do
subject { |example| path(example).nodes }
it { is_expected.to eq 2 }
end
describe '#metadata' do describe '#metadata' do
let(:universe) do let(:universe) do
['path/', 'path/file1', 'path/file2'] ['path/', 'path/file1', 'path/file2']
......
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