Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
da557fbc
Commit
da557fbc
authored
Apr 07, 2013
by
Akira Yumiyama
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add File.expand_path
parent
cc40f93a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
90 additions
and
0 deletions
+90
-0
mrblib/file.rb
mrblib/file.rb
+65
-0
run_test.rb
run_test.rb
+2
-0
test/file.rb
test/file.rb
+23
-0
No files found.
mrblib/file.rb
View file @
da557fbc
...
@@ -63,6 +63,71 @@ class File < IO
...
@@ -63,6 +63,71 @@ class File < IO
end
end
end
end
def
self
.
expand_path
(
path
,
default_dir
=
'.'
)
def
concat_path
(
path
,
base_path
)
if
path
[
0
]
==
"/"
expanded_path
=
path
elsif
path
[
0
]
==
"~"
if
(
path
[
1
]
==
"/"
||
path
[
1
]
==
nil
)
dir
=
path
[
1
,
path
.
size
]
home_dir
=
_gethome
unless
home_dir
raise
ArgumentError
,
"couldn't find HOME environment -- expanding '~'"
end
expanded_path
=
home_dir
expanded_path
+=
dir
if
dir
expanded_path
+=
"/"
else
splitted_path
=
path
.
split
(
"/"
)
user
=
splitted_path
[
0
][
1
,
splitted_path
[
0
].
size
]
dir
=
"/"
+
splitted_path
[
1
,
splitted_path
.
size
].
join
(
"/"
)
home_dir
=
_gethome
(
user
)
unless
home_dir
raise
ArgumentError
,
"user
#{
user
}
doesn't exist"
end
expanded_path
=
home_dir
expanded_path
+=
dir
if
dir
expanded_path
+=
"/"
end
else
expanded_path
=
concat_path
(
base_path
,
_getwd
)
expanded_path
+=
"/"
+
path
end
expanded_path
end
expanded_path
=
concat_path
(
path
,
default_dir
)
expand_path_array
=
[]
while
expanded_path
.
include?
(
'//'
)
expanded_path
=
expanded_path
.
gsub
(
'//'
,
'/'
)
end
if
expanded_path
==
"/"
expanded_path
else
expanded_path
.
split
(
'/'
).
each
do
|
path_token
|
if
path_token
==
'..'
if
expand_path_array
.
size
>
1
expand_path_array
.
pop
end
elsif
path_token
==
'.'
# nothing to do.
else
expand_path_array
<<
path_token
end
end
expand_path
=
expand_path_array
.
join
(
"/"
)
expand_path
.
empty?
?
'/'
:
expand_path
end
end
def
self
.
directory?
(
file
)
def
self
.
directory?
(
file
)
FileTest
.
directory?
(
file
)
FileTest
.
directory?
(
file
)
end
end
...
...
run_test.rb
View file @
da557fbc
...
@@ -29,5 +29,7 @@ MRuby::Build.new do |conf|
...
@@ -29,5 +29,7 @@ MRuby::Build.new do |conf|
conf
.
gem
x
unless
x
=~
/\/mruby-(print|sprintf)$/
conf
.
gem
x
unless
x
=~
/\/mruby-(print|sprintf)$/
end
end
conf
.
gem
:github
=>
'iij/mruby-env'
conf
.
gem
File
.
expand_path
(
File
.
dirname
(
__FILE__
))
conf
.
gem
File
.
expand_path
(
File
.
dirname
(
__FILE__
))
end
end
test/file.rb
View file @
da557fbc
...
@@ -73,3 +73,26 @@ end
...
@@ -73,3 +73,26 @@ end
assert
(
'File TEST CLEANUP'
)
do
assert
(
'File TEST CLEANUP'
)
do
assert_nil
MRubyIOTestUtil
.
io_test_cleanup
assert_nil
MRubyIOTestUtil
.
io_test_cleanup
end
end
assert
(
'File.expand_path'
)
do
assert_equal
"/"
,
File
.
expand_path
(
".."
,
"/tmp"
),
"parent path with base_dir (1)"
assert_equal
"/tmp"
,
File
.
expand_path
(
".."
,
"/tmp/mruby"
),
"parent path with base_dir (2)"
assert_equal
"/home"
,
File
.
expand_path
(
"/home"
),
"absolute"
assert_equal
"/home"
,
File
.
expand_path
(
"/home"
,
"."
),
"absolute with base_dir"
assert_equal
"/hoge"
,
File
.
expand_path
(
"/tmp/..//hoge"
)
assert_equal
"/hoge"
,
File
.
expand_path
(
"////tmp/..///////hoge"
)
assert_equal
"/"
,
File
.
expand_path
(
"../../../.."
,
"/"
)
assert_equal
"/"
,
File
.
expand_path
(([
".."
]
*
100
).
join
(
"/"
))
end
assert
(
'File.expand_path (with ENV)'
)
do
skip
unless
Object
.
const_defined?
(
:ENV
)
&&
ENV
[
'HOME'
]
assert_equal
ENV
[
'HOME'
],
File
.
expand_path
(
"~/"
),
"home"
assert_equal
ENV
[
'HOME'
],
File
.
expand_path
(
"~/"
,
"/"
),
"home with base_dir"
assert_equal
"
#{
ENV
[
'HOME'
]
}
/user"
,
File
.
expand_path
(
"user"
,
ENV
[
'HOME'
]),
"relative with base_dir"
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