# As a test suite for the os module, this is woefully inadequate, but this
# does add tests for a few functions which have been determined to be more
# portable than they had been thought to be.
from test
import test_support
warnings
.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__
)
warnings
.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__
)
class TemporaryFileTests(unittest
.TestCase
):
os
.mkdir(test_support
.TESTFN
)
os
.rmdir(test_support
.TESTFN
)
def check_tempfile(self
, name
):
# make sure it doesn't already exist:
self
.failIf(os
.path
.exists(name
),
"file already exists for temporary file")
# make sure we can create the file
if not hasattr(os
, "tempnam"):
warnings
.filterwarnings("ignore", "tempnam", RuntimeWarning,
self
.check_tempfile(os
.tempnam())
name
= os
.tempnam(test_support
.TESTFN
)
self
.check_tempfile(name
)
name
= os
.tempnam(test_support
.TESTFN
, "pfx")
self
.assert_(os
.path
.basename(name
)[:3] == "pfx")
self
.check_tempfile(name
)
if not hasattr(os
, "tmpfile"):
self
.assert_(s
== "foobar")
if not hasattr(os
, "tmpnam"):
warnings
.filterwarnings("ignore", "tmpnam", RuntimeWarning,
if sys
.platform
in ("win32",):
# The Windows tmpnam() seems useless. From the MS docs:
# The character string that tmpnam creates consists of
# the path prefix, defined by the entry P_tmpdir in the
# file STDIO.H, followed by a sequence consisting of the
# digit characters '0' through '9'; the numerical value
# of this string is in the range 1 - 65,535. Changing the
# definitions of L_tmpnam or P_tmpdir in STDIO.H does not
# change the operation of tmpnam.
# The really bizarre part is that, at least under MSVC6,
# P_tmpdir is "\\". That is, the path returned refers to
# the root of the current drive. That's a terrible place to
# put temp files, and, depending on privileges, the user
# may not even be able to open a file in the root directory.
self
.failIf(os
.path
.exists(name
),
"file already exists for temporary file")
self
.check_tempfile(name
)
# Test attributes on return values from os.*stat* family.
class StatAttributeTests(unittest
.TestCase
):
os
.mkdir(test_support
.TESTFN
)
self
.fname
= os
.path
.join(test_support
.TESTFN
, "f1")
f
= open(self
.fname
, 'wb')
os
.rmdir(test_support
.TESTFN
)
def test_stat_attributes(self
):
if not hasattr(os
, "stat"):
result
= os
.stat(self
.fname
)
# Make sure direct access works
self
.assertEquals(result
[stat
.ST_SIZE
], 3)
self
.assertEquals(result
.st_size
, 3)
# Make sure all the attributes are there
self
.assertEquals(getattr(result
, attr
),
result
[getattr(stat
, name
)])
self
.assert_(attr
in members
)
self
.fail("No exception thrown")
# Make sure that assignment fails
self
.fail("No exception thrown")
self
.fail("No exception thrown")
except (AttributeError, TypeError):
self
.fail("No exception thrown")
# Use the stat_result constructor with a too-short tuple.
result2
= os
.stat_result((10,))
self
.fail("No exception thrown")
# Use the constructr with a too-long tuple.
result2
= os
.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
def test_statvfs_attributes(self
):
if not hasattr(os
, "statvfs"):
result
= os
.statvfs(self
.fname
)
# On AtheOS, glibc always returns ENOSYS
if e
.errno
== errno
.ENOSYS
:
# Make sure direct access works
self
.assertEquals(result
.f_bfree
, result
[statvfs
.F_BFREE
])
# Make sure all the attributes are there
for name
in dir(statvfs
):
self
.assertEquals(getattr(result
, attr
),
result
[getattr(statvfs
, name
)])
self
.assert_(attr
in members
)
# Make sure that assignment really fails
self
.fail("No exception thrown")
self
.fail("No exception thrown")
# Use the constructor with a too-short tuple.
result2
= os
.statvfs_result((10,))
self
.fail("No exception thrown")
# Use the constructr with a too-long tuple.
result2
= os
.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
from test
import mapping_tests
class EnvironTests(mapping_tests
.BasicTestMappingProtocol
):
"""check that os.environ object conform to mapping protocol"""
return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
def _empty_mapping(self
):
self
.__save
= dict(os
.environ
)
os
.environ
.update(self
.__save
)
class WalkTests(unittest
.TestCase
):
"""Tests for os.walk()."""
def test_traversal(self
):
# TESTFN/ a file kid and two directory kids
# SUB1/ a file kid and a directory kid
sub1_path
= join(test_support
.TESTFN
, "SUB1")
sub11_path
= join(sub1_path
, "SUB11")
sub2_path
= join(test_support
.TESTFN
, "SUB2")
tmp1_path
= join(test_support
.TESTFN
, "tmp1")
tmp2_path
= join(sub1_path
, "tmp2")
tmp3_path
= join(sub2_path
, "tmp3")
for path
in tmp1_path
, tmp2_path
, tmp3_path
:
f
.write("I'm " + path
+ " and proud of it. Blame test_os.\n")
all
= list(os
.walk(test_support
.TESTFN
))
self
.assertEqual(len(all
), 4)
# We can't know which order SUB1 and SUB2 will appear in.
# Not flipped: TESTFN, SUB1, SUB11, SUB2
# flipped: TESTFN, SUB2, SUB1, SUB11
flipped
= all
[0][1][0] != "SUB1"
self
.assertEqual(all
[0], (test_support
.TESTFN
, ["SUB1", "SUB2"], ["tmp1"]))
self
.assertEqual(all
[1 + flipped
], (sub1_path
, ["SUB11"], ["tmp2"]))
self
.assertEqual(all
[2 + flipped
], (sub11_path
, [], []))
self
.assertEqual(all
[3 - 2 * flipped
], (sub2_path
, [], ["tmp3"]))
for root
, dirs
, files
in os
.walk(test_support
.TESTFN
):
all
.append((root
, dirs
, files
))
# Don't descend into SUB1.
# Note that this also mutates the dirs we appended to all!
self
.assertEqual(len(all
), 2)
self
.assertEqual(all
[0], (test_support
.TESTFN
, ["SUB2"], ["tmp1"]))
self
.assertEqual(all
[1], (sub2_path
, [], ["tmp3"]))
all
= list(os
.walk(test_support
.TESTFN
, topdown
=False))
self
.assertEqual(len(all
), 4)
# We can't know which order SUB1 and SUB2 will appear in.
# Not flipped: SUB11, SUB1, SUB2, TESTFN
# flipped: SUB2, SUB11, SUB1, TESTFN
flipped
= all
[3][1][0] != "SUB1"
self
.assertEqual(all
[3], (test_support
.TESTFN
, ["SUB1", "SUB2"], ["tmp1"]))
self
.assertEqual(all
[flipped
], (sub11_path
, [], []))
self
.assertEqual(all
[flipped
+ 1], (sub1_path
, ["SUB11"], ["tmp2"]))
self
.assertEqual(all
[2 - 2 * flipped
], (sub2_path
, [], ["tmp3"]))
# Tear everything down. This is a decent use for bottom-up on
# Windows, which doesn't have a recursive delete command. The
# (not so) subtlety is that rmdir will fail unless the dir's
# kids are removed first, so bottom up is essential.
for root
, dirs
, files
in os
.walk(test_support
.TESTFN
, topdown
=False):
os
.remove(join(root
, name
))
os
.rmdir(join(root
, name
))
os
.rmdir(test_support
.TESTFN
)
class MakedirTests (unittest
.TestCase
):
os
.mkdir(test_support
.TESTFN
)
base
= test_support
.TESTFN
path
= os
.path
.join(base
, 'dir1', 'dir2', 'dir3')
os
.makedirs(path
) # Should work
path
= os
.path
.join(base
, 'dir1', 'dir2', 'dir3', 'dir4')
# Try paths with a '.' in them
self
.failUnlessRaises(OSError, os
.makedirs
, os
.curdir
)
path
= os
.path
.join(base
, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os
.curdir
)
path
= os
.path
.join(base
, 'dir1', os
.curdir
, 'dir2', 'dir3', 'dir4',
path
= os
.path
.join(test_support
.TESTFN
, 'dir1', 'dir2', 'dir3',
# If the tests failed, the bottom-most directory ('../dir6')
# may not have been created, so we look for the outermost directory
while not os
.path
.exists(path
) and path
!= test_support
.TESTFN
:
path
= os
.path
.dirname(path
)
class DevNullTests (unittest
.TestCase
):
f
= file(os
.devnull
, 'w')
f
= file(os
.devnull
, 'r')
self
.assertEqual(f
.read(), '')
class URandomTests (unittest
.TestCase
):
self
.assertEqual(len(os
.urandom(1)), 1)
self
.assertEqual(len(os
.urandom(10)), 10)
self
.assertEqual(len(os
.urandom(100)), 100)
self
.assertEqual(len(os
.urandom(1000)), 1000)
except NotImplementedError:
test_support
.run_unittest(
if __name__
== "__main__":