Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
0561c53d
Kaydet (Commit)
0561c53d
authored
Mar 12, 2015
tarafından
Victor Stinner
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #23605: Refactor os.walk() tests to also run them on os.fwalk()
üst
a112a8ae
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
37 deletions
+70
-37
test_os.py
Lib/test/test_os.py
+70
-37
No files found.
Lib/test/test_os.py
Dosyayı görüntüle @
0561c53d
...
@@ -694,9 +694,17 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol):
...
@@ -694,9 +694,17 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol):
class
WalkTests
(
unittest
.
TestCase
):
class
WalkTests
(
unittest
.
TestCase
):
"""Tests for os.walk()."""
"""Tests for os.walk()."""
# Wrapper to hide minor differences between os.walk and os.fwalk
# to tests both functions with the same code base
def
walk
(
self
,
directory
,
topdown
=
True
,
follow_symlinks
=
False
):
walk_it
=
os
.
walk
(
directory
,
topdown
=
topdown
,
followlinks
=
follow_symlinks
)
for
root
,
dirs
,
files
in
walk_it
:
yield
(
root
,
dirs
,
files
)
def
setUp
(
self
):
def
setUp
(
self
):
import
os
join
=
os
.
path
.
join
from
os.path
import
join
# Build:
# Build:
# TESTFN/
# TESTFN/
...
@@ -711,36 +719,39 @@ class WalkTests(unittest.TestCase):
...
@@ -711,36 +719,39 @@ class WalkTests(unittest.TestCase):
# broken_link
# broken_link
# TEST2/
# TEST2/
# tmp4 a lone file
# tmp4 a lone file
walk_path
=
join
(
support
.
TESTFN
,
"TEST1"
)
self
.
walk_path
=
join
(
support
.
TESTFN
,
"TEST1"
)
s
ub1_path
=
join
(
walk_path
,
"SUB1"
)
s
elf
.
sub1_path
=
join
(
self
.
walk_path
,
"SUB1"
)
s
ub11_path
=
join
(
sub1_path
,
"SUB11"
)
s
elf
.
sub11_path
=
join
(
self
.
sub1_path
,
"SUB11"
)
sub2_path
=
join
(
walk_path
,
"SUB2"
)
sub2_path
=
join
(
self
.
walk_path
,
"SUB2"
)
tmp1_path
=
join
(
walk_path
,
"tmp1"
)
tmp1_path
=
join
(
self
.
walk_path
,
"tmp1"
)
tmp2_path
=
join
(
sub1_path
,
"tmp2"
)
tmp2_path
=
join
(
s
elf
.
s
ub1_path
,
"tmp2"
)
tmp3_path
=
join
(
sub2_path
,
"tmp3"
)
tmp3_path
=
join
(
sub2_path
,
"tmp3"
)
link_path
=
join
(
sub2_path
,
"link"
)
self
.
link_path
=
join
(
sub2_path
,
"link"
)
t2_path
=
join
(
support
.
TESTFN
,
"TEST2"
)
t2_path
=
join
(
support
.
TESTFN
,
"TEST2"
)
tmp4_path
=
join
(
support
.
TESTFN
,
"TEST2"
,
"tmp4"
)
tmp4_path
=
join
(
support
.
TESTFN
,
"TEST2"
,
"tmp4"
)
link_path
=
join
(
sub2_path
,
"link"
)
broken_link_path
=
join
(
sub2_path
,
"broken_link"
)
broken_link_path
=
join
(
sub2_path
,
"broken_link"
)
# Create stuff.
# Create stuff.
os
.
makedirs
(
sub11_path
)
os
.
makedirs
(
s
elf
.
s
ub11_path
)
os
.
makedirs
(
sub2_path
)
os
.
makedirs
(
sub2_path
)
os
.
makedirs
(
t2_path
)
os
.
makedirs
(
t2_path
)
for
path
in
tmp1_path
,
tmp2_path
,
tmp3_path
,
tmp4_path
:
for
path
in
tmp1_path
,
tmp2_path
,
tmp3_path
,
tmp4_path
:
f
=
open
(
path
,
"w"
)
f
=
open
(
path
,
"w"
)
f
.
write
(
"I'm "
+
path
+
" and proud of it. Blame test_os.
\n
"
)
f
.
write
(
"I'm "
+
path
+
" and proud of it. Blame test_os.
\n
"
)
f
.
close
()
f
.
close
()
if
support
.
can_symlink
():
if
support
.
can_symlink
():
os
.
symlink
(
os
.
path
.
abspath
(
t2_path
),
link_path
)
os
.
symlink
(
os
.
path
.
abspath
(
t2_path
),
self
.
link_path
)
os
.
symlink
(
'broken'
,
broken_link_path
,
True
)
os
.
symlink
(
'broken'
,
broken_link_path
,
True
)
sub2_tree
=
(
sub2_path
,
[
"link"
],
[
"broken_link"
,
"tmp3"
])
s
elf
.
s
ub2_tree
=
(
sub2_path
,
[
"link"
],
[
"broken_link"
,
"tmp3"
])
else
:
else
:
sub2_tree
=
(
sub2_path
,
[],
[
"tmp3"
])
s
elf
.
s
ub2_tree
=
(
sub2_path
,
[],
[
"tmp3"
])
def
test_walk_topdown
(
self
):
# Walk top-down.
# Walk top-down.
all
=
list
(
os
.
walk
(
walk_path
))
all
=
list
(
os
.
walk
(
self
.
walk_path
))
self
.
assertEqual
(
len
(
all
),
4
)
self
.
assertEqual
(
len
(
all
),
4
)
# We can't know which order SUB1 and SUB2 will appear in.
# We can't know which order SUB1 and SUB2 will appear in.
# Not flipped: TESTFN, SUB1, SUB11, SUB2
# Not flipped: TESTFN, SUB1, SUB11, SUB2
...
@@ -748,26 +759,32 @@ class WalkTests(unittest.TestCase):
...
@@ -748,26 +759,32 @@ class WalkTests(unittest.TestCase):
flipped
=
all
[
0
][
1
][
0
]
!=
"SUB1"
flipped
=
all
[
0
][
1
][
0
]
!=
"SUB1"
all
[
0
][
1
]
.
sort
()
all
[
0
][
1
]
.
sort
()
all
[
3
-
2
*
flipped
][
-
1
]
.
sort
()
all
[
3
-
2
*
flipped
][
-
1
]
.
sort
()
self
.
assertEqual
(
all
[
0
],
(
walk_path
,
[
"SUB1"
,
"SUB2"
],
[
"tmp1"
]))
self
.
assertEqual
(
all
[
0
],
(
self
.
walk_path
,
[
"SUB1"
,
"SUB2"
],
[
"tmp1"
]))
self
.
assertEqual
(
all
[
1
+
flipped
],
(
sub1_path
,
[
"SUB11"
],
[
"tmp2"
]))
self
.
assertEqual
(
all
[
1
+
flipped
],
(
s
elf
.
s
ub1_path
,
[
"SUB11"
],
[
"tmp2"
]))
self
.
assertEqual
(
all
[
2
+
flipped
],
(
sub11_path
,
[],
[]))
self
.
assertEqual
(
all
[
2
+
flipped
],
(
s
elf
.
s
ub11_path
,
[],
[]))
self
.
assertEqual
(
all
[
3
-
2
*
flipped
],
sub2_tree
)
self
.
assertEqual
(
all
[
3
-
2
*
flipped
],
s
elf
.
s
ub2_tree
)
def
test_walk_prune
(
self
):
# Prune the search.
# Prune the search.
all
=
[]
all
=
[]
for
root
,
dirs
,
files
in
os
.
walk
(
walk_path
):
for
root
,
dirs
,
files
in
self
.
walk
(
self
.
walk_path
):
all
.
append
((
root
,
dirs
,
files
))
all
.
append
((
root
,
dirs
,
files
))
# Don't descend into SUB1.
# Don't descend into SUB1.
if
'SUB1'
in
dirs
:
if
'SUB1'
in
dirs
:
# Note that this also mutates the dirs we appended to all!
# Note that this also mutates the dirs we appended to all!
dirs
.
remove
(
'SUB1'
)
dirs
.
remove
(
'SUB1'
)
self
.
assertEqual
(
len
(
all
),
2
)
self
.
assertEqual
(
len
(
all
),
2
)
self
.
assertEqual
(
all
[
0
],
(
walk_path
,
[
"SUB2"
],
[
"tmp1"
]))
self
.
assertEqual
(
all
[
0
],
(
self
.
walk_path
,
[
"SUB2"
],
[
"tmp1"
]))
all
[
1
][
-
1
]
.
sort
()
all
[
1
][
-
1
]
.
sort
()
self
.
assertEqual
(
all
[
1
],
sub2_tree
)
self
.
assertEqual
(
all
[
1
],
s
elf
.
s
ub2_tree
)
def
test_walk_bottom_up
(
self
):
# Walk bottom-up.
# Walk bottom-up.
all
=
list
(
os
.
walk
(
walk_path
,
topdown
=
False
))
all
=
list
(
self
.
walk
(
self
.
walk_path
,
topdown
=
False
))
self
.
assertEqual
(
len
(
all
),
4
)
self
.
assertEqual
(
len
(
all
),
4
)
# We can't know which order SUB1 and SUB2 will appear in.
# We can't know which order SUB1 and SUB2 will appear in.
# Not flipped: SUB11, SUB1, SUB2, TESTFN
# Not flipped: SUB11, SUB1, SUB2, TESTFN
...
@@ -775,20 +792,28 @@ class WalkTests(unittest.TestCase):
...
@@ -775,20 +792,28 @@ class WalkTests(unittest.TestCase):
flipped
=
all
[
3
][
1
][
0
]
!=
"SUB1"
flipped
=
all
[
3
][
1
][
0
]
!=
"SUB1"
all
[
3
][
1
]
.
sort
()
all
[
3
][
1
]
.
sort
()
all
[
2
-
2
*
flipped
][
-
1
]
.
sort
()
all
[
2
-
2
*
flipped
][
-
1
]
.
sort
()
self
.
assertEqual
(
all
[
3
],
(
walk_path
,
[
"SUB1"
,
"SUB2"
],
[
"tmp1"
]))
self
.
assertEqual
(
all
[
3
],
self
.
assertEqual
(
all
[
flipped
],
(
sub11_path
,
[],
[]))
(
self
.
walk_path
,
[
"SUB1"
,
"SUB2"
],
[
"tmp1"
]))
self
.
assertEqual
(
all
[
flipped
+
1
],
(
sub1_path
,
[
"SUB11"
],
[
"tmp2"
]))
self
.
assertEqual
(
all
[
flipped
],
self
.
assertEqual
(
all
[
2
-
2
*
flipped
],
sub2_tree
)
(
self
.
sub11_path
,
[],
[]))
self
.
assertEqual
(
all
[
flipped
+
1
],
if
support
.
can_symlink
():
(
self
.
sub1_path
,
[
"SUB11"
],
[
"tmp2"
]))
# Walk, following symlinks.
self
.
assertEqual
(
all
[
2
-
2
*
flipped
],
for
root
,
dirs
,
files
in
os
.
walk
(
walk_path
,
followlinks
=
True
):
self
.
sub2_tree
)
if
root
==
link_path
:
self
.
assertEqual
(
dirs
,
[])
def
test_walk_symlink
(
self
):
self
.
assertEqual
(
files
,
[
"tmp4"
])
if
not
support
.
can_symlink
():
break
self
.
skipTest
(
"need symlink support"
)
else
:
self
.
fail
(
"Didn't follow symlink with followlinks=True"
)
# Walk, following symlinks.
walk_it
=
self
.
walk
(
self
.
walk_path
,
follow_symlinks
=
True
)
for
root
,
dirs
,
files
in
walk_it
:
if
root
==
self
.
link_path
:
self
.
assertEqual
(
dirs
,
[])
self
.
assertEqual
(
files
,
[
"tmp4"
])
break
else
:
self
.
fail
(
"Didn't follow symlink with followlinks=True"
)
def
tearDown
(
self
):
def
tearDown
(
self
):
# Tear everything down. This is a decent use for bottom-up on
# Tear everything down. This is a decent use for bottom-up on
...
@@ -811,6 +836,14 @@ class WalkTests(unittest.TestCase):
...
@@ -811,6 +836,14 @@ class WalkTests(unittest.TestCase):
class
FwalkTests
(
WalkTests
):
class
FwalkTests
(
WalkTests
):
"""Tests for os.fwalk()."""
"""Tests for os.fwalk()."""
def
walk
(
self
,
directory
,
topdown
=
True
,
follow_symlinks
=
False
):
walk_it
=
os
.
fwalk
(
directory
,
topdown
=
topdown
,
follow_symlinks
=
follow_symlinks
)
for
root
,
dirs
,
files
,
root_fd
in
walk_it
:
yield
(
root
,
dirs
,
files
)
def
_compare_to_walk
(
self
,
walk_kwargs
,
fwalk_kwargs
):
def
_compare_to_walk
(
self
,
walk_kwargs
,
fwalk_kwargs
):
"""
"""
compare with walk() results.
compare with walk() results.
...
...
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