Skip to content

Commit 553101f

Browse files
committed
test(build): add tests for .mdbookignore feature
1 parent 1123126 commit 553101f

File tree

12 files changed

+85
-0
lines changed

12 files changed

+85
-0
lines changed

crates/mdbook-core/src/utils/fs.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<()> {
238238
#[cfg(test)]
239239
mod tests {
240240
use super::*;
241+
use ignore::gitignore::GitignoreBuilder;
241242
use std::io::Result;
242243
use std::path::Path;
243244

@@ -298,4 +299,56 @@ mod tests {
298299
panic!("output/symlink.png should exist")
299300
}
300301
}
302+
303+
#[test]
304+
fn copy_files_except_ignored_test() {
305+
let tmp = match tempfile::TempDir::new() {
306+
Ok(t) => t,
307+
Err(e) => panic!("Could not create a temp dir: {e}"),
308+
};
309+
310+
// Create files and directories
311+
write(tmp.path().join("file.txt"), "").unwrap();
312+
write(tmp.path().join("file.json"), "").unwrap();
313+
write(tmp.path().join("ignored_dir/nested.txt"), "").unwrap();
314+
write(tmp.path().join("included_dir/file.json"), "").unwrap();
315+
write(tmp.path().join("included_dir/file.txt"), "").unwrap();
316+
317+
// Create a gitignore that ignores *.txt and ignored_dir/
318+
let mut builder = GitignoreBuilder::new(tmp.path());
319+
builder.add_line(None, "*.txt").unwrap();
320+
builder.add_line(None, "ignored_dir/").unwrap();
321+
let ignore = builder.build().unwrap();
322+
323+
// Create output dir
324+
create_dir_all(tmp.path().join("output")).unwrap();
325+
326+
copy_files_except_ignored(
327+
tmp.path(),
328+
&tmp.path().join("output"),
329+
true,
330+
None,
331+
Some(&ignore),
332+
)
333+
.unwrap();
334+
335+
// Check that .txt files are ignored
336+
if tmp.path().join("output/file.txt").exists() {
337+
panic!("output/file.txt should not exist")
338+
}
339+
if tmp.path().join("output/included_dir/file.txt").exists() {
340+
panic!("output/included_dir/file.txt should not exist")
341+
}
342+
// Check that ignored_dir is not copied
343+
if tmp.path().join("output/ignored_dir").exists() {
344+
panic!("output/ignored_dir should not exist")
345+
}
346+
// Check that non-ignored files are copied
347+
if !tmp.path().join("output/file.json").exists() {
348+
panic!("output/file.json should exist")
349+
}
350+
if !tmp.path().join("output/included_dir/file.json").exists() {
351+
panic!("output/included_dir/file.json should exist")
352+
}
353+
}
301354
}

tests/testsuite/build.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,18 @@ fn dest_dir_relative_path() {
8585
});
8686
assert!(current_dir.join("foo/index.html").exists());
8787
}
88+
89+
// Test that .mdbookignore files are respected (single files, glob patterns, and directories).
90+
#[test]
91+
fn mdbookignore() {
92+
let mut test = BookTest::from_dir("build/mdbookignore");
93+
test.build();
94+
// Single file listed by name should not be copied.
95+
assert!(!test.dir.join("book/ignored_file").exists());
96+
// Files matching *.txt glob pattern should not be copied.
97+
assert!(!test.dir.join("book/ignored.txt").exists());
98+
// Directories listed in .mdbookignore should not be copied.
99+
assert!(!test.dir.join("book/ignored_dir").exists());
100+
// Non-ignored files should be copied.
101+
assert!(test.dir.join("book/included.json").exists());
102+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[book]
2+
title = "mdbookignore test"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ignored_file
2+
*.txt
3+
ignored_dir/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Summary
2+
3+
- [Chapter 1](./chapter_1.md)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Chapter 1
2+
3+
This is chapter 1.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This txt file should not be copied.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file in ignored dir should not be copied.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file should not be copied.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file should be copied.

0 commit comments

Comments
 (0)