Skip to content

Commit b82df5d

Browse files
authored
Move all unit tests into tests modules. (#1405)
This makes the code more idiomatic. As discussed in rust-lang/cargo#10559, the `use super::*;` pattern makes this style easier to use.
1 parent 9bc3704 commit b82df5d

File tree

15 files changed

+655
-576
lines changed

15 files changed

+655
-576
lines changed

src/backend/libc/fs/dir.rs

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -449,44 +449,49 @@ fn check_dirent_layout(dirent: &c::dirent) {
449449
);
450450
}
451451

452-
#[test]
453-
fn dir_iterator_handles_io_errors() {
454-
// create a dir, keep the FD, then delete the dir
455-
let tmp = tempfile::tempdir().unwrap();
456-
let fd = crate::fs::openat(
457-
crate::fs::CWD,
458-
tmp.path(),
459-
crate::fs::OFlags::RDONLY | crate::fs::OFlags::CLOEXEC,
460-
crate::fs::Mode::empty(),
461-
)
462-
.unwrap();
463-
464-
let file_fd = crate::fs::openat(
465-
&fd,
466-
tmp.path().join("test.txt"),
467-
crate::fs::OFlags::WRONLY | crate::fs::OFlags::CREATE,
468-
crate::fs::Mode::RWXU,
469-
)
470-
.unwrap();
471-
472-
let mut dir = Dir::read_from(&fd).unwrap();
473-
474-
// Reach inside the `Dir` and replace its directory with a file, which
475-
// will cause the subsequent `readdir` to fail.
476-
unsafe {
477-
let raw_fd = c::dirfd(dir.libc_dir.as_ptr());
478-
let mut owned_fd: crate::fd::OwnedFd = crate::fd::FromRawFd::from_raw_fd(raw_fd);
479-
crate::io::dup2(&file_fd, &mut owned_fd).unwrap();
480-
core::mem::forget(owned_fd);
481-
}
452+
#[cfg(test)]
453+
mod tests {
454+
use super::*;
455+
456+
#[test]
457+
fn dir_iterator_handles_io_errors() {
458+
// create a dir, keep the FD, then delete the dir
459+
let tmp = tempfile::tempdir().unwrap();
460+
let fd = crate::fs::openat(
461+
crate::fs::CWD,
462+
tmp.path(),
463+
crate::fs::OFlags::RDONLY | crate::fs::OFlags::CLOEXEC,
464+
crate::fs::Mode::empty(),
465+
)
466+
.unwrap();
467+
468+
let file_fd = crate::fs::openat(
469+
&fd,
470+
tmp.path().join("test.txt"),
471+
crate::fs::OFlags::WRONLY | crate::fs::OFlags::CREATE,
472+
crate::fs::Mode::RWXU,
473+
)
474+
.unwrap();
475+
476+
let mut dir = Dir::read_from(&fd).unwrap();
477+
478+
// Reach inside the `Dir` and replace its directory with a file, which
479+
// will cause the subsequent `readdir` to fail.
480+
unsafe {
481+
let raw_fd = c::dirfd(dir.libc_dir.as_ptr());
482+
let mut owned_fd: crate::fd::OwnedFd = crate::fd::FromRawFd::from_raw_fd(raw_fd);
483+
crate::io::dup2(&file_fd, &mut owned_fd).unwrap();
484+
core::mem::forget(owned_fd);
485+
}
482486

483-
// FreeBSD and macOS seem to read some directory entries before we call
484-
// `.next()`.
485-
#[cfg(any(apple, freebsdlike))]
486-
{
487-
dir.rewind();
488-
}
487+
// FreeBSD and macOS seem to read some directory entries before we call
488+
// `.next()`.
489+
#[cfg(any(apple, freebsdlike))]
490+
{
491+
dir.rewind();
492+
}
489493

490-
assert!(matches!(dir.next(), Some(Err(_))));
491-
assert!(dir.next().is_none());
494+
assert!(matches!(dir.next(), Some(Err(_))));
495+
assert!(dir.next().is_none());
496+
}
492497
}

src/backend/libc/fs/syscalls.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,19 +2663,6 @@ fn fix_negative_stat_nsecs(mut stat: Stat) -> Stat {
26632663
stat
26642664
}
26652665

2666-
#[test]
2667-
fn test_sizes() {
2668-
#[cfg(linux_kernel)]
2669-
assert_eq_size!(c::loff_t, u64);
2670-
2671-
// Assert that `Timestamps` has the expected layout. If we're not fixing
2672-
// y2038, libc's type should match ours. If we are, it's smaller.
2673-
#[cfg(not(fix_y2038))]
2674-
assert_eq_size!([c::timespec; 2], Timestamps);
2675-
#[cfg(fix_y2038)]
2676-
assert!(core::mem::size_of::<[c::timespec; 2]>() < core::mem::size_of::<Timestamps>());
2677-
}
2678-
26792666
#[inline]
26802667
#[cfg(linux_kernel)]
26812668
pub(crate) fn inotify_init1(flags: super::inotify::CreateFlags) -> io::Result<OwnedFd> {
@@ -2711,3 +2698,21 @@ pub(crate) fn inotify_rm_watch(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()>
27112698
// SAFETY: The fd is valid and closing an arbitrary wd is valid.
27122699
unsafe { ret(c::inotify_rm_watch(borrowed_fd(inot), wd)) }
27132700
}
2701+
2702+
#[cfg(test)]
2703+
mod tests {
2704+
use super::*;
2705+
2706+
#[test]
2707+
fn test_sizes() {
2708+
#[cfg(linux_kernel)]
2709+
assert_eq_size!(c::loff_t, u64);
2710+
2711+
// Assert that `Timestamps` has the expected layout. If we're not fixing
2712+
// y2038, libc's type should match ours. If we are, it's smaller.
2713+
#[cfg(not(fix_y2038))]
2714+
assert_eq_size!([c::timespec; 2], Timestamps);
2715+
#[cfg(fix_y2038)]
2716+
assert!(core::mem::size_of::<[c::timespec; 2]>() < core::mem::size_of::<Timestamps>());
2717+
}
2718+
}

src/backend/libc/pipe/types.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,17 @@ impl<'a> IoSliceRaw<'a> {
101101
}
102102
}
103103

104-
#[cfg(not(any(apple, target_os = "wasi")))]
105-
#[test]
106-
fn test_types() {
107-
assert_eq_size!(PipeFlags, c::c_int);
104+
#[cfg(test)]
105+
mod tests {
106+
#[allow(unused_imports)]
107+
use super::*;
108+
109+
#[cfg(not(any(apple, target_os = "wasi")))]
110+
#[test]
111+
fn test_types() {
112+
assert_eq_size!(PipeFlags, c::c_int);
108113

109-
#[cfg(linux_kernel)]
110-
assert_eq_size!(SpliceFlags, c::c_int);
114+
#[cfg(linux_kernel)]
115+
assert_eq_size!(SpliceFlags, c::c_int);
116+
}
111117
}

src/backend/libc/time/types.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,15 @@ pub enum TimerfdClockId {
165165
BoottimeAlarm = bitcast!(c::CLOCK_BOOTTIME_ALARM),
166166
}
167167

168-
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
169-
#[test]
170-
fn test_types() {
171-
assert_eq_size!(TimerfdFlags, c::c_int);
172-
assert_eq_size!(TimerfdTimerFlags, c::c_int);
168+
#[cfg(test)]
169+
mod tests {
170+
#[allow(unused_imports)]
171+
use super::*;
172+
173+
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
174+
#[test]
175+
fn test_types() {
176+
assert_eq_size!(TimerfdFlags, c::c_int);
177+
assert_eq_size!(TimerfdTimerFlags, c::c_int);
178+
}
173179
}

src/backend/linux_raw/fs/dir.rs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -337,32 +337,37 @@ impl DirEntry {
337337
}
338338
}
339339

340-
#[test]
341-
fn dir_iterator_handles_io_errors() {
342-
// create a dir, keep the FD, then delete the dir
343-
let tmp = tempfile::tempdir().unwrap();
344-
let fd = crate::fs::openat(
345-
crate::fs::CWD,
346-
tmp.path(),
347-
crate::fs::OFlags::RDONLY | crate::fs::OFlags::CLOEXEC,
348-
crate::fs::Mode::empty(),
349-
)
350-
.unwrap();
351-
352-
let file_fd = crate::fs::openat(
353-
&fd,
354-
tmp.path().join("test.txt"),
355-
crate::fs::OFlags::WRONLY | crate::fs::OFlags::CREATE,
356-
crate::fs::Mode::RWXU,
357-
)
358-
.unwrap();
359-
360-
let mut dir = Dir::read_from(&fd).unwrap();
361-
362-
// Reach inside the `Dir` and replace its directory with a file, which
363-
// will cause the subsequent `getdents64` to fail.
364-
crate::io::dup2(&file_fd, &mut dir.fd).unwrap();
365-
366-
assert!(matches!(dir.next(), Some(Err(_))));
367-
assert!(dir.next().is_none());
340+
#[cfg(test)]
341+
mod tests {
342+
use super::*;
343+
344+
#[test]
345+
fn dir_iterator_handles_io_errors() {
346+
// create a dir, keep the FD, then delete the dir
347+
let tmp = tempfile::tempdir().unwrap();
348+
let fd = crate::fs::openat(
349+
crate::fs::CWD,
350+
tmp.path(),
351+
crate::fs::OFlags::RDONLY | crate::fs::OFlags::CLOEXEC,
352+
crate::fs::Mode::empty(),
353+
)
354+
.unwrap();
355+
356+
let file_fd = crate::fs::openat(
357+
&fd,
358+
tmp.path().join("test.txt"),
359+
crate::fs::OFlags::WRONLY | crate::fs::OFlags::CREATE,
360+
crate::fs::Mode::RWXU,
361+
)
362+
.unwrap();
363+
364+
let mut dir = Dir::read_from(&fd).unwrap();
365+
366+
// Reach inside the `Dir` and replace its directory with a file, which
367+
// will cause the subsequent `getdents64` to fail.
368+
crate::io::dup2(&file_fd, &mut dir.fd).unwrap();
369+
370+
assert!(matches!(dir.next(), Some(Err(_))));
371+
assert!(dir.next().is_none());
372+
}
368373
}

src/backend/linux_raw/fs/syscalls.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,16 +1657,6 @@ pub(crate) fn fremovexattr(fd: BorrowedFd<'_>, name: &CStr) -> io::Result<()> {
16571657
unsafe { ret(syscall_readonly!(__NR_fremovexattr, fd, name)) }
16581658
}
16591659

1660-
#[test]
1661-
fn test_sizes() {
1662-
assert_eq_size!(linux_raw_sys::general::__kernel_loff_t, u64);
1663-
assert_eq_align!(linux_raw_sys::general::__kernel_loff_t, u64);
1664-
1665-
// Assert that `Timestamps` has the expected layout.
1666-
assert_eq_size!([linux_raw_sys::general::__kernel_timespec; 2], Timestamps);
1667-
assert_eq_align!([linux_raw_sys::general::__kernel_timespec; 2], Timestamps);
1668-
}
1669-
16701660
// Some linux_raw_sys structs have unsigned types for values which are
16711661
// interpreted as signed. This defines a utility or casting to the
16721662
// same-sized signed type.
@@ -1715,3 +1705,18 @@ mod to_signed {
17151705
target_arch = "mips64r6"
17161706
))]
17171707
use to_signed::*;
1708+
1709+
#[cfg(test)]
1710+
mod tests {
1711+
use super::*;
1712+
1713+
#[test]
1714+
fn test_sizes() {
1715+
assert_eq_size!(linux_raw_sys::general::__kernel_loff_t, u64);
1716+
assert_eq_align!(linux_raw_sys::general::__kernel_loff_t, u64);
1717+
1718+
// Assert that `Timestamps` has the expected layout.
1719+
assert_eq_size!([linux_raw_sys::general::__kernel_timespec; 2], Timestamps);
1720+
assert_eq_align!([linux_raw_sys::general::__kernel_timespec; 2], Timestamps);
1721+
}
1722+
}

src/backend/linux_raw/param/libc_auxv.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,6 @@ const _SC_CLK_TCK: c::c_int = 6;
5656
#[cfg(target_os = "linux")]
5757
const _SC_CLK_TCK: c::c_int = 2;
5858

59-
#[test]
60-
fn test_abi() {
61-
const_assert_eq!(self::_SC_PAGESIZE, ::libc::_SC_PAGESIZE);
62-
const_assert_eq!(self::_SC_CLK_TCK, ::libc::_SC_CLK_TCK);
63-
const_assert_eq!(self::AT_HWCAP, ::libc::AT_HWCAP);
64-
const_assert_eq!(self::AT_HWCAP2, ::libc::AT_HWCAP2);
65-
const_assert_eq!(self::AT_EXECFN, ::libc::AT_EXECFN);
66-
const_assert_eq!(self::AT_SECURE, ::libc::AT_SECURE);
67-
const_assert_eq!(self::AT_SYSINFO_EHDR, ::libc::AT_SYSINFO_EHDR);
68-
const_assert_eq!(self::AT_MINSIGSTKSZ, ::libc::AT_MINSIGSTKSZ);
69-
#[cfg(feature = "runtime")]
70-
const_assert_eq!(self::AT_PHDR, ::libc::AT_PHDR);
71-
#[cfg(feature = "runtime")]
72-
const_assert_eq!(self::AT_PHNUM, ::libc::AT_PHNUM);
73-
#[cfg(feature = "runtime")]
74-
const_assert_eq!(self::AT_ENTRY, ::libc::AT_ENTRY);
75-
#[cfg(feature = "runtime")]
76-
const_assert_eq!(self::AT_RANDOM, ::libc::AT_RANDOM);
77-
}
78-
7959
#[cfg(feature = "param")]
8060
#[inline]
8161
pub(crate) fn page_size() -> usize {
@@ -191,3 +171,28 @@ pub(crate) fn entry() -> usize {
191171
pub(crate) fn random() -> *const [u8; 16] {
192172
unsafe { getauxval(AT_RANDOM) as *const [u8; 16] }
193173
}
174+
175+
#[cfg(test)]
176+
mod tests {
177+
use super::*;
178+
179+
#[test]
180+
fn test_abi() {
181+
const_assert_eq!(self::_SC_PAGESIZE, ::libc::_SC_PAGESIZE);
182+
const_assert_eq!(self::_SC_CLK_TCK, ::libc::_SC_CLK_TCK);
183+
const_assert_eq!(self::AT_HWCAP, ::libc::AT_HWCAP);
184+
const_assert_eq!(self::AT_HWCAP2, ::libc::AT_HWCAP2);
185+
const_assert_eq!(self::AT_EXECFN, ::libc::AT_EXECFN);
186+
const_assert_eq!(self::AT_SECURE, ::libc::AT_SECURE);
187+
const_assert_eq!(self::AT_SYSINFO_EHDR, ::libc::AT_SYSINFO_EHDR);
188+
const_assert_eq!(self::AT_MINSIGSTKSZ, ::libc::AT_MINSIGSTKSZ);
189+
#[cfg(feature = "runtime")]
190+
const_assert_eq!(self::AT_PHDR, ::libc::AT_PHDR);
191+
#[cfg(feature = "runtime")]
192+
const_assert_eq!(self::AT_PHNUM, ::libc::AT_PHNUM);
193+
#[cfg(feature = "runtime")]
194+
const_assert_eq!(self::AT_ENTRY, ::libc::AT_ENTRY);
195+
#[cfg(feature = "runtime")]
196+
const_assert_eq!(self::AT_RANDOM, ::libc::AT_RANDOM);
197+
}
198+
}

0 commit comments

Comments
 (0)