BigW Consortium Gitlab

Commit b19e958d by 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' }
%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
......@@ -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
......@@ -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? }
......
......@@ -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
......
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