Skip to content

Commit b20b6c8

Browse files
Markus Jaiscarllerche
authored andcommitted
added test for fstat
1 parent 0811c8c commit b20b6c8

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

test/test_stat.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
use nix::sys::stat::stat;
1+
use nix::sys::stat::{stat, fstat};
22

3-
#[test]
4-
fn test_stat() {
5-
use nix::fcntl::open;
6-
use nix::unistd::{close, unlink};
7-
use nix::fcntl::O_CREAT;
8-
use nix::sys::stat::S_IWUSR;
9-
10-
let filename = b"target/foo.txt";
11-
let fd_res = open(filename, O_CREAT, S_IWUSR).unwrap();
12-
close(fd_res).unwrap(); //.ok().unwrap()); // close right here. We use the file only for the stat test
13-
let stat_result = stat(filename);
3+
use nix::fcntl::open;
4+
use nix::unistd::{close, unlink};
5+
use nix::fcntl::O_CREAT;
6+
use nix::sys::stat::{FileStat, S_IWUSR};
7+
use nix::NixResult;
148

9+
fn assert_stat_results(stat_result: NixResult<FileStat>) {
1510
match stat_result {
1611
Ok(stats) => {
1712
assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent
@@ -27,5 +22,20 @@ fn test_stat() {
2722
}
2823
Err(_) => panic!("stat call failed") // if stats system call fails, something is seriously wrong on that machine
2924
}
25+
}
26+
27+
28+
#[test]
29+
fn test_stat_and_fstat() {
30+
let filename = b"target/foo.txt";
31+
let fd = open(filename, O_CREAT, S_IWUSR).unwrap(); // create empty file
32+
33+
let stat_result = stat(filename);
34+
assert_stat_results(stat_result);
35+
36+
let fstat_result = fstat(fd);
37+
assert_stat_results(fstat_result);
38+
39+
close(fd).unwrap();
3040
unlink(filename).unwrap();
3141
}

0 commit comments

Comments
 (0)