(request)
| 62 | |
| 63 | |
| 64 | def _create_tempdir(request): |
| 65 | # Get directory name of test_foo.py file |
| 66 | testfile = request.module.__file__ |
| 67 | testfiledir = os.path.dirname(os.path.abspath(testfile)) |
| 68 | |
| 69 | # Construct name test_foo_tempdir from name test_foo.py |
| 70 | testfilename = os.path.basename(testfile) |
| 71 | outputname = testfilename.replace(".py", f"_tempdir_{_worker_id(request)}") |
| 72 | |
| 73 | # Get function name test_something from test_foo.py |
| 74 | function = request.function.__name__ |
| 75 | |
| 76 | # Join all of these to make a unique path for this test function |
| 77 | basepath = os.path.join(testfiledir, outputname) |
| 78 | path = os.path.join(basepath, function) |
| 79 | |
| 80 | # Add a sequence number to avoid collisions when tests are otherwise |
| 81 | # parameterized |
| 82 | comm = MPI.COMM_WORLD |
| 83 | if comm.rank == 0: |
| 84 | _create_tempdir._sequencenumber[path] += 1 |
| 85 | sequencenumber = _create_tempdir._sequencenumber[path] |
| 86 | else: |
| 87 | sequencenumber = None |
| 88 | |
| 89 | sequencenumber = comm.bcast(sequencenumber) |
| 90 | path += "__" + str(sequencenumber) |
| 91 | |
| 92 | # Delete and re-create directory on root node |
| 93 | if comm.rank == 0: |
| 94 | # First time visiting this basepath, delete the old and create a |
| 95 | # new |
| 96 | if basepath not in _create_tempdir._basepaths: |
| 97 | _create_tempdir._basepaths.add(basepath) |
| 98 | if os.path.exists(basepath): |
| 99 | shutil.rmtree(basepath) |
| 100 | # Make sure we have the base path test_foo_tempdir for this |
| 101 | # test_foo.py file |
| 102 | if not os.path.exists(basepath): |
| 103 | os.mkdir(basepath) |
| 104 | |
| 105 | # Delete path from old test run |
| 106 | if os.path.exists(path): |
| 107 | shutil.rmtree(path) |
| 108 | # Make sure we have the path for this test execution: e.g. |
| 109 | # test_foo_tempdir/test_something__3 |
| 110 | if not os.path.exists(path): |
| 111 | os.mkdir(path) |
| 112 | |
| 113 | # Wait until the above created the directory |
| 114 | waited = 0 |
| 115 | while not os.path.exists(path): |
| 116 | time.sleep(0.1) |
| 117 | waited += 0.1 |
| 118 | if waited > 1: |
| 119 | raise RuntimeError(f"Unable to create test directory {path}") |
| 120 | |
| 121 | comm.Barrier() |
no test coverage detected