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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::{implementation, private, Asserter};
use std::{
fs::{self, File},
path::Path,
};
/// Specifies various assertions on file system paths. Implemented on [`Asserter`]
///
/// This trait is sealed and cannot be implemented outside Smoothy.
pub trait PathAssertion<PathLike>: private::Sealed
where
PathLike: AsRef<Path>,
{
/// Asserts that the path exists in the file system
///
/// It does not matter if the path points to an existing file or directory.
/// For more see [`fs::exists`].
///
/// # Examples
/// ```
/// # use smoothy::prelude::*;
/// # use tempfile::NamedTempFile;
/// let temp_file = NamedTempFile::new().unwrap();
///
/// assert_that(temp_file.path()).exists();
/// ```
///
/// ```should_panic
/// # use smoothy::prelude::*;
/// #
/// assert_that("/path/that/does/not/exist").exists();
/// ```
///
/// # Panics
/// When the path does not exist in the file system
#[track_caller]
fn exists(self) -> Asserter<File>;
/// Asserts that the path does not exist in the file system
///
/// For more see [`fs::exists`].
///
/// # Examples
/// ```
/// # use smoothy::prelude::*;
/// #
/// assert_that("/path/that/does/not/exist").not_exists();
/// ```
///
/// ```should_panic
/// # use smoothy::prelude::*;
/// # use tempfile::NamedTempFile;
/// let temp_file = NamedTempFile::new().unwrap();
///
/// assert_that(temp_file.path()).not_exists();
/// ```
///
/// # Panics
/// When the path exists in the file system
#[track_caller]
fn not_exists(self);
/// Asserts that the path is a symlink
///
/// This checks the metadata of the path without following symlinks to determine
/// if it represents a symbolic link. Use this to verify that a path is actually
/// a symlink, not just what it points to.
///
/// # Examples
/// ```
/// # use smoothy::prelude::*;
/// # use std::os::unix::fs::symlink;
/// # use std::fs::File;
/// # use tempfile::TempDir;
/// # let temp_dir = TempDir::new().unwrap();
/// # let target = temp_dir.path().join("target");
/// # File::create(&target).unwrap();
/// # let link = temp_dir.path().join("link");
/// symlink(&target, &link).unwrap();
///
/// assert_that(&link).is_symlink();
/// ```
///
/// # Panics
/// When the path is not a symlink or does not exist
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_symlink(self) -> Asserter<File>;
/// Asserts that the path points to a regular file
///
/// This checks the metadata of the path without following symlinks. If you want
/// to check if a symlink's target is a file, use [`exists`](PathAssertion::exists)
/// followed by [`is_file`](crate::FileAssertion::is_file).
///
/// # Examples
/// ```
/// # use smoothy::prelude::*;
/// # use tempfile::NamedTempFile;
/// let temp_file = NamedTempFile::new().unwrap();
///
/// assert_that(temp_file.path()).is_file();
/// ```
///
/// # Panics
/// When the path does not point to a regular file or does not exist
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_file(self) -> Asserter<File>;
/// Asserts that the path points to a directory
///
/// This checks the metadata of the path without following symlinks. If you want
/// to check if a symlink's target is a directory, use [`exists`](PathAssertion::exists)
/// followed by [`is_directory`](crate::FileAssertion::is_directory).
///
/// # Examples
/// ```
/// # use smoothy::prelude::*;
/// # use tempfile::TempDir;
/// let temp_dir = TempDir::new().unwrap();
///
/// assert_that(temp_dir.path()).is_directory();
/// ```
///
/// # Panics
/// When the path does not point to a directory or does not exist
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_directory(self) -> Asserter<File>;
}
impl<PathLike> PathAssertion<PathLike> for Asserter<PathLike>
where
PathLike: AsRef<Path>,
{
#[allow(clippy::expect_used)]
fn exists(self) -> Asserter<File> {
let path = self.value.as_ref();
implementation::assert_no_expected(
fs::exists(path).expect("Failed to check if path exists"),
path,
"to point at an existing entity in the filesystem",
);
Asserter {
value: File::open(path).expect("Failed to open file"),
}
}
#[allow(clippy::expect_used)]
fn not_exists(self) {
let path = self.value.as_ref();
implementation::assert_no_expected(
!fs::exists(path).expect("Failed to check if path exists"),
path,
"to point at an non existing entity in the filesystem",
);
}
#[allow(clippy::expect_used)]
fn is_symlink(self) -> Asserter<File> {
let path = self.value.as_ref();
let metadata = fs::symlink_metadata(path);
implementation::assert_no_expected(metadata.is_ok(), path, "to exist");
#[allow(clippy::unwrap_used)]
implementation::assert_no_expected(metadata.unwrap().is_symlink(), path, "to be a symlink");
Asserter {
value: File::open(path).expect("Failed to open file"),
}
}
#[allow(clippy::expect_used)]
fn is_file(self) -> Asserter<File> {
let path = self.value.as_ref();
let metadata = fs::symlink_metadata(path);
implementation::assert_no_expected(metadata.is_ok(), path, "to exist");
#[allow(clippy::unwrap_used)]
implementation::assert_no_expected(
metadata.unwrap().is_file(),
path,
"to be a regular file",
);
Asserter {
value: File::open(path).expect("Failed to open file"),
}
}
#[allow(clippy::expect_used)]
fn is_directory(self) -> Asserter<File> {
let path = self.value.as_ref();
let metadata = fs::symlink_metadata(path);
implementation::assert_no_expected(metadata.is_ok(), path, "to exist");
#[allow(clippy::unwrap_used)]
implementation::assert_no_expected(metadata.unwrap().is_dir(), path, "to be a directory");
Asserter {
value: File::open(path).expect("Failed to open file"),
}
}
}