Commit b19e958d authored by Grzegorz Bizon's avatar Grzegorz Bizon

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.
parent aae674c3
%tr{ class: 'tree-item' } %tr{ class: 'tree-item' }
%td.tree-item-file-name %td.tree-item-file-name
= tree_icon('folder', '755', directory.basename) = tree_icon('folder', '755', directory.name)
%span.str-truncated %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 %td
...@@ -16,5 +16,5 @@ ...@@ -16,5 +16,5 @@
%tr %tr
%th Name %th Name
%th Download %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 = render partial: 'tree_file', collection: @path.files, as: :file
...@@ -5,6 +5,7 @@ module Gitlab ...@@ -5,6 +5,7 @@ module Gitlab
# This is IO-operations safe class, that does similar job to # This is IO-operations safe class, that does similar job to
# Ruby's Pathname but without the risk of accessing filesystem. # Ruby's Pathname but without the risk of accessing filesystem.
# #
# TODO: better support for './' and '../'
# #
class StringPath class StringPath
attr_reader :path, :universe attr_reader :path, :universe
...@@ -45,10 +46,13 @@ module Gitlab ...@@ -45,10 +46,13 @@ module Gitlab
end end
def basename def basename
name = @path.split(::File::SEPARATOR).last
directory? ? name + ::File::SEPARATOR : name directory? ? name + ::File::SEPARATOR : name
end end
def name
@path.split(::File::SEPARATOR).last
end
def has_descendants? def has_descendants?
descendants.any? descendants.any?
end end
...@@ -68,6 +72,10 @@ module Gitlab ...@@ -68,6 +72,10 @@ module Gitlab
children.select { |child| child.directory? } children.select { |child| child.directory? }
end end
def directories!
has_parent? ? directories.prepend(new(@path + '../')) : directories
end
def files def files
return [] unless directory? return [] unless directory?
children.select { |child| child.file? } children.select { |child| child.file? }
......
...@@ -50,18 +50,20 @@ describe Gitlab::StringPath do ...@@ -50,18 +50,20 @@ describe Gitlab::StringPath do
describe 'path/dir_1/', path: 'path/dir_1/' do describe 'path/dir_1/', path: 'path/dir_1/' do
subject { |example| path(example) } subject { |example| path(example) }
it { is_expected.to have_parent } it { is_expected.to have_parent }
describe '#basename' do describe '#basename' do
subject { |example| path(example).basename } subject { |example| path(example).basename }
it { is_expected.to eq 'dir_1/' } it { is_expected.to eq 'dir_1/' }
end end
describe '#name' do
subject { |example| path(example).name }
it { is_expected.to eq 'dir_1' }
end
describe '#parent' do describe '#parent' do
subject { |example| path(example).parent } subject { |example| path(example).parent }
it { is_expected.to eq string_path('path/') } it { is_expected.to eq string_path('path/') }
end end
...@@ -101,6 +103,15 @@ describe Gitlab::StringPath do ...@@ -101,6 +103,15 @@ describe Gitlab::StringPath do
it { is_expected.to all(be_an_instance_of described_class) } it { is_expected.to all(be_an_instance_of described_class) }
it { is_expected.to contain_exactly string_path('path/dir_1/subdir/') } it { is_expected.to contain_exactly string_path('path/dir_1/subdir/') }
end 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 end
describe './', path: './' do describe './', path: './' do
...@@ -118,7 +129,6 @@ describe Gitlab::StringPath do ...@@ -118,7 +129,6 @@ describe Gitlab::StringPath do
describe '#children' do describe '#children' do
subject { |example| path(example).children } subject { |example| path(example).children }
it { expect(subject.count).to eq 3 } it { expect(subject.count).to eq 3 }
end end
end end
......
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