| OLD | NEW |
| 1 # As a test suite for the os module, this is woefully inadequate, but this | 1 # As a test suite for the os module, this is woefully inadequate, but this |
| 2 # does add tests for a few functions which have been determined to be more | 2 # does add tests for a few functions which have been determined to be more |
| 3 # portable than they had been thought to be. | 3 # portable than they had been thought to be. |
| 4 | 4 |
| 5 import os | 5 import os |
| 6 import unittest | 6 import unittest |
| 7 import warnings | 7 import warnings |
| 8 import subprocess |
| 8 import sys | 9 import sys |
| 9 from test import test_support | 10 from test import test_support |
| 10 | 11 |
| 11 warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__) | 12 warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__) |
| 12 warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__) | 13 warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__) |
| 13 | 14 |
| 14 # Tests creating TESTFN | 15 # Tests creating TESTFN |
| 15 class FileTests(unittest.TestCase): | 16 class FileTests(unittest.TestCase): |
| 16 def setUp(self): | 17 def setUp(self): |
| 17 if os.path.exists(test_support.TESTFN): | 18 if os.path.exists(test_support.TESTFN): |
| (...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 def test_devnull(self): | 493 def test_devnull(self): |
| 493 f = file(os.devnull, 'w') | 494 f = file(os.devnull, 'w') |
| 494 f.write('hello') | 495 f.write('hello') |
| 495 f.close() | 496 f.close() |
| 496 f = file(os.devnull, 'r') | 497 f = file(os.devnull, 'r') |
| 497 self.assertEqual(f.read(), '') | 498 self.assertEqual(f.read(), '') |
| 498 f.close() | 499 f.close() |
| 499 | 500 |
| 500 class URandomTests (unittest.TestCase): | 501 class URandomTests (unittest.TestCase): |
| 501 def test_urandom(self): | 502 def test_urandom(self): |
| 502 try: | 503 with test_support.check_warnings(): |
| 503 with test_support.check_warnings(): | 504 self.assertEqual(len(os.urandom(1)), 1) |
| 504 self.assertEqual(len(os.urandom(1)), 1) | 505 self.assertEqual(len(os.urandom(10)), 10) |
| 505 self.assertEqual(len(os.urandom(10)), 10) | 506 self.assertEqual(len(os.urandom(100)), 100) |
| 506 self.assertEqual(len(os.urandom(100)), 100) | 507 self.assertEqual(len(os.urandom(1000)), 1000) |
| 507 self.assertEqual(len(os.urandom(1000)), 1000) | 508 # see http://bugs.python.org/issue3708 |
| 508 # see http://bugs.python.org/issue3708 | 509 self.assertEqual(len(os.urandom(0.9)), 0) |
| 509 self.assertEqual(len(os.urandom(0.9)), 0) | 510 self.assertEqual(len(os.urandom(1.1)), 1) |
| 510 self.assertEqual(len(os.urandom(1.1)), 1) | 511 self.assertEqual(len(os.urandom(2.0)), 2) |
| 511 self.assertEqual(len(os.urandom(2.0)), 2) | 512 |
| 512 except NotImplementedError: | 513 def test_urandom_length(self): |
| 513 pass | 514 self.assertEqual(len(os.urandom(0)), 0) |
| 515 self.assertEqual(len(os.urandom(1)), 1) |
| 516 self.assertEqual(len(os.urandom(10)), 10) |
| 517 self.assertEqual(len(os.urandom(100)), 100) |
| 518 self.assertEqual(len(os.urandom(1000)), 1000) |
| 519 |
| 520 def test_urandom_value(self): |
| 521 data1 = os.urandom(16) |
| 522 data2 = os.urandom(16) |
| 523 self.assertNotEqual(data1, data2) |
| 524 |
| 525 def get_urandom_subprocess(self, count): |
| 526 code = '\n'.join(( |
| 527 'import os, sys', |
| 528 'data = os.urandom(%s)' % count, |
| 529 'sys.stdout.write(data)')) |
| 530 cmd_line = [sys.executable, |
| 531 '-c', code] |
| 532 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
| 533 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 534 out, err = p.communicate() |
| 535 out = test_support.strip_python_stderr(out) |
| 536 self.assertEqual(len(out), count) |
| 537 return out |
| 538 |
| 539 def test_urandom_subprocess(self): |
| 540 data1 = self.get_urandom_subprocess(16) |
| 541 data2 = self.get_urandom_subprocess(16) |
| 542 self.assertNotEqual(data1, data2) |
| 514 | 543 |
| 515 class Win32ErrorTests(unittest.TestCase): | 544 class Win32ErrorTests(unittest.TestCase): |
| 516 def test_rename(self): | 545 def test_rename(self): |
| 517 self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_sup
port.TESTFN+".bak") | 546 self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_sup
port.TESTFN+".bak") |
| 518 | 547 |
| 519 def test_remove(self): | 548 def test_remove(self): |
| 520 self.assertRaises(WindowsError, os.remove, test_support.TESTFN) | 549 self.assertRaises(WindowsError, os.remove, test_support.TESTFN) |
| 521 | 550 |
| 522 def test_chdir(self): | 551 def test_chdir(self): |
| 523 self.assertRaises(WindowsError, os.chdir, test_support.TESTFN) | 552 self.assertRaises(WindowsError, os.chdir, test_support.TESTFN) |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 MakedirTests, | 710 MakedirTests, |
| 682 DevNullTests, | 711 DevNullTests, |
| 683 URandomTests, | 712 URandomTests, |
| 684 Win32ErrorTests, | 713 Win32ErrorTests, |
| 685 TestInvalidFD, | 714 TestInvalidFD, |
| 686 PosixUidGidTests | 715 PosixUidGidTests |
| 687 ) | 716 ) |
| 688 | 717 |
| 689 if __name__ == "__main__": | 718 if __name__ == "__main__": |
| 690 test_main() | 719 test_main() |
| OLD | NEW |