Tests for temporary dir
()
| 34 | |
| 35 | |
| 36 | def test_tempdir(): |
| 37 | """Tests for temporary dir""" |
| 38 | assert utils.TempDirectory._KEEP_FOR_DEBUG is False, "don't submit with KEEP_FOR_DEBUG == True" |
| 39 | |
| 40 | temp_dir = utils.tempdir() |
| 41 | assert os.path.exists(temp_dir.temp_dir) |
| 42 | |
| 43 | old_debug_mode = utils.TempDirectory._KEEP_FOR_DEBUG |
| 44 | old_tempdirs = utils.TempDirectory.TEMPDIRS |
| 45 | try: |
| 46 | for temp_dir_number in range(0, 3): |
| 47 | with utils.TempDirectory.set_keep_for_debug(): |
| 48 | debug_temp_dir = utils.tempdir() |
| 49 | try: |
| 50 | validate_debug_dir_path(debug_temp_dir, "0000" + str(temp_dir_number)) |
| 51 | finally: |
| 52 | shutil.rmtree(debug_temp_dir.temp_dir) |
| 53 | |
| 54 | with utils.TempDirectory.set_keep_for_debug(): |
| 55 | # Create 2 temp_dir within the same session. |
| 56 | debug_temp_dir = utils.tempdir() |
| 57 | try: |
| 58 | validate_debug_dir_path(debug_temp_dir, "00003") |
| 59 | finally: |
| 60 | shutil.rmtree(debug_temp_dir.temp_dir) |
| 61 | |
| 62 | debug_temp_dir = utils.tempdir() |
| 63 | try: |
| 64 | validate_debug_dir_path(debug_temp_dir, "00004") |
| 65 | finally: |
| 66 | shutil.rmtree(debug_temp_dir.temp_dir) |
| 67 | |
| 68 | with utils.TempDirectory.set_keep_for_debug(False): |
| 69 | debug_temp_dir = utils.tempdir() # This one should get deleted. |
| 70 | |
| 71 | # Simulate atexit hook |
| 72 | utils.TempDirectory.remove_tempdirs() |
| 73 | |
| 74 | # Calling twice should be a no-op. |
| 75 | utils.TempDirectory.remove_tempdirs() |
| 76 | |
| 77 | # Creating a new TempDirectory should fail now |
| 78 | try: |
| 79 | utils.tempdir() |
| 80 | assert False, "creation should fail" |
| 81 | except utils.DirectoryCreatedPastAtExit: |
| 82 | pass |
| 83 | |
| 84 | finally: |
| 85 | utils.TempDirectory.DEBUG_MODE = old_debug_mode |
| 86 | utils.TempDirectory.TEMPDIRS = old_tempdirs |
| 87 | |
| 88 | |
| 89 | if __name__ == "__main__": |
no test coverage detected
searching dependent graphs…