-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathfiles.rs
More file actions
173 lines (149 loc) · 5.48 KB
/
files.rs
File metadata and controls
173 lines (149 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use super::checker::{CheckDeserialization, Checker};
/// A file that is used as an input to for a benchmark.
///
/// When used during input deserialization, with a [`Checker`], the following actions will be
/// taken:
///
/// 1. If the path is absolute or points to an existing file relative to the current working
/// directory, no additional measure will be taken.
///
/// 2. If the path is not absolute, then every directory in [`Checker::search_directories()`]
/// will be explored in-order. The first directory where `path` exists will be selected.
///
/// 3. If all these steps fail, then post-deserialization checking will fail with an error.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(transparent)]
pub struct InputFile {
path: PathBuf,
}
impl InputFile {
/// Create a new new input file from the path-like `path``
pub fn new<P>(path: P) -> Self
where
PathBuf: From<P>,
{
Self {
path: PathBuf::from(path),
}
}
}
impl std::ops::Deref for InputFile {
type Target = Path;
fn deref(&self) -> &Self::Target {
&self.path
}
}
impl CheckDeserialization for InputFile {
fn check_deserialization(&mut self, checker: &mut Checker) -> Result<(), anyhow::Error> {
let checked_path = checker.check_path(self);
match checked_path {
Ok(p) => {
self.path = p;
Ok(())
}
Err(e) => Err(e),
}
}
}
impl AsRef<Path> for InputFile {
fn as_ref(&self) -> &Path {
self
}
}
///////////
// Tests //
///////////
#[cfg(test)]
mod tests {
use std::fs::{create_dir, File};
use super::*;
#[test]
fn test_input_file() {
let file = InputFile::new("hello/world");
let file_deref: &Path = &file;
assert_eq!(file_deref.to_str().unwrap(), "hello/world");
}
#[test]
fn test_serialization() {
let st: &str = "\"path/to/directory\"";
let file: InputFile = serde_json::from_str(st).unwrap();
assert_eq!(file.to_str().unwrap(), st.trim_matches('\"'));
assert_eq!(&*serde_json::to_string(&file).unwrap(), st);
}
#[test]
fn test_check_deserialization() {
// We create a directory that looks like this:
//
// dir/
// file_a.txt
// dir0/
// file_b.txt
// dir1/
// file_c.txt
// dir0/
// file_c.txt
let dir = tempfile::tempdir().unwrap();
let path = dir.path();
File::create(path.join("file_a.txt")).unwrap();
create_dir(path.join("dir0")).unwrap();
create_dir(path.join("dir1")).unwrap();
create_dir(path.join("dir1/dir0")).unwrap();
File::create(path.join("dir0/file_b.txt")).unwrap();
File::create(path.join("dir1/file_c.txt")).unwrap();
File::create(path.join("dir1/dir0/file_c.txt")).unwrap();
// Test absolute path success.
{
let absolute = path.join("file_a.txt");
let mut file = InputFile::new(absolute.clone());
let mut checker = Checker::new(Vec::new(), None);
file.check_deserialization(&mut checker).unwrap();
assert_eq!(file.path, absolute);
let absolute = path.join("dir0/file_b.txt");
let mut file = InputFile::new(absolute.clone());
let mut checker = Checker::new(Vec::new(), None);
file.check_deserialization(&mut checker).unwrap();
assert_eq!(file.path, absolute);
}
// Absolute path fail.
{
let absolute = path.join("dir0/file_c.txt");
let mut file = InputFile::new(absolute.clone());
let mut checker = Checker::new(Vec::new(), None);
let err = file.check_deserialization(&mut checker).unwrap_err();
let message = err.to_string();
assert!(message.contains("input file with absolute path"));
assert!(message.contains("either does not exist or is not a file"));
}
// Directory search
{
let mut checker = Checker::new(
vec![path.join("dir1/dir0"), path.join("dir1"), path.join("dir0")],
None,
);
// Directories are searched in order.
let mut file = InputFile::new("file_c.txt");
file.check_deserialization(&mut checker).unwrap();
assert_eq!(file.path, path.join("dir1/dir0/file_c.txt"));
let mut file = InputFile::new("file_b.txt");
file.check_deserialization(&mut checker).unwrap();
assert_eq!(file.path, path.join("dir0/file_b.txt"));
// Directory search can fail.
let mut file = InputFile::new("file_a.txt");
let err = file.check_deserialization(&mut checker).unwrap_err();
let message = err.to_string();
assert!(message.contains("could not find input file"));
assert!(message.contains("in the search directories"));
// If we give an absolute path, no directory search is performed.
let mut file = InputFile::new(path.join("file_c.txt"));
let err = file.check_deserialization(&mut checker).unwrap_err();
let message = err.to_string();
assert!(message.starts_with("input file with absolute path"));
}
}
}