| 
									
										
										
										
											2001-07-17 20:52:51 +00:00
										 |  |  | # 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 | 
					
						
							| 
									
										
										
										
											2003-10-20 14:01:56 +00:00
										 |  |  | # portable than they had been thought to be. | 
					
						
							| 
									
										
										
										
											2001-07-17 20:52:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2001-08-20 20:10:01 +00:00
										 |  |  | import warnings | 
					
						
							| 
									
										
										
										
											2006-05-04 10:08:42 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  | from test import test_support | 
					
						
							| 
									
										
										
										
											2001-07-17 20:52:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-22 19:24:42 +00:00
										 |  |  | warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__) | 
					
						
							|  |  |  | warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-02 18:44:00 +00:00
										 |  |  | # Tests creating TESTFN | 
					
						
							|  |  |  | class FileTests(unittest.TestCase): | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         if os.path.exists(test_support.TESTFN): | 
					
						
							|  |  |  |             os.unlink(test_support.TESTFN) | 
					
						
							|  |  |  |     tearDown = setUp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_access(self): | 
					
						
							|  |  |  |         f = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR) | 
					
						
							|  |  |  |         os.close(f) | 
					
						
							|  |  |  |         self.assert_(os.access(test_support.TESTFN, os.W_OK)) | 
					
						
							| 
									
										
										
										
											2006-07-03 08:23:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-02 18:44:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-17 20:52:51 +00:00
										 |  |  | class TemporaryFileTests(unittest.TestCase): | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self.files = [] | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         os.mkdir(test_support.TESTFN) | 
					
						
							| 
									
										
										
										
											2001-07-17 20:52:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         for name in self.files: | 
					
						
							|  |  |  |             os.unlink(name) | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         os.rmdir(test_support.TESTFN) | 
					
						
							| 
									
										
										
										
											2001-07-17 20:52:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  |         open(name, "w") | 
					
						
							|  |  |  |         self.files.append(name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_tempnam(self): | 
					
						
							|  |  |  |         if not hasattr(os, "tempnam"): | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2001-08-20 20:10:01 +00:00
										 |  |  |         warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, | 
					
						
							| 
									
										
										
										
											2002-04-16 01:27:44 +00:00
										 |  |  |                                 r"test_os$") | 
					
						
							| 
									
										
										
										
											2001-07-17 20:52:51 +00:00
										 |  |  |         self.check_tempfile(os.tempnam()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         name = os.tempnam(test_support.TESTFN) | 
					
						
							| 
									
										
										
										
											2001-07-17 20:52:51 +00:00
										 |  |  |         self.check_tempfile(name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         name = os.tempnam(test_support.TESTFN, "pfx") | 
					
						
							| 
									
										
										
										
											2001-07-17 20:52:51 +00:00
										 |  |  |         self.assert_(os.path.basename(name)[:3] == "pfx") | 
					
						
							|  |  |  |         self.check_tempfile(name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_tmpfile(self): | 
					
						
							|  |  |  |         if not hasattr(os, "tmpfile"): | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         fp = os.tmpfile() | 
					
						
							|  |  |  |         fp.write("foobar") | 
					
						
							|  |  |  |         fp.seek(0,0) | 
					
						
							|  |  |  |         s = fp.read() | 
					
						
							|  |  |  |         fp.close() | 
					
						
							|  |  |  |         self.assert_(s == "foobar") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_tmpnam(self): | 
					
						
							| 
									
										
										
										
											2003-04-28 03:13:03 +00:00
										 |  |  |         import sys | 
					
						
							| 
									
										
										
										
											2001-07-17 20:52:51 +00:00
										 |  |  |         if not hasattr(os, "tmpnam"): | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2001-08-20 20:10:01 +00:00
										 |  |  |         warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, | 
					
						
							| 
									
										
										
										
											2002-04-16 01:27:44 +00:00
										 |  |  |                                 r"test_os$") | 
					
						
							| 
									
										
										
										
											2003-04-28 03:13:03 +00:00
										 |  |  |         name = os.tmpnam() | 
					
						
							|  |  |  |         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") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.check_tempfile(name) | 
					
						
							| 
									
										
										
										
											2001-07-21 01:41:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
											
												SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files.  Some of the platforms
aren't tested yet.  Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences.  When accessed as a sequence, they behave exactly as
before.  But they also have attributes like st_mtime or tm_year.  The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there).  If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
											
										 
											2001-10-18 20:34:25 +00:00
										 |  |  | # Test attributes on return values from os.*stat* family. | 
					
						
							|  |  |  | class StatAttributeTests(unittest.TestCase): | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         os.mkdir(test_support.TESTFN) | 
					
						
							|  |  |  |         self.fname = os.path.join(test_support.TESTFN, "f1") | 
					
						
							| 
									
										
										
											
												SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files.  Some of the platforms
aren't tested yet.  Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences.  When accessed as a sequence, they behave exactly as
before.  But they also have attributes like st_mtime or tm_year.  The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there).  If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
											
										 
											2001-10-18 20:34:25 +00:00
										 |  |  |         f = open(self.fname, 'wb') | 
					
						
							|  |  |  |         f.write("ABC") | 
					
						
							|  |  |  |         f.close() | 
					
						
							| 
									
										
										
										
											2001-10-18 21:57:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
											
												SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files.  Some of the platforms
aren't tested yet.  Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences.  When accessed as a sequence, they behave exactly as
before.  But they also have attributes like st_mtime or tm_year.  The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there).  If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
											
										 
											2001-10-18 20:34:25 +00:00
										 |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         os.unlink(self.fname) | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         os.rmdir(test_support.TESTFN) | 
					
						
							| 
									
										
										
											
												SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files.  Some of the platforms
aren't tested yet.  Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences.  When accessed as a sequence, they behave exactly as
before.  But they also have attributes like st_mtime or tm_year.  The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there).  If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
											
										 
											2001-10-18 20:34:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_stat_attributes(self): | 
					
						
							|  |  |  |         if not hasattr(os, "stat"): | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         import stat | 
					
						
							|  |  |  |         result = os.stat(self.fname) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Make sure direct access works | 
					
						
							|  |  |  |         self.assertEquals(result[stat.ST_SIZE], 3) | 
					
						
							|  |  |  |         self.assertEquals(result.st_size, 3) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         import sys | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Make sure all the attributes are there | 
					
						
							|  |  |  |         members = dir(result) | 
					
						
							|  |  |  |         for name in dir(stat): | 
					
						
							|  |  |  |             if name[:3] == 'ST_': | 
					
						
							|  |  |  |                 attr = name.lower() | 
					
						
							| 
									
										
										
										
											2005-01-23 09:19:22 +00:00
										 |  |  |                 if name.endswith("TIME"): | 
					
						
							|  |  |  |                     def trunc(x): return int(x) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     def trunc(x): return x | 
					
						
							|  |  |  |                 self.assertEquals(trunc(getattr(result, attr)), | 
					
						
							| 
									
										
										
											
												SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files.  Some of the platforms
aren't tested yet.  Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences.  When accessed as a sequence, they behave exactly as
before.  But they also have attributes like st_mtime or tm_year.  The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there).  If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
											
										 
											2001-10-18 20:34:25 +00:00
										 |  |  |                                   result[getattr(stat, name)]) | 
					
						
							|  |  |  |                 self.assert_(attr in members) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result[200] | 
					
						
							|  |  |  |             self.fail("No exception thrown") | 
					
						
							|  |  |  |         except IndexError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Make sure that assignment fails | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result.st_mode = 1 | 
					
						
							|  |  |  |             self.fail("No exception thrown") | 
					
						
							|  |  |  |         except TypeError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result.st_rdev = 1 | 
					
						
							|  |  |  |             self.fail("No exception thrown") | 
					
						
							| 
									
										
										
										
											2001-10-18 21:19:31 +00:00
										 |  |  |         except (AttributeError, TypeError): | 
					
						
							| 
									
										
										
											
												SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files.  Some of the platforms
aren't tested yet.  Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences.  When accessed as a sequence, they behave exactly as
before.  But they also have attributes like st_mtime or tm_year.  The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there).  If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
											
										 
											2001-10-18 20:34:25 +00:00
										 |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result.parrot = 1 | 
					
						
							|  |  |  |             self.fail("No exception thrown") | 
					
						
							|  |  |  |         except AttributeError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Use the stat_result constructor with a too-short tuple. | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result2 = os.stat_result((10,)) | 
					
						
							|  |  |  |             self.fail("No exception thrown") | 
					
						
							|  |  |  |         except TypeError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Use the constructr with a too-long tuple. | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) | 
					
						
							|  |  |  |         except TypeError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 21:57:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
											
												SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files.  Some of the platforms
aren't tested yet.  Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences.  When accessed as a sequence, they behave exactly as
before.  But they also have attributes like st_mtime or tm_year.  The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there).  If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
											
										 
											2001-10-18 20:34:25 +00:00
										 |  |  |     def test_statvfs_attributes(self): | 
					
						
							|  |  |  |         if not hasattr(os, "statvfs"): | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         import statvfs | 
					
						
							| 
									
										
										
										
											2002-06-11 06:22:31 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             result = os.statvfs(self.fname) | 
					
						
							|  |  |  |         except OSError, e: | 
					
						
							|  |  |  |             # On AtheOS, glibc always returns ENOSYS | 
					
						
							|  |  |  |             import errno | 
					
						
							|  |  |  |             if e.errno == errno.ENOSYS: | 
					
						
							|  |  |  |                 return | 
					
						
							| 
									
										
										
											
												SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files.  Some of the platforms
aren't tested yet.  Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences.  When accessed as a sequence, they behave exactly as
before.  But they also have attributes like st_mtime or tm_year.  The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there).  If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
											
										 
											2001-10-18 20:34:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Make sure direct access works | 
					
						
							|  |  |  |         self.assertEquals(result.f_bfree, result[statvfs.F_BFREE]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Make sure all the attributes are there | 
					
						
							|  |  |  |         members = dir(result) | 
					
						
							|  |  |  |         for name in dir(statvfs): | 
					
						
							|  |  |  |             if name[:2] == 'F_': | 
					
						
							|  |  |  |                 attr = name.lower() | 
					
						
							|  |  |  |                 self.assertEquals(getattr(result, attr), | 
					
						
							|  |  |  |                                   result[getattr(statvfs, name)]) | 
					
						
							|  |  |  |                 self.assert_(attr in members) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Make sure that assignment really fails | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result.f_bfree = 1 | 
					
						
							|  |  |  |             self.fail("No exception thrown") | 
					
						
							|  |  |  |         except TypeError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result.parrot = 1 | 
					
						
							|  |  |  |             self.fail("No exception thrown") | 
					
						
							|  |  |  |         except AttributeError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Use the constructor with a too-short tuple. | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result2 = os.statvfs_result((10,)) | 
					
						
							|  |  |  |             self.fail("No exception thrown") | 
					
						
							|  |  |  |         except TypeError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Use the constructr with a too-long tuple. | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) | 
					
						
							|  |  |  |         except TypeError: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2001-07-17 20:52:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-09 20:44:25 +00:00
										 |  |  |     # Restrict test to Win32, since there is no guarantee other | 
					
						
							|  |  |  |     # systems support centiseconds | 
					
						
							|  |  |  |     if sys.platform == 'win32': | 
					
						
							|  |  |  |         def test_1565150(self): | 
					
						
							|  |  |  |             t1 = 1159195039.25 | 
					
						
							|  |  |  |             os.utime(self.fname, (t1, t1)) | 
					
						
							|  |  |  |             self.assertEquals(os.stat(self.fname).st_mtime, t1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 16:29:04 +00:00
										 |  |  | from test import mapping_tests | 
					
						
							| 
									
										
										
										
											2003-03-09 07:05:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 16:29:04 +00:00
										 |  |  | class EnvironTests(mapping_tests.BasicTestMappingProtocol): | 
					
						
							| 
									
										
										
										
											2003-03-09 07:05:43 +00:00
										 |  |  |     """check that os.environ object conform to mapping protocol""" | 
					
						
							| 
									
										
										
										
											2004-06-02 18:42:25 +00:00
										 |  |  |     type2test = None | 
					
						
							| 
									
										
										
										
											2003-03-09 07:05:43 +00:00
										 |  |  |     def _reference(self): | 
					
						
							|  |  |  |         return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"} | 
					
						
							|  |  |  |     def _empty_mapping(self): | 
					
						
							|  |  |  |         os.environ.clear() | 
					
						
							|  |  |  |         return os.environ | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self.__save = dict(os.environ) | 
					
						
							|  |  |  |         os.environ.clear() | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         os.environ.clear() | 
					
						
							|  |  |  |         os.environ.update(self.__save) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-29 13:29:23 +00:00
										 |  |  |     # Bug 1110478 | 
					
						
							| 
									
										
										
										
											2005-02-17 21:23:20 +00:00
										 |  |  |     def test_update2(self): | 
					
						
							| 
									
										
										
										
											2005-01-29 13:29:23 +00:00
										 |  |  |         if os.path.exists("/bin/sh"): | 
					
						
							|  |  |  |             os.environ.update(HELLO="World") | 
					
						
							|  |  |  |             value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip() | 
					
						
							|  |  |  |             self.assertEquals(value, "World") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-25 07:11:48 +00:00
										 |  |  | class WalkTests(unittest.TestCase): | 
					
						
							|  |  |  |     """Tests for os.walk().""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_traversal(self): | 
					
						
							|  |  |  |         import os | 
					
						
							|  |  |  |         from os.path import join | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Build: | 
					
						
							|  |  |  |         #     TESTFN/               a file kid and two directory kids | 
					
						
							|  |  |  |         #         tmp1 | 
					
						
							|  |  |  |         #         SUB1/             a file kid and a directory kid | 
					
						
							|  |  |  |         #             tmp2 | 
					
						
							|  |  |  |         #             SUB11/        no kids | 
					
						
							|  |  |  |         #         SUB2/             just a file kid | 
					
						
							|  |  |  |         #             tmp3 | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         sub1_path = join(test_support.TESTFN, "SUB1") | 
					
						
							| 
									
										
										
										
											2003-04-25 07:11:48 +00:00
										 |  |  |         sub11_path = join(sub1_path, "SUB11") | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         sub2_path = join(test_support.TESTFN, "SUB2") | 
					
						
							|  |  |  |         tmp1_path = join(test_support.TESTFN, "tmp1") | 
					
						
							| 
									
										
										
										
											2003-04-25 07:11:48 +00:00
										 |  |  |         tmp2_path = join(sub1_path, "tmp2") | 
					
						
							|  |  |  |         tmp3_path = join(sub2_path, "tmp3") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Create stuff. | 
					
						
							|  |  |  |         os.makedirs(sub11_path) | 
					
						
							|  |  |  |         os.makedirs(sub2_path) | 
					
						
							|  |  |  |         for path in tmp1_path, tmp2_path, tmp3_path: | 
					
						
							|  |  |  |             f = file(path, "w") | 
					
						
							|  |  |  |             f.write("I'm " + path + " and proud of it.  Blame test_os.\n") | 
					
						
							|  |  |  |             f.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Walk top-down. | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         all = list(os.walk(test_support.TESTFN)) | 
					
						
							| 
									
										
										
										
											2003-04-25 07:11:48 +00:00
										 |  |  |         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" | 
					
						
							|  |  |  |         all[0][1].sort() | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         self.assertEqual(all[0], (test_support.TESTFN, ["SUB1", "SUB2"], ["tmp1"])) | 
					
						
							| 
									
										
										
										
											2003-04-25 07:11:48 +00:00
										 |  |  |         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"])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Prune the search. | 
					
						
							|  |  |  |         all = [] | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         for root, dirs, files in os.walk(test_support.TESTFN): | 
					
						
							| 
									
										
										
										
											2003-04-25 07:11:48 +00:00
										 |  |  |             all.append((root, dirs, files)) | 
					
						
							|  |  |  |             # Don't descend into SUB1. | 
					
						
							|  |  |  |             if 'SUB1' in dirs: | 
					
						
							|  |  |  |                 # Note that this also mutates the dirs we appended to all! | 
					
						
							|  |  |  |                 dirs.remove('SUB1') | 
					
						
							|  |  |  |         self.assertEqual(len(all), 2) | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         self.assertEqual(all[0], (test_support.TESTFN, ["SUB2"], ["tmp1"])) | 
					
						
							| 
									
										
										
										
											2003-04-25 07:11:48 +00:00
										 |  |  |         self.assertEqual(all[1], (sub2_path, [], ["tmp3"])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Walk bottom-up. | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         all = list(os.walk(test_support.TESTFN, topdown=False)) | 
					
						
							| 
									
										
										
										
											2003-04-25 07:11:48 +00:00
										 |  |  |         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" | 
					
						
							|  |  |  |         all[3][1].sort() | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         self.assertEqual(all[3], (test_support.TESTFN, ["SUB1", "SUB2"], ["tmp1"])) | 
					
						
							| 
									
										
										
										
											2003-04-25 07:11:48 +00:00
										 |  |  |         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. | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         for root, dirs, files in os.walk(test_support.TESTFN, topdown=False): | 
					
						
							| 
									
										
										
										
											2003-04-25 07:11:48 +00:00
										 |  |  |             for name in files: | 
					
						
							|  |  |  |                 os.remove(join(root, name)) | 
					
						
							|  |  |  |             for name in dirs: | 
					
						
							|  |  |  |                 os.rmdir(join(root, name)) | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         os.rmdir(test_support.TESTFN) | 
					
						
							| 
									
										
										
										
											2003-04-25 07:11:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-12-23 16:36:11 +00:00
										 |  |  | class MakedirTests (unittest.TestCase): | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         os.mkdir(test_support.TESTFN) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_makedir(self): | 
					
						
							|  |  |  |         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') | 
					
						
							|  |  |  |         os.makedirs(path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # 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) | 
					
						
							|  |  |  |         os.makedirs(path) | 
					
						
							|  |  |  |         path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4', | 
					
						
							|  |  |  |                             'dir5', 'dir6') | 
					
						
							|  |  |  |         os.makedirs(path) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-01-18 20:29:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-12-23 16:36:11 +00:00
										 |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         path = os.path.join(test_support.TESTFN, 'dir1', 'dir2', 'dir3', | 
					
						
							|  |  |  |                             'dir4', 'dir5', 'dir6') | 
					
						
							|  |  |  |         # If the tests failed, the bottom-most directory ('../dir6') | 
					
						
							|  |  |  |         # may not have been created, so we look for the outermost directory | 
					
						
							|  |  |  |         # that exists. | 
					
						
							|  |  |  |         while not os.path.exists(path) and path != test_support.TESTFN: | 
					
						
							|  |  |  |             path = os.path.dirname(path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         os.removedirs(path) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-08 08:29:33 +00:00
										 |  |  | class DevNullTests (unittest.TestCase): | 
					
						
							|  |  |  |     def test_devnull(self): | 
					
						
							|  |  |  |         f = file(os.devnull, 'w') | 
					
						
							|  |  |  |         f.write('hello') | 
					
						
							|  |  |  |         f.close() | 
					
						
							|  |  |  |         f = file(os.devnull, 'r') | 
					
						
							| 
									
										
										
										
											2004-06-08 20:34:34 +00:00
										 |  |  |         self.assertEqual(f.read(), '') | 
					
						
							| 
									
										
										
										
											2004-06-08 08:29:33 +00:00
										 |  |  |         f.close() | 
					
						
							| 
									
										
										
										
											2003-12-23 16:36:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-29 15:46:35 +00:00
										 |  |  | class URandomTests (unittest.TestCase): | 
					
						
							|  |  |  |     def test_urandom(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             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: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-04 10:08:42 +00:00
										 |  |  | class Win32ErrorTests(unittest.TestCase): | 
					
						
							|  |  |  |     def test_rename(self): | 
					
						
							|  |  |  |         self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_support.TESTFN+".bak") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_remove(self): | 
					
						
							|  |  |  |         self.assertRaises(WindowsError, os.remove, test_support.TESTFN) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_chdir(self): | 
					
						
							|  |  |  |         self.assertRaises(WindowsError, os.chdir, test_support.TESTFN) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 16:32:54 +00:00
										 |  |  |     def test_mkdir(self): | 
					
						
							|  |  |  |         self.assertRaises(WindowsError, os.chdir, test_support.TESTFN) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_utime(self): | 
					
						
							|  |  |  |         self.assertRaises(WindowsError, os.utime, test_support.TESTFN, None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_access(self): | 
					
						
							|  |  |  |         self.assertRaises(WindowsError, os.utime, test_support.TESTFN, 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_chmod(self): | 
					
						
							|  |  |  |         self.assertRaises(WindowsError, os.utime, test_support.TESTFN, 0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-04 10:08:42 +00:00
										 |  |  | if sys.platform != 'win32': | 
					
						
							|  |  |  |     class Win32ErrorTests(unittest.TestCase): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-20 21:33:42 +00:00
										 |  |  | def test_main(): | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |     test_support.run_unittest( | 
					
						
							| 
									
										
										
										
											2006-07-02 18:44:00 +00:00
										 |  |  |         FileTests, | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |         TemporaryFileTests, | 
					
						
							|  |  |  |         StatAttributeTests, | 
					
						
							|  |  |  |         EnvironTests, | 
					
						
							| 
									
										
										
										
											2003-12-23 16:36:11 +00:00
										 |  |  |         WalkTests, | 
					
						
							|  |  |  |         MakedirTests, | 
					
						
							| 
									
										
										
										
											2004-06-08 08:29:33 +00:00
										 |  |  |         DevNullTests, | 
					
						
							| 
									
										
										
										
											2006-05-04 10:08:42 +00:00
										 |  |  |         URandomTests, | 
					
						
							|  |  |  |         Win32ErrorTests | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2001-09-20 21:33:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     test_main() |