#![allow(missing_docs)]
#![cfg(feature = "lockfile")]
use librebar::lockfile::Lockfile;
use tempfile::TempDir;
#[test]
fn acquire_lock_succeeds() {
let tmp = TempDir::new().unwrap();
let lock = Lockfile::new("test-app", tmp.path());
assert!(lock.try_acquire().is_ok());
}
#[test]
fn lock_creates_file() {
let tmp = TempDir::new().unwrap();
let lock = Lockfile::new("test-app", tmp.path());
let _guard = lock.try_acquire().unwrap();
let lock_path = tmp.path().join("test-app.lock");
assert!(lock_path.exists());
}
#[test]
fn lock_released_on_guard_drop() {
let tmp = TempDir::new().unwrap();
let lock = Lockfile::new("test-app", tmp.path());
{
let _guard = lock.try_acquire().unwrap();
}
let lock2 = Lockfile::new("test-app", tmp.path());
assert!(lock2.try_acquire().is_ok());
}
#[test]
fn second_acquire_fails_while_held() {
let tmp = TempDir::new().unwrap();
let lock = Lockfile::new("test-app", tmp.path());
let _guard = lock.try_acquire().unwrap();
let lock2 = Lockfile::new("test-app", tmp.path());
let err = lock2
.try_acquire()
.expect_err("should fail while lock is held");
assert!(
matches!(err, librebar::Error::Lock(_)),
"expected Error::Lock, got: {err:?}"
);
}
#[test]
fn lock_dir_default_contains_app_name() {
let dir = librebar::lockfile::default_lock_dir("test-app");
let path = dir.to_string_lossy();
assert!(
path.contains("test-app"),
"lock dir should contain app name: {path}"
);
}