aoc-toolkit 0.1.0-alpha.3

A toolkit for solving Advent of Code problems
Documentation
use super::*;

use std::fs::TryLockError;
use tempfile::tempdir;

#[test]
fn prevents_concurrent_lock_acquisition() {
    let directory = tempdir().unwrap();
    let path = directory.path().join("download.lock");

    let first = lock(&path).unwrap();

    let second = OpenOptions::new()
        .read(true)
        .write(true)
        .open(&path)
        .unwrap();

    assert!(matches!(second.try_lock(), Err(TryLockError::WouldBlock)));

    drop(first);

    second.try_lock().unwrap();
}

#[test]
fn creates_parent_directories_for_lock() {
    let directory = tempdir().unwrap();

    let path = directory.path().join("nested").join("download.lock");

    let _guard = lock(&path).unwrap();

    assert!(path.is_file());
}

#[test]
fn preserves_lock_file_after_release() {
    let directory = tempdir().unwrap();
    let path = directory.path().join("download.lock");

    let guard = lock(&path).unwrap();

    drop(guard);

    assert!(path.is_file());

    let _guard = lock(&path).unwrap();
}