def test_create_temp_dir__creation_preempted(temp_folder): unwrapped, preempted_once = os.makedirs, [] def _wrap_os_makedirs(*args, **kwargs): # Simulate somebody else creating the directory first if not preempted_once: unwrapped(*args, **kwargs) preempted_once.append(True) unwrapped(*args, **kwargs) with Monkeypatch("os.makedirs", _wrap_os_makedirs): assert not os.listdir(temp_folder) work_dir = create_temp_dir(temp_folder) assert os.path.exists(temp_folder) dirs = os.listdir(temp_folder) assert_equal(len(dirs), 2) assert_in(os.path.basename(work_dir), dirs) assert bool(preempted_once)
def run(self, config): """Runs the node, by calling _setup, _run, and _teardown in that order. Prior to calling these functions, a temporary dir is created using the 'temp_root' prefix from the config object. Both the config object and the temporary dir are passed to the above functions. The temporary dir is removed after _teardown is called, and all expected files should have been removed/renamed at that point. Any non-NodeError exception raised in this function is wrapped in a NodeUnhandledException, which includes a full backtrace. This is needed to allow showing these in the main process.""" try: temp = None temp = fileutils.create_temp_dir(config.temp_root) self._setup(config, temp) self._run(config, temp) self._teardown(config, temp) os.rmdir(temp) except NodeError, error: self._write_error_log(temp, error) raise error
def _create_temp_dir(self, config): """Called by 'run' in order to create a temporary folder. 'config' is expected to have a property .temp_root under which the temporary folder is created.""" return fileutils.create_temp_dir(config.temp_root)
def test_create_temp_dir__permissions(temp_folder): tmp_dir = create_temp_dir(temp_folder) stats = os.stat(tmp_dir) assert_equal(stats.st_mode & 0777, 0700)
def test_create_temp_dir__empty(temp_folder): tmp_dir = create_temp_dir(temp_folder) contents = os.listdir(tmp_dir) assert not contents
def test_create_temp_dir__create(temp_folder): tmp_dir_1 = create_temp_dir(temp_folder) tmp_dir_2 = create_temp_dir(temp_folder) assert os.path.exists(tmp_dir_1) assert os.path.exists(tmp_dir_2)