Use tempdir for temporary files in tests

This commit is contained in:
Kamal Marhubi 2015-12-27 22:21:54 +04:00 committed by Carl Lerche
parent 67f695a051
commit 4450b96399
4 changed files with 31 additions and 33 deletions

View File

@ -25,6 +25,7 @@ bitflags = "0.3.2"
[dev-dependencies]
rand = "0.3.8"
tempdir = "0.3"
[dev-dependencies.nix-test]
path = "nix-test"

View File

@ -5,6 +5,8 @@ use std::{cmp, iter};
use std::fs::{OpenOptions, remove_file};
use std::os::unix::io::AsRawFd;
use tempdir::TempDir;
#[test]
fn test_writev() {
let mut to_write = Vec::with_capacity(16 * 128);
@ -115,7 +117,9 @@ fn test_pwrite() {
fn test_pread() {
use std::io::Write;
let path = "pread_test_file";
let tempdir = TempDir::new("nix-test_pread").unwrap();
let path = tempdir.path().join("pread_test_file");
let mut file = OpenOptions::new().write(true).read(true).create(true)
.truncate(true).open(path).unwrap();
let file_content: Vec<u8> = (0..64).collect();
@ -125,8 +129,6 @@ fn test_pread() {
assert_eq!(Ok(16), pread(file.as_raw_fd(), &mut buf, 16));
let expected: Vec<_> = (16..32).collect();
assert_eq!(&buf[..], &expected[..]);
remove_file(path).unwrap();
}
#[test]
@ -143,8 +145,10 @@ fn test_pwritev() {
IoVec::from_slice(&to_write[64..128]),
];
let tempdir = TempDir::new("nix-test_pwritev").unwrap();
// pwritev them into a temporary file
let path = "pwritev_test_file";
let path = tempdir.path().join("pwritev_test_file");
let mut file = OpenOptions::new().write(true).read(true).create(true)
.truncate(true).open(path).unwrap();
@ -155,8 +159,6 @@ fn test_pwritev() {
let mut contents = Vec::new();
file.read_to_end(&mut contents).unwrap();
assert_eq!(contents, expected);
remove_file(path).unwrap();
}
#[test]
@ -167,7 +169,9 @@ fn test_preadv() {
let to_write: Vec<u8> = (0..200).collect();
let expected: Vec<u8> = (100..200).collect();
let path = "preadv_test_file";
let tempdir = TempDir::new("nix-test_preadv").unwrap();
let path = tempdir.path().join("preadv_test_file");
let mut file = OpenOptions::new().read(true).write(true).create(true)
.truncate(true).open(path).unwrap();
@ -188,6 +192,4 @@ fn test_preadv() {
let all = buffers.concat();
assert_eq!(all, expected);
remove_file(path).unwrap();
}

View File

@ -2,6 +2,7 @@
extern crate nix;
extern crate libc;
extern crate rand;
extern crate tempdir;
extern crate nix_test as nixtest;

View File

@ -1,15 +1,14 @@
use std::fs::File;
use std::os::unix::fs::symlink;
use std::str;
use std::os::unix::prelude::AsRawFd;
use libc::consts::os::posix88;
use nix::sys::stat::{stat, fstat, lstat};
use nix::fcntl::open;
use nix::unistd::{close, unlink};
use nix::fcntl::{O_CREAT, O_RDONLY};
use nix::sys::stat::{FileStat, S_IWUSR, S_IRUSR};
use nix::sys::stat::FileStat;
use nix::Result;
use tempdir::TempDir;
#[allow(unused_comparisons)]
// uid and gid are signed on Windows, but not on other platforms. This function
@ -65,40 +64,35 @@ fn assert_lstat_results(stat_result: Result<FileStat>) {
#[test]
fn test_stat_and_fstat() {
let filename = b"target/foo.txt".as_ref();
let fd = open(filename, O_CREAT, S_IWUSR).unwrap(); // create empty file
let tempdir = TempDir::new("nix-test_stat_and_fstat").unwrap();
let filename = tempdir.path().join("foo.txt");
let file = File::create(&filename).unwrap();
let stat_result = stat(filename);
let stat_result = stat(&filename);
assert_stat_results(stat_result);
let fstat_result = fstat(fd);
let fstat_result = fstat(file.as_raw_fd());
assert_stat_results(fstat_result);
close(fd).unwrap();
unlink(filename).unwrap();
}
#[test]
fn test_stat_fstat_lstat() {
let filename = b"target/bar.txt".as_ref();
let linkname = b"target/barlink".as_ref();
let tempdir = TempDir::new("nix-test_stat_fstat_lstat").unwrap();
let filename = tempdir.path().join("bar.txt");
let linkname = tempdir.path().join("barlink");
open(filename, O_CREAT, S_IWUSR | S_IRUSR).unwrap(); // create empty file
symlink("bar.txt", str::from_utf8(linkname).unwrap()).unwrap();
let fd = open(linkname, O_RDONLY, S_IRUSR).unwrap();
File::create(&filename).unwrap();
symlink("bar.txt", &linkname).unwrap();
let link = File::open(&linkname).unwrap();
// should be the same result as calling stat,
// since it's a regular file
let stat_result = lstat(filename);
let stat_result = lstat(&filename);
assert_stat_results(stat_result);
let lstat_result = lstat(linkname);
let lstat_result = lstat(&linkname);
assert_lstat_results(lstat_result);
let fstat_result = fstat(fd);
let fstat_result = fstat(link.as_raw_fd());
assert_stat_results(fstat_result);
close(fd).unwrap();
unlink(linkname).unwrap();
unlink(filename).unwrap();
}