diff options
-rw-r--r-- | lib/gitlab/string_path.rb | 16 | ||||
-rw-r--r-- | spec/lib/gitlab/string_path_spec.rb | 53 |
2 files changed, 57 insertions, 12 deletions
diff --git a/lib/gitlab/string_path.rb b/lib/gitlab/string_path.rb index 3add56d2213..a2dc63db2f2 100644 --- a/lib/gitlab/string_path.rb +++ b/lib/gitlab/string_path.rb @@ -43,12 +43,24 @@ module Gitlab new(@path.sub(basename, '')) end + def descendants + return [] unless directory? + children = @universe.select { |entry| entry =~ /^#{@path}.+/ } + children.map { |path| new(path) } + end + + def children + descendants.select { |descendant| descendant.parent == self } + end + def directories - raise NotImplementedError + return [] unless directory? + children.select { |child| child.directory? } end def files - raise NotImplementedError + return [] unless directory? + children.select { |child| child.file? } end def basename diff --git a/spec/lib/gitlab/string_path_spec.rb b/spec/lib/gitlab/string_path_spec.rb index d086a011763..7818e340726 100644 --- a/spec/lib/gitlab/string_path_spec.rb +++ b/spec/lib/gitlab/string_path_spec.rb @@ -16,7 +16,11 @@ describe Gitlab::StringPath do end def path(example) - described_class.new(example.metadata[:path], universe) + string_path(example.metadata[:path]) + end + + def string_path(string_path) + described_class.new(string_path, universe) end describe '/file/with/absolute_path', path: '/file/with/absolute_path' do @@ -47,14 +51,6 @@ describe Gitlab::StringPath do it { is_expected.to have_parent } - describe '#files' do - subject { |example| path(example).files } - - pending { is_expected.to all(be_an_instance_of described_class) } - pending { is_expected.to be eq [Gitlab::StringPath.new('path/dir_1/file_1', universe), - Gitlab::StringPath.new('path/dir_1/file_b', universe)] } - end - describe '#basename' do subject { |example| path(example).basename } @@ -64,7 +60,44 @@ describe Gitlab::StringPath do describe '#parent' do subject { |example| path(example).parent } - it { is_expected.to eq Gitlab::StringPath.new('path/', universe) } + it { is_expected.to eq string_path('path/') } + end + + describe '#descendants' do + subject { |example| path(example).descendants } + + it { is_expected.to be_an_instance_of Array } + it { is_expected.to all(be_an_instance_of described_class) } + it { is_expected.to contain_exactly string_path('path/dir_1/file_1'), + string_path('path/dir_1/file_b'), + string_path('path/dir_1/subdir/'), + string_path('path/dir_1/subdir/subfile') } + end + + describe '#children' do + subject { |example| path(example).children } + + it { is_expected.to all(be_an_instance_of described_class) } + it { is_expected.to contain_exactly string_path('path/dir_1/file_1'), + string_path('path/dir_1/file_b'), + string_path('path/dir_1/subdir/') } + end + + describe '#files' do + subject { |example| path(example).files } + + it { is_expected.to all(be_file) } + it { is_expected.to all(be_an_instance_of described_class) } + it { is_expected.to contain_exactly string_path('path/dir_1/file_1'), + string_path('path/dir_1/file_b') } + end + + describe '#directories' do + subject { |example| path(example).directories } + + it { is_expected.to all(be_directory) } + it { is_expected.to all(be_an_instance_of described_class) } + it { is_expected.to contain_exactly string_path('path/dir_1/subdir/') } end end end |