diff options
-rw-r--r-- | lib/gitlab/string_path.rb | 15 | ||||
-rw-r--r-- | spec/lib/gitlab/string_path_spec.rb | 17 |
2 files changed, 28 insertions, 4 deletions
diff --git a/lib/gitlab/string_path.rb b/lib/gitlab/string_path.rb index 9948502e8ea..4d024b3ff73 100644 --- a/lib/gitlab/string_path.rb +++ b/lib/gitlab/string_path.rb @@ -7,6 +7,7 @@ module Gitlab # class StringPath attr_reader :path, :universe + attr_accessor :name def initialize(path, universe, metadata = []) @path = sanitize(path) @@ -35,7 +36,7 @@ module Gitlab end def has_parent? - @universe.include?(@path.sub(basename, '')) + nodes > 1 end def parent @@ -48,7 +49,7 @@ module Gitlab end def name - @path.split(::File::SEPARATOR).last + @name || @path.split(::File::SEPARATOR).last end def has_descendants? @@ -75,7 +76,11 @@ module Gitlab end 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 def files @@ -88,6 +93,10 @@ module Gitlab @metadata[index] end + def nodes + @path.count('/') + (file? ? 1 : 0) + end + def ==(other) @path == other.path && @universe == other.universe end diff --git a/spec/lib/gitlab/string_path_spec.rb b/spec/lib/gitlab/string_path_spec.rb index 0ef2155cb9b..a54bf109c80 100644 --- a/spec/lib/gitlab/string_path_spec.rb +++ b/spec/lib/gitlab/string_path_spec.rb @@ -30,7 +30,7 @@ describe Gitlab::StringPath do it { is_expected.to be_absolute } it { is_expected.to_not be_relative } 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 } describe '#basename' do @@ -140,6 +140,21 @@ describe Gitlab::StringPath do 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 let(:universe) do ['path/', 'path/file1', 'path/file2'] |