def test_mktemp(self, tmp_path): config = cast(Config, FakeConfig(tmp_path)) t = TempdirFactory(TempPathFactory.from_config(config)) tmp = t.mktemp("world") assert tmp.relto(t.getbasetemp()) == "world0" tmp = t.mktemp("this") assert tmp.relto(t.getbasetemp()).startswith("this") tmp2 = t.mktemp("this") assert tmp2.relto(t.getbasetemp()).startswith("this") assert tmp2 != tmp
def test_mktemp(self, testdir): from _pytest.tmpdir import TempdirFactory config = testdir.parseconfig() config.option.basetemp = testdir.mkdir("hello") t = TempdirFactory(config) tmp = t.mktemp("world") assert tmp.relto(t.getbasetemp()) == "world0" tmp = t.mktemp("this") assert tmp.relto(t.getbasetemp()).startswith("this") tmp2 = t.mktemp("this") assert tmp2.relto(t.getbasetemp()).startswith("this") assert tmp2 != tmp
def test_mktemp(self, tmp_path): from _pytest.tmpdir import TempdirFactory, TempPathFactory config = FakeConfig(tmp_path) t = TempdirFactory(TempPathFactory.from_config(config)) tmp = t.mktemp("world") assert tmp.relto(t.getbasetemp()) == "world0" tmp = t.mktemp("this") assert tmp.relto(t.getbasetemp()).startswith("this") tmp2 = t.mktemp("this") assert tmp2.relto(t.getbasetemp()).startswith("this") assert tmp2 != tmp
def get_pytest_globaltemp(tmp_path_factory: TempdirFactory) -> Path: """Return global temporary directory for a single pytest run.""" pytest_tmp_dir = Path(tmp_path_factory.getbasetemp()) basetemp = pytest_tmp_dir.parent if IS_XDIST else pytest_tmp_dir basetemp = basetemp / "tmp" basetemp.mkdir(exist_ok=True) return basetemp
def get_pytest_worker_tmp(tmp_path_factory: TempdirFactory) -> Path: """Return pytest temporary directory for the current worker. When running pytest with multiple workers, each worker has it's own base temporary directory inside the "root" temporary directory. """ worker_tmp = Path(tmp_path_factory.getbasetemp()) return worker_tmp
def cluster_cleanup(tmp_path_factory: TempdirFactory, worker_id: str, request: FixtureRequest) -> Generator[None, None, None]: pytest_tmp_dir = Path(tmp_path_factory.getbasetemp()) if not worker_id or worker_id == "master": # if cluster was started outside of test framework, do nothing if cluster_management.DEV_CLUSTER_RUNNING: # TODO: check that socket is open and print error if not yield return yield cluster_manager_obj = cluster_management.ClusterManager( tmp_path_factory=tmp_path_factory, worker_id=worker_id, pytest_config=request.config) cluster_manager_obj.save_worker_cli_coverage() _stop_all_cluster_instances( tmp_path_factory=tmp_path_factory, worker_id=worker_id, pytest_config=request.config, pytest_tmp_dir=pytest_tmp_dir, ) return lock_dir = pytest_tmp_dir = pytest_tmp_dir.parent # pylint: disable=consider-using-with open(lock_dir / f".started_session_{worker_id}", "a").close() yield with helpers.FileLockIfXdist( f"{lock_dir}/{cluster_management.CLUSTER_LOCK}"): cluster_manager_obj = cluster_management.ClusterManager( tmp_path_factory=tmp_path_factory, worker_id=worker_id, pytest_config=request.config) cluster_manager_obj.save_worker_cli_coverage() os.remove(lock_dir / f".started_session_{worker_id}") if not list(lock_dir.glob(".started_session_*")): _stop_all_cluster_instances( tmp_path_factory=tmp_path_factory, worker_id=worker_id, pytest_config=request.config, pytest_tmp_dir=pytest_tmp_dir, )
def cluster_cleanup(tmp_path_factory: TempdirFactory, worker_id: str, request: FixtureRequest) -> Generator: pytest_tmp_dir = Path(tmp_path_factory.getbasetemp()) if not worker_id or worker_id == "master": yield cluster_manager_obj = parallel_run.ClusterManager( tmp_path_factory=tmp_path_factory, worker_id=worker_id, pytest_config=request.config) cluster_manager_obj.save_worker_cli_coverage() _stop_all_cluster_instances( tmp_path_factory=tmp_path_factory, worker_id=worker_id, pytest_config=request.config, pytest_tmp_dir=pytest_tmp_dir, ) return lock_dir = pytest_tmp_dir = pytest_tmp_dir.parent open(lock_dir / f".started_session_{worker_id}", "a").close() yield with helpers.FileLockIfXdist(f"{lock_dir}/{parallel_run.CLUSTER_LOCK}"): cluster_manager_obj = parallel_run.ClusterManager( tmp_path_factory=tmp_path_factory, worker_id=worker_id, pytest_config=request.config) cluster_manager_obj.save_worker_cli_coverage() os.remove(lock_dir / f".started_session_{worker_id}") if not list(lock_dir.glob(".started_session_*")): _stop_all_cluster_instances( tmp_path_factory=tmp_path_factory, worker_id=worker_id, pytest_config=request.config, pytest_tmp_dir=pytest_tmp_dir, )
def __init__(self, tmp_path_factory: TempdirFactory, worker_id: str, pytest_config: Config) -> None: self.cluster_obj: Optional[clusterlib.ClusterLib] = None self.worker_id = worker_id self.pytest_config = pytest_config self.tmp_path_factory = tmp_path_factory self.pytest_tmp_dir = Path(tmp_path_factory.getbasetemp()) self.is_xdist = helpers.IS_XDIST if self.is_xdist: self.lock_dir = self.pytest_tmp_dir.parent self.range_num = 5 self.num_of_instances = CLUSTERS_COUNT else: self.lock_dir = self.pytest_tmp_dir self.range_num = 1 self.num_of_instances = 1 self.cluster_lock = f"{self.lock_dir}/{CLUSTER_LOCK}" self.lock_log = self._init_log() self._cluster_instance = -1
def create_temp_dir(tmp_path_factory: TempdirFactory): """Create a temporary dir.""" p = Path(tmp_path_factory.getbasetemp()).joinpath( helpers.get_id_for_mktemp(__file__)).resolve() p.mkdir(exist_ok=True, parents=True) return p
def change_dir(tmp_path_factory: TempdirFactory) -> None: """Change CWD to temp directory before running tests.""" tmp_path = tmp_path_factory.getbasetemp() os.chdir(tmp_path) LOGGER.info(f"Changed CWD to '{tmp_path}'.")