From b19e958d86f5363057f006c8dbf9a8e8762618b9 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 22 Dec 2015 11:05:22 +0100 Subject: [PATCH] Add support for parent directories in `StringPath` This support is not completed though, as parent directory that is first in collection returned by `directories!` is not iterable yet. --- .../artifacts/_tree_directory.html.haml | 4 ++-- app/views/projects/artifacts/browse.html.haml | 2 +- lib/gitlab/string_path.rb | 10 +++++++++- spec/lib/gitlab/string_path_spec.rb | 18 ++++++++++++++---- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/views/projects/artifacts/_tree_directory.html.haml b/app/views/projects/artifacts/_tree_directory.html.haml index ec68c45865e..481a2cb072f 100644 --- a/app/views/projects/artifacts/_tree_directory.html.haml +++ b/app/views/projects/artifacts/_tree_directory.html.haml @@ -1,6 +1,6 @@ %tr{ class: 'tree-item' } %td.tree-item-file-name - = tree_icon('folder', '755', directory.basename) + = tree_icon('folder', '755', directory.name) %span.str-truncated - = link_to directory.basename, browse_namespace_project_build_artifacts_path(@project.namespace, @project, @build, path: directory.path) + = link_to directory.name, browse_namespace_project_build_artifacts_path(@project.namespace, @project, @build, path: directory.path) %td diff --git a/app/views/projects/artifacts/browse.html.haml b/app/views/projects/artifacts/browse.html.haml index ddca02d2bed..141fea9a6d4 100644 --- a/app/views/projects/artifacts/browse.html.haml +++ b/app/views/projects/artifacts/browse.html.haml @@ -16,5 +16,5 @@ %tr %th Name %th Download - = render partial: 'tree_directory', collection: @path.directories, as: :directory + = render partial: 'tree_directory', collection: @path.directories!, as: :directory = render partial: 'tree_file', collection: @path.files, as: :file diff --git a/lib/gitlab/string_path.rb b/lib/gitlab/string_path.rb index d165829132a..493fceb256d 100644 --- a/lib/gitlab/string_path.rb +++ b/lib/gitlab/string_path.rb @@ -5,6 +5,7 @@ module Gitlab # This is IO-operations safe class, that does similar job to # Ruby's Pathname but without the risk of accessing filesystem. # + # TODO: better support for './' and '../' # class StringPath attr_reader :path, :universe @@ -45,10 +46,13 @@ module Gitlab end def basename - name = @path.split(::File::SEPARATOR).last directory? ? name + ::File::SEPARATOR : name end + def name + @path.split(::File::SEPARATOR).last + end + def has_descendants? descendants.any? end @@ -68,6 +72,10 @@ module Gitlab children.select { |child| child.directory? } end + def directories! + has_parent? ? directories.prepend(new(@path + '../')) : directories + end + def files return [] unless directory? children.select { |child| child.file? } diff --git a/spec/lib/gitlab/string_path_spec.rb b/spec/lib/gitlab/string_path_spec.rb index 7ee69c7d3cb..c1722977576 100644 --- a/spec/lib/gitlab/string_path_spec.rb +++ b/spec/lib/gitlab/string_path_spec.rb @@ -50,18 +50,20 @@ describe Gitlab::StringPath do describe 'path/dir_1/', path: 'path/dir_1/' do subject { |example| path(example) } - it { is_expected.to have_parent } describe '#basename' do subject { |example| path(example).basename } - it { is_expected.to eq 'dir_1/' } end + describe '#name' do + subject { |example| path(example).name } + it { is_expected.to eq 'dir_1' } + end + describe '#parent' do subject { |example| path(example).parent } - it { is_expected.to eq string_path('path/') } end @@ -101,6 +103,15 @@ describe Gitlab::StringPath do it { is_expected.to all(be_an_instance_of described_class) } it { is_expected.to contain_exactly string_path('path/dir_1/subdir/') } 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/'), + string_path('path/dir_1/../') } + end end describe './', path: './' do @@ -118,7 +129,6 @@ describe Gitlab::StringPath do describe '#children' do subject { |example| path(example).children } - it { expect(subject.count).to eq 3 } end end