Left: | ||
Right: |
OLD | NEW |
---|---|
1 # Author: Steven J. Bethard <steven.bethard@gmail.com>. | 1 # Author: Steven J. Bethard <steven.bethard@gmail.com>. |
2 | 2 |
3 import codecs | 3 import codecs |
4 import inspect | 4 import inspect |
5 import os | 5 import os |
6 import shutil | 6 import shutil |
7 import sys | 7 import sys |
8 import textwrap | 8 import textwrap |
9 import tempfile | 9 import tempfile |
10 import unittest | 10 import unittest |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 def tearDown(self): | 46 def tearDown(self): |
47 os.chdir(self.old_dir) | 47 os.chdir(self.old_dir) |
48 while True: | 48 while True: |
49 try: | 49 try: |
50 shutil.rmtree(self.temp_dir) | 50 shutil.rmtree(self.temp_dir) |
51 except WindowsError: | 51 except WindowsError: |
52 continue | 52 continue |
53 else: | 53 else: |
54 break | 54 break |
55 | 55 |
56 def create_readonly_file(self, filename): | |
57 file_path = os.path.join(self.temp_dir, filename) | |
58 file = open(file_path, 'w') | |
merwok
2010/11/25 11:28:06
Please use a with statement.
SilentGhost
2010/11/25 13:46:37
Done.
| |
59 file.write(filename) | |
60 file.close() | |
61 import stat | |
merwok
2010/11/25 11:28:06
Please avoid imports at the function level.
SilentGhost
2010/11/25 13:46:37
Done.
| |
62 os.chmod(file_path, stat.S_IREAD) | |
63 | |
56 | 64 |
57 class Sig(object): | 65 class Sig(object): |
58 | 66 |
59 def __init__(self, *args, **kwargs): | 67 def __init__(self, *args, **kwargs): |
60 self.args = args | 68 self.args = args |
61 self.kwargs = kwargs | 69 self.kwargs = kwargs |
62 | 70 |
63 | 71 |
64 class NS(object): | 72 class NS(object): |
65 | 73 |
(...skipping 1373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1439 | 1447 |
1440 class TestFileTypeR(TempDirMixin, ParserTestCase): | 1448 class TestFileTypeR(TempDirMixin, ParserTestCase): |
1441 """Test the FileType option/argument type for reading files""" | 1449 """Test the FileType option/argument type for reading files""" |
1442 | 1450 |
1443 def setUp(self): | 1451 def setUp(self): |
1444 super(TestFileTypeR, self).setUp() | 1452 super(TestFileTypeR, self).setUp() |
1445 for file_name in ['foo', 'bar']: | 1453 for file_name in ['foo', 'bar']: |
1446 file = open(os.path.join(self.temp_dir, file_name), 'w') | 1454 file = open(os.path.join(self.temp_dir, file_name), 'w') |
1447 file.write(file_name) | 1455 file.write(file_name) |
1448 file.close() | 1456 file.close() |
1457 self.create_readonly_file('readonly') | |
1449 | 1458 |
1450 argument_signatures = [ | 1459 argument_signatures = [ |
1451 Sig('-x', type=argparse.FileType()), | 1460 Sig('-x', type=argparse.FileType()), |
1452 Sig('spam', type=argparse.FileType('r')), | 1461 Sig('spam', type=argparse.FileType('r')), |
1453 ] | 1462 ] |
1454 failures = ['-x', ''] | 1463 failures = ['-x', '', 'non-existent-file.txt'] |
1455 successes = [ | 1464 successes = [ |
1456 ('foo', NS(x=None, spam=RFile('foo'))), | 1465 ('foo', NS(x=None, spam=RFile('foo'))), |
1457 ('-x foo bar', NS(x=RFile('foo'), spam=RFile('bar'))), | 1466 ('-x foo bar', NS(x=RFile('foo'), spam=RFile('bar'))), |
1458 ('bar -x foo', NS(x=RFile('foo'), spam=RFile('bar'))), | 1467 ('bar -x foo', NS(x=RFile('foo'), spam=RFile('bar'))), |
1459 ('-x - -', NS(x=sys.stdin, spam=sys.stdin)), | 1468 ('-x - -', NS(x=sys.stdin, spam=sys.stdin)), |
1469 ('readonly', NS(x=None, spam=RFile('readonly'))), | |
1460 ] | 1470 ] |
1461 | 1471 |
1462 | 1472 |
1463 class TestFileTypeRB(TempDirMixin, ParserTestCase): | 1473 class TestFileTypeRB(TempDirMixin, ParserTestCase): |
1464 """Test the FileType option/argument type for reading files""" | 1474 """Test the FileType option/argument type for reading files""" |
1465 | 1475 |
1466 def setUp(self): | 1476 def setUp(self): |
1467 super(TestFileTypeRB, self).setUp() | 1477 super(TestFileTypeRB, self).setUp() |
1468 for file_name in ['foo', 'bar']: | 1478 for file_name in ['foo', 'bar']: |
1469 file = open(os.path.join(self.temp_dir, file_name), 'w') | 1479 file = open(os.path.join(self.temp_dir, file_name), 'w') |
(...skipping 26 matching lines...) Expand all Loading... | |
1496 text = text.encode('ascii') | 1506 text = text.encode('ascii') |
1497 other.write(text) | 1507 other.write(text) |
1498 other.close() | 1508 other.close() |
1499 self.seen.add(other) | 1509 self.seen.add(other) |
1500 return self.name == other.name | 1510 return self.name == other.name |
1501 | 1511 |
1502 | 1512 |
1503 class TestFileTypeW(TempDirMixin, ParserTestCase): | 1513 class TestFileTypeW(TempDirMixin, ParserTestCase): |
1504 """Test the FileType option/argument type for writing files""" | 1514 """Test the FileType option/argument type for writing files""" |
1505 | 1515 |
1516 def setUp(self): | |
1517 super(TestFileTypeW, self).setUp() | |
1518 self.create_readonly_file('readonly') | |
1519 | |
1506 argument_signatures = [ | 1520 argument_signatures = [ |
1507 Sig('-x', type=argparse.FileType('w')), | 1521 Sig('-x', type=argparse.FileType('w')), |
1508 Sig('spam', type=argparse.FileType('w')), | 1522 Sig('spam', type=argparse.FileType('w')), |
1509 ] | 1523 ] |
1510 failures = ['-x', ''] | 1524 failures = ['-x', '', 'readonly'] |
1511 successes = [ | 1525 successes = [ |
1512 ('foo', NS(x=None, spam=WFile('foo'))), | 1526 ('foo', NS(x=None, spam=WFile('foo'))), |
1513 ('-x foo bar', NS(x=WFile('foo'), spam=WFile('bar'))), | 1527 ('-x foo bar', NS(x=WFile('foo'), spam=WFile('bar'))), |
1514 ('bar -x foo', NS(x=WFile('foo'), spam=WFile('bar'))), | 1528 ('bar -x foo', NS(x=WFile('foo'), spam=WFile('bar'))), |
1515 ('-x - -', NS(x=sys.stdout, spam=sys.stdout)), | 1529 ('-x - -', NS(x=sys.stdout, spam=sys.stdout)), |
1516 ] | 1530 ] |
1517 | 1531 |
1518 | 1532 |
1519 class TestFileTypeWB(TempDirMixin, ParserTestCase): | 1533 class TestFileTypeWB(TempDirMixin, ParserTestCase): |
1520 | 1534 |
(...skipping 2808 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4329 DeprecationWarning)): | 4343 DeprecationWarning)): |
4330 support.run_unittest(__name__) | 4344 support.run_unittest(__name__) |
4331 # Remove global references to avoid looking like we have refleaks. | 4345 # Remove global references to avoid looking like we have refleaks. |
4332 RFile.seen = {} | 4346 RFile.seen = {} |
4333 WFile.seen = set() | 4347 WFile.seen = set() |
4334 | 4348 |
4335 | 4349 |
4336 | 4350 |
4337 if __name__ == '__main__': | 4351 if __name__ == '__main__': |
4338 test_main() | 4352 test_main() |
OLD | NEW |