Best Python code snippet using pytest
pathlib.py
Source:pathlib.py  
...125            lock_path.unlink()126        except (OSError, IOError):127            pass128    return register(cleanup_on_exit)129def maybe_delete_a_numbered_dir(path):130    """removes a numbered directory if its lock can be obtained and it does not seem to be in use"""131    lock_path = None132    try:133        lock_path = create_cleanup_lock(path)134        parent = path.parent135        garbage = parent.joinpath("garbage-{}".format(uuid.uuid4()))136        path.rename(garbage)137        rmtree(garbage, force=True)138    except (OSError, EnvironmentError):139        #  known races:140        #  * other process did a cleanup at the same time141        #  * deletable folder was found142        #  * process cwd (Windows)143        return144    finally:145        # if we created the lock, ensure we remove it even if we failed146        # to properly remove the numbered dir147        if lock_path is not None:148            try:149                lock_path.unlink()150            except (OSError, IOError):151                pass152def ensure_deletable(path, consider_lock_dead_if_created_before):153    """checks if a lock exists and breaks it if its considered dead"""154    if path.is_symlink():155        return False156    lock = get_lock_path(path)157    if not lock.exists():158        return True159    try:160        lock_time = lock.stat().st_mtime161    except Exception:162        return False163    else:164        if lock_time < consider_lock_dead_if_created_before:165            lock.unlink()166            return True167        else:168            return False169def try_cleanup(path, consider_lock_dead_if_created_before):170    """tries to cleanup a folder if we can ensure it's deletable"""171    if ensure_deletable(path, consider_lock_dead_if_created_before):172        maybe_delete_a_numbered_dir(path)173def cleanup_candidates(root, prefix, keep):174    """lists candidates for numbered directories to be removed - follows py.path"""175    max_existing = max(map(parse_num, find_suffixes(root, prefix)), default=-1)176    max_delete = max_existing - keep177    paths = find_prefixed(root, prefix)178    paths, paths2 = itertools.tee(paths)179    numbers = map(parse_num, extract_suffixes(paths2, prefix))180    for path, number in zip(paths, numbers):181        if number <= max_delete:182            yield path183def cleanup_numbered_dir(root, prefix, keep, consider_lock_dead_if_created_before):184    """cleanup for lock driven numbered directories"""185    for path in cleanup_candidates(root, prefix, keep):186        try_cleanup(path, consider_lock_dead_if_created_before)...test_pathlib.py
Source:test_pathlib.py  
...64    def renamed_failed(*args):65        raise OSError("access denied")66    monkeypatch.setattr(Path, "rename", renamed_failed)67    lock_path = get_lock_path(path)68    maybe_delete_a_numbered_dir(path)...Looking for an in-depth tutorial around pytest? LambdaTest covers the detailed pytest tutorial that has everything related to the pytest, from setting up the pytest framework to automation testing. Delve deeper into pytest testing by exploring advanced use cases like parallel testing, pytest fixtures, parameterization, executing multiple test cases from a single file, and more.
Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.
https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP
Get 100 minutes of automation test minutes FREE!!
