diff options
-rw-r--r-- | lib/gitlab/string_path.rb | 11 | ||||
-rw-r--r-- | spec/lib/gitlab/string_path_spec.rb | 11 |
2 files changed, 19 insertions, 3 deletions
diff --git a/lib/gitlab/string_path.rb b/lib/gitlab/string_path.rb index 3aa6200b572..3add56d2213 100644 --- a/lib/gitlab/string_path.rb +++ b/lib/gitlab/string_path.rb @@ -35,11 +35,12 @@ module Gitlab end def has_parent? - raise NotImplementedError + @universe.include?(@path.sub(basename, '')) end def parent - raise NotImplementedError + return nil unless has_parent? + new(@path.sub(basename, '')) end def directories @@ -58,5 +59,11 @@ module Gitlab def ==(other) @path == other.path && @universe == other.universe end + + private + + def new(path) + self.class.new(path, @universe) + end end end diff --git a/spec/lib/gitlab/string_path_spec.rb b/spec/lib/gitlab/string_path_spec.rb index 6e75e1f3ced..86e48f6ee0b 100644 --- a/spec/lib/gitlab/string_path_spec.rb +++ b/spec/lib/gitlab/string_path_spec.rb @@ -19,6 +19,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 } describe '#basename' do subject { described_class.new('/file/with/absolute_path', universe).basename } @@ -32,9 +33,13 @@ describe Gitlab::StringPath do it { is_expected.to be_directory } it { is_expected.to be_relative } + it { is_expected.to_not have_parent } end describe 'path/dir_1/' do + subject { described_class.new('path/dir_1/', universe) } + it { is_expected.to have_parent } + describe '#files' do subject { described_class.new('path/dir_1/', universe).files } @@ -45,8 +50,12 @@ describe Gitlab::StringPath do describe '#basename' do subject { described_class.new('path/dir_1/', universe).basename } - it { is_expected.to eq 'dir_1/' } end + + describe '#parent' do + subject { described_class.new('path/dir_1/', universe).parent } + it { is_expected.to eq Gitlab::StringPath.new('path/', universe) } + end end end |