|
4 | 4 | import inspect |
5 | 5 | import os |
6 | 6 | import shutil |
| 7 | +import stat |
7 | 8 | import sys |
8 | 9 | import textwrap |
9 | 10 | import tempfile |
@@ -45,14 +46,13 @@ def setUp(self): |
45 | 46 |
|
46 | 47 | def tearDown(self): |
47 | 48 | os.chdir(self.old_dir) |
48 | | - while True: |
49 | | - try: |
50 | | - shutil.rmtree(self.temp_dir) |
51 | | - except WindowsError: |
52 | | - continue |
53 | | - else: |
54 | | - break |
| 49 | + shutil.rmtree(self.temp_dir, True) |
55 | 50 |
|
| 51 | + def create_readonly_file(self, filename): |
| 52 | + file_path = os.path.join(self.temp_dir, filename) |
| 53 | + with open(file_path, 'w') as file: |
| 54 | + file.write(filename) |
| 55 | + os.chmod(file_path, stat.S_IREAD) |
56 | 56 |
|
57 | 57 | class Sig(object): |
58 | 58 |
|
@@ -1446,17 +1446,19 @@ def setUp(self): |
1446 | 1446 | file = open(os.path.join(self.temp_dir, file_name), 'w') |
1447 | 1447 | file.write(file_name) |
1448 | 1448 | file.close() |
| 1449 | + self.create_readonly_file('readonly') |
1449 | 1450 |
|
1450 | 1451 | argument_signatures = [ |
1451 | 1452 | Sig('-x', type=argparse.FileType()), |
1452 | 1453 | Sig('spam', type=argparse.FileType('r')), |
1453 | 1454 | ] |
1454 | | - failures = ['-x', ''] |
| 1455 | + failures = ['-x', '', 'non-existent-file.txt'] |
1455 | 1456 | successes = [ |
1456 | 1457 | ('foo', NS(x=None, spam=RFile('foo'))), |
1457 | 1458 | ('-x foo bar', NS(x=RFile('foo'), spam=RFile('bar'))), |
1458 | 1459 | ('bar -x foo', NS(x=RFile('foo'), spam=RFile('bar'))), |
1459 | 1460 | ('-x - -', NS(x=sys.stdin, spam=sys.stdin)), |
| 1461 | + ('readonly', NS(x=None, spam=RFile('readonly'))), |
1460 | 1462 | ] |
1461 | 1463 |
|
1462 | 1464 |
|
@@ -1503,11 +1505,15 @@ def __eq__(self, other): |
1503 | 1505 | class TestFileTypeW(TempDirMixin, ParserTestCase): |
1504 | 1506 | """Test the FileType option/argument type for writing files""" |
1505 | 1507 |
|
| 1508 | + def setUp(self): |
| 1509 | + super(TestFileTypeW, self).setUp() |
| 1510 | + self.create_readonly_file('readonly') |
| 1511 | + |
1506 | 1512 | argument_signatures = [ |
1507 | 1513 | Sig('-x', type=argparse.FileType('w')), |
1508 | 1514 | Sig('spam', type=argparse.FileType('w')), |
1509 | 1515 | ] |
1510 | | - failures = ['-x', ''] |
| 1516 | + failures = ['-x', '', 'readonly'] |
1511 | 1517 | successes = [ |
1512 | 1518 | ('foo', NS(x=None, spam=WFile('foo'))), |
1513 | 1519 | ('-x foo bar', NS(x=WFile('foo'), spam=WFile('bar'))), |
|
0 commit comments