Skip to content

Commit a95a432

Browse files
committed
Auto merge of #2997 - RalfJung:test-utils, r=RalfJung
refactor tests/utils a bit, and move some FS functions there
2 parents 2183cda + e565624 commit a95a432

File tree

16 files changed

+77
-112
lines changed

16 files changed

+77
-112
lines changed

src/tools/miri/tests/fail/tree_borrows/reserved/cell-protected-write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// Check how a Reserved with interior mutability
44
// responds to a Foreign Write under a Protector
55
#[path = "../../../utils/mod.rs"]
6+
#[macro_use]
67
mod utils;
7-
use utils::macros::*;
88

99
use std::cell::UnsafeCell;
1010

src/tools/miri/tests/fail/tree_borrows/reserved/int-protected-write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0
22

33
#[path = "../../../utils/mod.rs"]
4+
#[macro_use]
45
mod utils;
5-
use utils::macros::*;
66

77
// Check how a Reserved without interior mutability responds to a Foreign
88
// Write when under a protector

src/tools/miri/tests/pass-dep/shims/libc-fs.rs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
#![feature(io_error_uncategorized)]
66

77
use std::convert::TryInto;
8-
use std::ffi::{c_char, CStr, CString};
8+
use std::ffi::CString;
99
use std::fs::{canonicalize, remove_dir_all, remove_file, File};
1010
use std::io::{Error, ErrorKind, Write};
1111
use std::os::unix::ffi::OsStrExt;
1212
use std::path::PathBuf;
1313

14+
#[path = "../../utils/mod.rs"]
15+
mod utils;
16+
1417
fn main() {
1518
test_dup_stdout_stderr();
1619
test_canonicalize_too_long();
@@ -22,31 +25,9 @@ fn main() {
2225
test_o_tmpfile_flag();
2326
}
2427

25-
fn tmp() -> PathBuf {
26-
let path = std::env::var("MIRI_TEMP")
27-
.unwrap_or_else(|_| std::env::temp_dir().into_os_string().into_string().unwrap());
28-
// These are host paths. We need to convert them to the target.
29-
let path = CString::new(path).unwrap();
30-
let mut out = Vec::with_capacity(1024);
31-
32-
unsafe {
33-
extern "Rust" {
34-
fn miri_host_to_target_path(
35-
path: *const c_char,
36-
out: *mut c_char,
37-
out_size: usize,
38-
) -> usize;
39-
}
40-
let ret = miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
41-
assert_eq!(ret, 0);
42-
let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
43-
PathBuf::from(out)
44-
}
45-
}
46-
4728
/// Prepare: compute filename and make sure the file does not exist.
4829
fn prepare(filename: &str) -> PathBuf {
49-
let path = tmp().join(filename);
30+
let path = utils::tmp().join(filename);
5031
// Clean the paths for robustness.
5132
remove_file(&path).ok();
5233
path
@@ -55,7 +36,7 @@ fn prepare(filename: &str) -> PathBuf {
5536
/// Prepare directory: compute directory name and make sure it does not exist.
5637
#[allow(unused)]
5738
fn prepare_dir(dirname: &str) -> PathBuf {
58-
let path = tmp().join(&dirname);
39+
let path = utils::tmp().join(&dirname);
5940
// Clean the directory for robustness.
6041
remove_dir_all(&path).ok();
6142
path

src/tools/miri/tests/pass-dep/shims/libc-misc.rs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,8 @@ use std::fs::{remove_file, File};
66
use std::os::unix::io::AsRawFd;
77
use std::path::PathBuf;
88

9-
fn tmp() -> PathBuf {
10-
use std::ffi::{c_char, CStr, CString};
11-
12-
let path = std::env::var("MIRI_TEMP")
13-
.unwrap_or_else(|_| std::env::temp_dir().into_os_string().into_string().unwrap());
14-
// These are host paths. We need to convert them to the target.
15-
let path = CString::new(path).unwrap();
16-
let mut out = Vec::with_capacity(1024);
17-
18-
unsafe {
19-
extern "Rust" {
20-
fn miri_host_to_target_path(
21-
path: *const c_char,
22-
out: *mut c_char,
23-
out_size: usize,
24-
) -> usize;
25-
}
26-
let ret = miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
27-
assert_eq!(ret, 0);
28-
let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
29-
PathBuf::from(out)
30-
}
31-
}
9+
#[path = "../../utils/mod.rs"]
10+
mod utils;
3211

3312
/// Test allocating variant of `realpath`.
3413
fn test_posix_realpath_alloc() {
@@ -38,7 +17,7 @@ fn test_posix_realpath_alloc() {
3817
use std::os::unix::ffi::OsStringExt;
3918

4019
let buf;
41-
let path = tmp().join("miri_test_libc_posix_realpath_alloc");
20+
let path = utils::tmp().join("miri_test_libc_posix_realpath_alloc");
4221
let c_path = CString::new(path.as_os_str().as_bytes()).expect("CString::new failed");
4322

4423
// Cleanup before test.
@@ -63,7 +42,7 @@ fn test_posix_realpath_noalloc() {
6342
use std::ffi::{CStr, CString};
6443
use std::os::unix::ffi::OsStrExt;
6544

66-
let path = tmp().join("miri_test_libc_posix_realpath_noalloc");
45+
let path = utils::tmp().join("miri_test_libc_posix_realpath_noalloc");
6746
let c_path = CString::new(path.as_os_str().as_bytes()).expect("CString::new failed");
6847

6948
let mut v = vec![0; libc::PATH_MAX as usize];
@@ -103,7 +82,7 @@ fn test_posix_realpath_errors() {
10382
fn test_posix_fadvise() {
10483
use std::io::Write;
10584

106-
let path = tmp().join("miri_test_libc_posix_fadvise.txt");
85+
let path = utils::tmp().join("miri_test_libc_posix_fadvise.txt");
10786
// Cleanup before test
10887
remove_file(&path).ok();
10988

@@ -130,7 +109,7 @@ fn test_posix_fadvise() {
130109
fn test_sync_file_range() {
131110
use std::io::Write;
132111

133-
let path = tmp().join("miri_test_libc_sync_file_range.txt");
112+
let path = utils::tmp().join("miri_test_libc_sync_file_range.txt");
134113
// Cleanup before test.
135114
remove_file(&path).ok();
136115

@@ -243,7 +222,7 @@ fn test_isatty() {
243222
libc::isatty(libc::STDERR_FILENO);
244223

245224
// But when we open a file, it is definitely not a TTY.
246-
let path = tmp().join("notatty.txt");
225+
let path = utils::tmp().join("notatty.txt");
247226
// Cleanup before test.
248227
remove_file(&path).ok();
249228
let file = File::create(&path).unwrap();

src/tools/miri/tests/pass/shims/fs.rs

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
#![feature(io_error_uncategorized)]
66

77
use std::collections::HashMap;
8-
use std::ffi::{c_char, OsString};
8+
use std::ffi::OsString;
99
use std::fs::{
1010
canonicalize, create_dir, read_dir, read_link, remove_dir, remove_dir_all, remove_file, rename,
1111
File, OpenOptions,
1212
};
1313
use std::io::{Error, ErrorKind, IsTerminal, Read, Result, Seek, SeekFrom, Write};
1414
use std::path::{Path, PathBuf};
1515

16+
#[path = "../../utils/mod.rs"]
17+
mod utils;
18+
1619
fn main() {
1720
test_path_conversion();
1821
test_file();
@@ -30,45 +33,17 @@ fn main() {
3033
test_from_raw_os_error();
3134
}
3235

33-
fn host_to_target_path(path: String) -> PathBuf {
34-
use std::ffi::{CStr, CString};
35-
36-
let path = CString::new(path).unwrap();
37-
let mut out = Vec::with_capacity(1024);
38-
39-
unsafe {
40-
extern "Rust" {
41-
fn miri_host_to_target_path(
42-
path: *const c_char,
43-
out: *mut c_char,
44-
out_size: usize,
45-
) -> usize;
46-
}
47-
let ret = miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
48-
assert_eq!(ret, 0);
49-
let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
50-
PathBuf::from(out)
51-
}
52-
}
53-
54-
fn tmp() -> PathBuf {
55-
let path = std::env::var("MIRI_TEMP")
56-
.unwrap_or_else(|_| std::env::temp_dir().into_os_string().into_string().unwrap());
57-
// These are host paths. We need to convert them to the target.
58-
host_to_target_path(path)
59-
}
60-
6136
/// Prepare: compute filename and make sure the file does not exist.
6237
fn prepare(filename: &str) -> PathBuf {
63-
let path = tmp().join(filename);
38+
let path = utils::tmp().join(filename);
6439
// Clean the paths for robustness.
6540
remove_file(&path).ok();
6641
path
6742
}
6843

6944
/// Prepare directory: compute directory name and make sure it does not exist.
7045
fn prepare_dir(dirname: &str) -> PathBuf {
71-
let path = tmp().join(&dirname);
46+
let path = utils::tmp().join(&dirname);
7247
// Clean the directory for robustness.
7348
remove_dir_all(&path).ok();
7449
path
@@ -83,7 +58,7 @@ fn prepare_with_content(filename: &str, content: &[u8]) -> PathBuf {
8358
}
8459

8560
fn test_path_conversion() {
86-
let tmp = tmp();
61+
let tmp = utils::tmp();
8762
assert!(tmp.is_absolute(), "{:?} is not absolute", tmp);
8863
assert!(tmp.is_dir(), "{:?} is not a directory", tmp);
8964
}

src/tools/miri/tests/pass/tree_borrows/cell-alternate-writes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0
22
#[path = "../../utils/mod.rs"]
3+
#[macro_use]
34
mod utils;
4-
use utils::macros::*;
55

66
use std::cell::UnsafeCell;
77

src/tools/miri/tests/pass/tree_borrows/end-of-protector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// Check that a protector goes back to normal behavior when the function
44
// returns.
55
#[path = "../../utils/mod.rs"]
6+
#[macro_use]
67
mod utils;
7-
use utils::macros::*;
88

99
fn main() {
1010
unsafe {

src/tools/miri/tests/pass/tree_borrows/formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0
22

33
#[path = "../../utils/mod.rs"]
4+
#[macro_use]
45
mod utils;
5-
use utils::macros::*;
66

77
// Check the formatting of the trees.
88
fn main() {

src/tools/miri/tests/pass/tree_borrows/reborrow-is-read.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0
22

33
#[path = "../../utils/mod.rs"]
4+
#[macro_use]
45
mod utils;
5-
use utils::macros::*;
66

77
// To check that a reborrow is counted as a Read access, we use a reborrow
88
// with no additional Read to Freeze an Active pointer.

src/tools/miri/tests/pass/tree_borrows/reserved.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0
22

33
#[path = "../../utils/mod.rs"]
4+
#[macro_use]
45
mod utils;
5-
use utils::macros::*;
6-
use utils::miri_extern::miri_write_to_stderr;
76

87
use std::cell::UnsafeCell;
98

@@ -28,8 +27,8 @@ fn main() {
2827
}
2928

3029
unsafe fn print(msg: &str) {
31-
miri_write_to_stderr(msg.as_bytes());
32-
miri_write_to_stderr("\n".as_bytes());
30+
utils::miri_write_to_stderr(msg.as_bytes());
31+
utils::miri_write_to_stderr("\n".as_bytes());
3332
}
3433

3534
unsafe fn read_second<T>(x: &mut T, y: *mut u8) {

0 commit comments

Comments
 (0)