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
f678b313
Kaydet (Commit)
f678b313
authored
Haz 21, 2013
tarafından
Christian Heimes
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add tests for untested features of the 'stat' module (part of issue #11016)
üst
edb4260f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
132 additions
and
13 deletions
+132
-13
test_stat.py
Lib/test/test_stat.py
+132
-13
No files found.
Lib/test/test_stat.py
Dosyayı görüntüle @
f678b313
import
unittest
import
os
from
test.support
import
TESTFN
,
run_unittest
,
import_fresh_module
import
stat
from
test.support
import
TESTFN
,
run_unittest
class
TestFilemode
(
unittest
.
TestCase
):
file_flags
=
{
'SF_APPEND'
,
'SF_ARCHIVED'
,
'SF_IMMUTABLE'
,
'SF_NOUNLINK'
,
'SF_SNAPSHOT'
,
'UF_APPEND'
,
'UF_COMPRESSED'
,
'UF_HIDDEN'
,
'UF_IMMUTABLE'
,
'UF_NODUMP'
,
'UF_NOUNLINK'
,
'UF_OPAQUE'
}
def
get_mode
(
fname
=
TESTFN
):
return
stat
.
filemode
(
os
.
lstat
(
fname
)
.
st_mode
)
formats
=
{
'S_IFBLK'
,
'S_IFCHR'
,
'S_IFDIR'
,
'S_IFIFO'
,
'S_IFLNK'
,
'S_IFREG'
,
'S_IFSOCK'
}
format_funcs
=
{
'S_ISBLK'
,
'S_ISCHR'
,
'S_ISDIR'
,
'S_ISFIFO'
,
'S_ISLNK'
,
'S_ISREG'
,
'S_ISSOCK'
}
class
TestFilemode
(
unittest
.
TestCase
):
stat_struct
=
{
'ST_MODE'
:
0
,
'ST_INO'
:
1
,
'ST_DEV'
:
2
,
'ST_NLINK'
:
3
,
'ST_UID'
:
4
,
'ST_GID'
:
5
,
'ST_SIZE'
:
6
,
'ST_ATIME'
:
7
,
'ST_MTIME'
:
8
,
'ST_CTIME'
:
9
}
# permission bit value are defined by POSIX
permission_bits
=
{
'S_ISUID'
:
0
o4000
,
'S_ISGID'
:
0
o2000
,
'S_ENFMT'
:
0
o2000
,
'S_ISVTX'
:
0
o1000
,
'S_IRWXU'
:
0
o700
,
'S_IRUSR'
:
0
o400
,
'S_IREAD'
:
0
o400
,
'S_IWUSR'
:
0
o200
,
'S_IWRITE'
:
0
o200
,
'S_IXUSR'
:
0
o100
,
'S_IEXEC'
:
0
o100
,
'S_IRWXG'
:
0
o070
,
'S_IRGRP'
:
0
o040
,
'S_IWGRP'
:
0
o020
,
'S_IXGRP'
:
0
o010
,
'S_IRWXO'
:
0
o007
,
'S_IROTH'
:
0
o004
,
'S_IWOTH'
:
0
o002
,
'S_IXOTH'
:
0
o001
}
def
setUp
(
self
):
try
:
...
...
@@ -20,29 +58,75 @@ class TestFilemode(unittest.TestCase):
pass
tearDown
=
setUp
def
get_mode
(
self
,
fname
=
TESTFN
):
st_mode
=
os
.
lstat
(
fname
)
.
st_mode
modestr
=
stat
.
filemode
(
st_mode
)
return
st_mode
,
modestr
def
assertS_IS
(
self
,
name
,
mode
):
# test format, lstrip is for S_IFIFO
fmt
=
getattr
(
stat
,
"S_IF"
+
name
.
lstrip
(
"F"
))
self
.
assertEqual
(
stat
.
S_IFMT
(
mode
),
fmt
)
# test that just one function returns true
testname
=
"S_IS"
+
name
for
funcname
in
self
.
format_funcs
:
func
=
getattr
(
stat
,
funcname
,
None
)
if
func
is
None
:
if
funcname
==
testname
:
raise
ValueError
(
funcname
)
continue
if
funcname
==
testname
:
self
.
assertTrue
(
func
(
mode
))
else
:
self
.
assertFalse
(
func
(
mode
))
def
test_mode
(
self
):
with
open
(
TESTFN
,
'w'
):
pass
if
os
.
name
==
'posix'
:
os
.
chmod
(
TESTFN
,
0
o700
)
self
.
assertEqual
(
get_mode
(),
'-rwx------'
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertEqual
(
modestr
,
'-rwx------'
)
self
.
assertS_IS
(
"REG"
,
st_mode
)
self
.
assertEqual
(
stat
.
S_IMODE
(
st_mode
),
stat
.
S_IRWXU
)
os
.
chmod
(
TESTFN
,
0
o070
)
self
.
assertEqual
(
get_mode
(),
'----rwx---'
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertEqual
(
modestr
,
'----rwx---'
)
self
.
assertS_IS
(
"REG"
,
st_mode
)
self
.
assertEqual
(
stat
.
S_IMODE
(
st_mode
),
stat
.
S_IRWXG
)
os
.
chmod
(
TESTFN
,
0
o007
)
self
.
assertEqual
(
get_mode
(),
'-------rwx'
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertEqual
(
modestr
,
'-------rwx'
)
self
.
assertS_IS
(
"REG"
,
st_mode
)
self
.
assertEqual
(
stat
.
S_IMODE
(
st_mode
),
stat
.
S_IRWXO
)
os
.
chmod
(
TESTFN
,
0
o444
)
self
.
assertEqual
(
get_mode
(),
'-r--r--r--'
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertS_IS
(
"REG"
,
st_mode
)
self
.
assertEqual
(
modestr
,
'-r--r--r--'
)
self
.
assertEqual
(
stat
.
S_IMODE
(
st_mode
),
0
o444
)
else
:
os
.
chmod
(
TESTFN
,
0
o700
)
self
.
assertEqual
(
get_mode
()[:
3
],
'-rw'
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertEqual
(
modestr
[:
3
],
'-rw'
)
self
.
assertS_IS
(
"REG"
,
st_mode
)
self
.
assertEqual
(
stat
.
S_IFMT
(
st_mode
),
stat
.
S_IFREG
)
def
test_directory
(
self
):
os
.
mkdir
(
TESTFN
)
os
.
chmod
(
TESTFN
,
0
o700
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertS_IS
(
"DIR"
,
st_mode
)
if
os
.
name
==
'posix'
:
self
.
assertEqual
(
get_mode
()
,
'drwx------'
)
self
.
assertEqual
(
modestr
,
'drwx------'
)
else
:
self
.
assertEqual
(
get_mode
()
[
0
],
'd'
)
self
.
assertEqual
(
modestr
[
0
],
'd'
)
@unittest.skipUnless
(
hasattr
(
os
,
'symlink'
),
'os.symlink not available'
)
def
test_link
(
self
):
...
...
@@ -51,12 +135,47 @@ class TestFilemode(unittest.TestCase):
except
(
OSError
,
NotImplementedError
)
as
err
:
raise
unittest
.
SkipTest
(
str
(
err
))
else
:
self
.
assertEqual
(
get_mode
()[
0
],
'l'
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertEqual
(
modestr
[
0
],
'l'
)
self
.
assertS_IS
(
"LNK"
,
st_mode
)
@unittest.skipUnless
(
hasattr
(
os
,
'mkfifo'
),
'os.mkfifo not available'
)
def
test_fifo
(
self
):
os
.
mkfifo
(
TESTFN
,
0
o700
)
self
.
assertEqual
(
get_mode
(),
'prwx------'
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertEqual
(
modestr
,
'prwx------'
)
self
.
assertS_IS
(
"FIFO"
,
st_mode
)
@unittest.skipUnless
(
os
.
name
==
'posix'
,
'requires Posix'
)
def
test_devices
(
self
):
if
os
.
path
.
exists
(
os
.
devnull
):
st_mode
,
modestr
=
self
.
get_mode
(
os
.
devnull
)
self
.
assertEqual
(
modestr
[
0
],
'c'
)
self
.
assertS_IS
(
"CHR"
,
st_mode
)
for
blockdev
in
(
"/dev/sda"
,
"/dev/hda"
,
"/dev/da0"
,
"/dev/ad0"
):
if
os
.
path
.
exists
(
blockdev
):
st_mode
,
modestr
=
self
.
get_mode
(
blockdev
)
self
.
assertEqual
(
modestr
[
0
],
'b'
)
self
.
assertS_IS
(
"BLK"
,
st_mode
)
break
def
test_module_attributes
(
self
):
for
key
,
value
in
self
.
stat_struct
.
items
():
modvalue
=
getattr
(
stat
,
key
)
self
.
assertEqual
(
value
,
modvalue
,
key
)
for
key
,
value
in
self
.
permission_bits
.
items
():
modvalue
=
getattr
(
stat
,
key
)
self
.
assertEqual
(
value
,
modvalue
,
key
)
for
key
in
self
.
file_flags
:
modvalue
=
getattr
(
stat
,
key
)
self
.
assertIsInstance
(
modvalue
,
int
)
for
key
in
self
.
formats
:
modvalue
=
getattr
(
stat
,
key
)
self
.
assertIsInstance
(
modvalue
,
int
)
for
key
in
self
.
format_funcs
:
func
=
getattr
(
stat
,
key
)
self
.
assertTrue
(
callable
(
func
))
self
.
assertEqual
(
func
(
0
),
0
)
def
test_main
():
...
...
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