Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
37b2c5dd
Commit
37b2c5dd
authored
Dec 21, 2015
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for root path for `StringPath`
parent
d382335d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
9 deletions
+41
-9
lib/gitlab/string_path.rb
lib/gitlab/string_path.rb
+18
-8
spec/lib/gitlab/string_path_spec.rb
spec/lib/gitlab/string_path_spec.rb
+23
-1
No files found.
lib/gitlab/string_path.rb
View file @
37b2c5dd
module
Gitlab
module
Gitlab
##
##
# Class that represents a path to a file or directory
# Class that represents a
simplified
path to a file or directory
#
#
# 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.
...
@@ -10,8 +10,9 @@ module Gitlab
...
@@ -10,8 +10,9 @@ module Gitlab
attr_reader
:path
,
:universe
attr_reader
:path
,
:universe
def
initialize
(
path
,
universe
)
def
initialize
(
path
,
universe
)
@path
=
path
@path
=
prepare
(
path
)
@universe
=
universe
@universe
=
universe
.
map
{
|
entry
|
prepare
(
entry
)
}
@universe
.
unshift
(
'./'
)
unless
@universe
.
include?
(
'./'
)
end
end
def
to_s
def
to_s
...
@@ -43,6 +44,15 @@ module Gitlab
...
@@ -43,6 +44,15 @@ module Gitlab
new
(
@path
.
sub
(
basename
,
''
))
new
(
@path
.
sub
(
basename
,
''
))
end
end
def
basename
name
=
@path
.
split
(
::
File
::
SEPARATOR
).
last
directory?
?
name
+
::
File
::
SEPARATOR
:
name
end
def
has_descendants?
descendants
.
any?
end
def
descendants
def
descendants
return
[]
unless
directory?
return
[]
unless
directory?
children
=
@universe
.
select
{
|
entry
|
entry
=~
/^
#{
@path
}
.+/
}
children
=
@universe
.
select
{
|
entry
|
entry
=~
/^
#{
@path
}
.+/
}
...
@@ -63,11 +73,6 @@ module Gitlab
...
@@ -63,11 +73,6 @@ module Gitlab
children
.
select
{
|
child
|
child
.
file?
}
children
.
select
{
|
child
|
child
.
file?
}
end
end
def
basename
name
=
@path
.
split
(
::
File
::
SEPARATOR
).
last
directory?
?
name
+
::
File
::
SEPARATOR
:
name
end
def
==
(
other
)
def
==
(
other
)
@path
==
other
.
path
&&
@universe
==
other
.
universe
@path
==
other
.
path
&&
@universe
==
other
.
universe
end
end
...
@@ -77,5 +82,10 @@ module Gitlab
...
@@ -77,5 +82,10 @@ module Gitlab
def
new
(
path
)
def
new
(
path
)
self
.
class
.
new
(
path
,
@universe
)
self
.
class
.
new
(
path
,
@universe
)
end
end
def
prepare
(
path
)
return
path
if
path
=~
%r{^(/|
\.
|
\.\.
)}
path
.
dup
.
prepend
(
'./'
)
end
end
end
end
end
spec/lib/gitlab/string_path_spec.rb
View file @
37b2c5dd
...
@@ -11,6 +11,7 @@ describe Gitlab::StringPath do
...
@@ -11,6 +11,7 @@ describe Gitlab::StringPath do
'path/second_dir'
,
'path/second_dir'
,
'path/second_dir/dir_3/file_2'
,
'path/second_dir/dir_3/file_2'
,
'path/second_dir/dir_3/file_3'
,
'path/second_dir/dir_3/file_3'
,
'another_directory/'
,
'another_file'
,
'another_file'
,
'/file/with/absolute_path'
]
'/file/with/absolute_path'
]
end
end
...
@@ -30,6 +31,7 @@ describe Gitlab::StringPath do
...
@@ -30,6 +31,7 @@ describe Gitlab::StringPath do
it
{
is_expected
.
to_not
be_relative
}
it
{
is_expected
.
to_not
be_relative
}
it
{
is_expected
.
to
be_file
}
it
{
is_expected
.
to
be_file
}
it
{
is_expected
.
to_not
have_parent
}
it
{
is_expected
.
to_not
have_parent
}
it
{
is_expected
.
to_not
have_descendants
}
describe
'#basename'
do
describe
'#basename'
do
subject
{
|
example
|
path
(
example
).
basename
}
subject
{
|
example
|
path
(
example
).
basename
}
...
@@ -43,7 +45,7 @@ describe Gitlab::StringPath do
...
@@ -43,7 +45,7 @@ describe Gitlab::StringPath do
it
{
is_expected
.
to
be_directory
}
it
{
is_expected
.
to
be_directory
}
it
{
is_expected
.
to
be_relative
}
it
{
is_expected
.
to
be_relative
}
it
{
is_expected
.
to
_not
have_parent
}
it
{
is_expected
.
to
have_parent
}
end
end
describe
'path/dir_1/'
,
path:
'path/dir_1/'
do
describe
'path/dir_1/'
,
path:
'path/dir_1/'
do
...
@@ -100,4 +102,24 @@ describe Gitlab::StringPath do
...
@@ -100,4 +102,24 @@ describe Gitlab::StringPath do
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
end
end
describe
'./'
,
path:
'./'
do
subject
{
|
example
|
path
(
example
)
}
it
{
is_expected
.
to_not
have_parent
}
it
{
is_expected
.
to
have_descendants
}
describe
'#descendants'
do
subject
{
|
example
|
path
(
example
).
descendants
}
it
{
expect
(
subject
.
count
).
to
eq
universe
.
count
-
1
}
it
{
is_expected
.
to_not
include
string_path
(
'./'
)
}
end
describe
'#children'
do
subject
{
|
example
|
path
(
example
).
children
}
it
{
expect
(
subject
.
count
).
to
eq
3
}
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment