Skip to content

Commit 2f57fa5

Browse files
authored
Improve tests (#86)
1 parent d57d884 commit 2f57fa5

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

justfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
watch +args='test':
1+
watch +args='ltest':
22
cargo watch --clear --exec '{{ args }}'
33

4+
clippy: (watch 'lclippy --all-targets -- --deny warnings')
5+
46
ci: lint
57
cargo test --workspace
68

tests/create.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,38 +171,41 @@ fn file_in_subdirectory() {
171171
.success();
172172
}
173173

174-
// disable test on macos, since it does not allow non-unicode filenames
175-
#[cfg(not(target_os = "macos"))]
176174
#[test]
177175
fn non_unicode_path_error() {
178176
use std::path::PathBuf;
179177

178+
// macos does not allow non-unicode filenames
179+
if cfg!(target_os = "macos") {
180+
return;
181+
}
182+
180183
let dir = TempDir::new().unwrap();
181184

182-
let path: PathBuf;
185+
let invalid: PathBuf;
183186

184187
#[cfg(unix)]
185188
{
186189
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
187190

188-
path = OsStr::from_bytes(&[0x80]).into();
191+
invalid = OsStr::from_bytes(&[0x80]).into();
189192
};
190193

191194
#[cfg(windows)]
192195
{
193196
use std::{ffi::OsString, os::windows::ffi::OsStringExt};
194197

195-
path = OsString::from_wide(&[0xd800]).into();
198+
invalid = OsString::from_wide(&[0xd800]).into();
196199
};
197200

198-
dir.child(path).touch().unwrap();
201+
dir.child(invalid).touch().unwrap();
199202

200203
Command::cargo_bin("filepack")
201204
.unwrap()
202205
.args(["create", "."])
203206
.current_dir(&dir)
204207
.assert()
205-
.stderr(format!("error: path not valid unicode: `.{SEPARATOR}�`\n"))
208+
.stderr(path("error: path not valid unicode: `./�`\n"))
206209
.failure();
207210
}
208211

@@ -268,13 +271,16 @@ fn only_leaf_empty_directory_is_reported() {
268271
.args(["create", "."])
269272
.current_dir(&dir)
270273
.assert()
271-
.stderr(format!("error: empty directory `foo{SEPARATOR}bar`\n"))
274+
.stderr(path("error: empty directory `foo/bar`\n"))
272275
.failure();
273276
}
274277

275-
#[cfg(not(windows))]
276278
#[test]
277279
fn backslash_error() {
280+
if cfg!(windows) {
281+
return;
282+
}
283+
278284
let dir = TempDir::new().unwrap();
279285

280286
dir.child("\\").touch().unwrap();
@@ -293,9 +299,12 @@ error: invalid path `\\`
293299
.failure();
294300
}
295301

296-
#[cfg(all(not(windows), not(target_os = "macos")))]
297302
#[test]
298303
fn deny_case_insensitive_filesystem_path_conflict() {
304+
if cfg!(windows) || cfg!(target_os = "macos") {
305+
return;
306+
}
307+
299308
let dir = TempDir::new().unwrap();
300309

301310
dir.child("foo").touch().unwrap();
@@ -317,9 +326,12 @@ error: 1 lint error
317326
.failure();
318327
}
319328

320-
#[cfg(not(windows))]
321329
#[test]
322330
fn deny_lint() {
331+
if cfg!(windows) {
332+
return;
333+
}
334+
323335
let dir = TempDir::new().unwrap();
324336

325337
dir.child("aux").touch().unwrap();
@@ -339,9 +351,12 @@ error: 1 lint error
339351
.failure();
340352
}
341353

342-
#[cfg(not(windows))]
343354
#[test]
344355
fn allow_lint() {
356+
if cfg!(windows) {
357+
return;
358+
}
359+
345360
let dir = TempDir::new().unwrap();
346361

347362
dir.child("aux").touch().unwrap();
@@ -617,9 +632,7 @@ fn metadata_already_exists() {
617632
.args(["create", "foo", "--metadata", "metadata.yaml"])
618633
.current_dir(&dir)
619634
.assert()
620-
.stderr(format!(
621-
"error: metadata `foo{SEPARATOR}metadata.json` already exists\n"
622-
))
635+
.stderr(path("error: metadata `foo/metadata.json` already exists\n"))
623636
.failure();
624637

625638
Command::cargo_bin("filepack")

tests/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use {
1010
std::{collections::BTreeMap, fs, path::Path, str},
1111
};
1212

13-
const SEPARATOR: char = if cfg!(windows) { '\\' } else { '/' };
13+
fn path(message: &str) -> String {
14+
message.replace('/', std::path::MAIN_SEPARATOR_STR)
15+
}
1416

1517
fn is_match<S>(pattern: S) -> RegexPredicate
1618
where

tests/verify.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn only_leaf_empty_directory_is_reported() {
200200
.args(["verify", "."])
201201
.current_dir(&dir)
202202
.assert()
203-
.stderr(format!("error: empty directory `foo{SEPARATOR}bar`\n"))
203+
.stderr(path("error: empty directory `foo/bar`\n"))
204204
.failure();
205205
}
206206

@@ -302,31 +302,34 @@ error: 2 mismatched files
302302
.failure();
303303
}
304304

305-
// disable test on macos, since it does not allow non-unicode filenames
306-
#[cfg(not(target_os = "macos"))]
307305
#[test]
308306
fn non_unicode_path_error() {
309307
use std::path::PathBuf;
310308

309+
// macos does not allow non-unicode filenames
310+
if cfg!(target_os = "macos") {
311+
return;
312+
}
313+
311314
let dir = TempDir::new().unwrap();
312315

313-
let path: PathBuf;
316+
let invalid: PathBuf;
314317

315318
#[cfg(unix)]
316319
{
317320
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
318321

319-
path = OsStr::from_bytes(&[0x80]).into();
322+
invalid = OsStr::from_bytes(&[0x80]).into();
320323
};
321324

322325
#[cfg(windows)]
323326
{
324327
use std::{ffi::OsString, os::windows::ffi::OsStringExt};
325328

326-
path = OsString::from_wide(&[0xd800]).into();
329+
invalid = OsString::from_wide(&[0xd800]).into();
327330
};
328331

329-
dir.child(path).touch().unwrap();
332+
dir.child(invalid).touch().unwrap();
330333

331334
dir
332335
.child("filepack.json")
@@ -338,7 +341,7 @@ fn non_unicode_path_error() {
338341
.args(["verify", "."])
339342
.current_dir(&dir)
340343
.assert()
341-
.stderr(format!("error: path not valid unicode: `.{SEPARATOR}�`\n"))
344+
.stderr(path("error: path not valid unicode: `./�`\n"))
342345
.failure();
343346
}
344347

0 commit comments

Comments
 (0)