Skip to content

Commit 4c0484e

Browse files
committed
chore: Update tests, includ empty include list test
1 parent fd853af commit 4c0484e

File tree

1 file changed

+69
-31
lines changed

1 file changed

+69
-31
lines changed

src/tests.rs

+69-31
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,59 @@ use std::os::unix::fs::{symlink, PermissionsExt};
66

77
#[test]
88
fn copy_basic() {
9-
create_dir_all("source/level1/level2/level3").unwrap();
10-
File::create("source/test").unwrap();
11-
File::create("source/level1/other_file").unwrap();
9+
std::env::set_var("RUST_LOG", "DEBUG");
10+
let _ = env_logger::builder().try_init();
11+
12+
let src = "basic_src";
13+
let dst = "basic_dest";
14+
15+
create_dir_all(format!("{src}/level1/level2/level3")).unwrap();
16+
17+
File::create(format!("{src}/test")).unwrap();
18+
File::create(format!("{src}/level1/other_file")).unwrap();
1219

1320
#[cfg(unix)]
1421
{
15-
File::create("source/exec_file").unwrap();
16-
std::fs::set_permissions("source/exec_file", std::fs::Permissions::from_mode(0o755))
17-
.unwrap();
18-
symlink("exec_file", "source/symlink").unwrap();
19-
symlink("does_not_exist", "source/dangling_symlink").unwrap();
22+
File::create(format!("{src}/exec_file")).unwrap();
23+
std::fs::set_permissions(
24+
format!("{src}/exec_file"),
25+
std::fs::Permissions::from_mode(0o755),
26+
)
27+
.unwrap();
28+
symlink("exec_file", format!("{src}/symlink")).unwrap();
29+
symlink("does_not_exist", format!("{src}/dangling_symlink")).unwrap();
2030
}
2131

22-
CopyBuilder::new("source", "dest")
32+
CopyBuilder::new(src, dst)
2333
.overwrite(true)
2434
.overwrite_if_newer(true)
2535
.run()
2636
.unwrap();
2737

2838
#[cfg(unix)]
2939
{
30-
let f = File::open("dest/exec_file").unwrap();
40+
let f = File::open(format!("{dst}/exec_file")).unwrap();
3141
let metadata = f.metadata().unwrap();
3242
let permissions = metadata.permissions();
3343
println!("permissions: {:o}", permissions.mode());
3444
assert_eq!(permissions.mode(), 33261);
3545
assert_eq!(
3646
Path::new("exec_file"),
37-
read_link("dest/symlink").unwrap().as_path()
47+
read_link(format!("{dst}/symlink")).unwrap().as_path()
3848
);
3949
assert_eq!(
4050
Path::new("does_not_exist"),
41-
read_link("dest/dangling_symlink").unwrap().as_path()
51+
read_link(format!("{dst}/dangling_symlink"))
52+
.unwrap()
53+
.as_path()
4254
);
4355
}
4456

4557
// clean up
46-
std::fs::remove_dir_all("source").unwrap();
47-
std::fs::remove_dir_all("dest").unwrap();
58+
std::fs::remove_dir_all(src).unwrap();
59+
std::fs::remove_dir_all(dst).unwrap();
4860
}
4961

50-
5162
#[test]
5263
fn copy_subdir() {
5364
std::env::set_var("RUST_LOG", "debug");
@@ -59,14 +70,11 @@ fn copy_subdir() {
5970
File::create("source/b.jpg").unwrap();
6071
File::create("source/d.txt").unwrap();
6172

62-
CopyBuilder::new("source", "source/subdir")
63-
.run()
64-
.unwrap();
73+
CopyBuilder::new("source", "source/subdir").run().unwrap();
6574

66-
// std::fs::remove_dir_all("source").unwrap();
75+
std::fs::remove_dir_all("source").unwrap();
6776
}
6877

69-
7078
#[test]
7179
fn copy_exclude() {
7280
std::env::set_var("RUST_LOG", "DEBUG");
@@ -76,8 +84,8 @@ fn copy_exclude() {
7684
let dst = "ex_dest";
7785

7886
create_dir_all(src).unwrap();
79-
File::create(format!("{}/foo", src)).unwrap();
80-
File::create(format!("{}/bar", src)).unwrap();
87+
File::create(format!("{src}/foo")).unwrap();
88+
File::create(format!("{src}/bar")).unwrap();
8189

8290
CopyBuilder::new(src, dst)
8391
.overwrite(true)
@@ -102,9 +110,9 @@ fn copy_include() {
102110
let dst = "in_dest";
103111

104112
create_dir_all(src).unwrap();
105-
File::create(format!("{}/foo", src)).unwrap();
106-
File::create(format!("{}/bar", src)).unwrap();
107-
File::create(format!("{}/baz", src)).unwrap();
113+
File::create(format!("{src}/foo")).unwrap();
114+
File::create(format!("{src}/bar")).unwrap();
115+
File::create(format!("{src}/baz")).unwrap();
108116

109117
CopyBuilder::new(src, dst)
110118
.overwrite(true)
@@ -114,9 +122,37 @@ fn copy_include() {
114122
.run()
115123
.unwrap();
116124

117-
assert!(Path::new(&format!("{}/foo", dst)).is_file());
118-
assert!(!Path::new(&format!("{}/bar", dst)).exists());
119-
assert!(Path::new(&format!("{}/baz", dst)).exists());
125+
assert!(Path::new(&format!("{dst}/foo")).is_file());
126+
assert!(!Path::new(&format!("{dst}/bar")).exists());
127+
assert!(Path::new(&format!("{dst}/baz")).exists());
128+
129+
// clean up
130+
std::fs::remove_dir_all(src).unwrap();
131+
std::fs::remove_dir_all(dst).unwrap();
132+
}
133+
134+
#[test]
135+
fn copy_empty_include() {
136+
std::env::set_var("RUST_LOG", "DEBUG");
137+
let _ = env_logger::builder().try_init();
138+
139+
let src = "in_src_inc";
140+
let dst = "in_dest_inc";
141+
142+
create_dir_all(src).unwrap();
143+
File::create(format!("{src}/foo")).unwrap();
144+
File::create(format!("{src}/bar")).unwrap();
145+
File::create(format!("{src}/baz")).unwrap();
146+
147+
CopyBuilder::new(src, dst)
148+
.overwrite(true)
149+
.overwrite_if_newer(true)
150+
.run()
151+
.unwrap();
152+
153+
assert!(Path::new(&format!("{dst}/foo")).is_file());
154+
assert!(Path::new(&format!("{dst}/bar")).exists());
155+
assert!(Path::new(&format!("{dst}/baz")).exists());
120156

121157
// clean up
122158
std::fs::remove_dir_all(src).unwrap();
@@ -125,11 +161,13 @@ fn copy_include() {
125161

126162
#[test]
127163
fn copy_cargo() {
164+
std::env::set_var("RUST_LOG", "DEBUG");
165+
let _ = env_logger::builder().try_init();
128166
let url = "https://github.com/rust-lang/cargo/archive/master.zip";
129167
let sample_dir = "cargo";
130-
let output_dir = format!("{}_output", sample_dir);
131-
let archive = format!("{}.zip", sample_dir);
132-
println!("Expanding {}", archive);
168+
let output_dir = format!("{sample_dir}_output");
169+
let archive = format!("{sample_dir}.zip");
170+
info!("Expanding {archive}");
133171

134172
let mut resp = reqwest::blocking::get(url).unwrap();
135173
let mut out = File::create(&archive).expect("failed to create file");

0 commit comments

Comments
 (0)