tests: use tempfile::TempDir instead of env::temp_dir() + random number

Otherwise /tmp/test-<random> file would be left.
This commit is contained in:
Yuya Nishihara 2022-09-07 13:16:35 +09:00
parent 3b835df66e
commit 31e90bdede

View File

@ -72,16 +72,17 @@ impl Drop for FileLock {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::cmp::max; use std::cmp::max;
use std::{env, thread}; use std::thread;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use super::*; use super::*;
use crate::testutils;
#[test] #[test]
fn lock_basic() { fn lock_basic() {
let number: u32 = rand::random(); let temp_dir = testutils::new_temp_dir();
let lock_path = env::temp_dir().join(format!("test-{}.lock", number)); let lock_path = temp_dir.path().join("test.lock");
assert!(!lock_path.exists()); assert!(!lock_path.exists());
{ {
let _lock = FileLock::lock(lock_path.clone()); let _lock = FileLock::lock(lock_path.clone());
@ -92,9 +93,9 @@ mod tests {
#[test] #[test]
fn lock_concurrent() { fn lock_concurrent() {
let number: u32 = rand::random(); let temp_dir = testutils::new_temp_dir();
let data_path = env::temp_dir().join(format!("test-{}", number)); let data_path = temp_dir.path().join("test");
let lock_path = env::temp_dir().join(format!("test-{}.lock", number)); let lock_path = temp_dir.path().join("test.lock");
let mut data_file = OpenOptions::new() let mut data_file = OpenOptions::new()
.create(true) .create(true)
.write(true) .write(true)