-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for epub-version parameter in mdbook-epub plugin
- Loading branch information
Showing
4 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::fs; | ||
use std::io::{self, Write}; | ||
use std::process::Command; | ||
use tempfile::TempDir; | ||
|
||
fn create_book_with_epub_version(epub_version: &str) -> io::Result<String> { | ||
let temp_dir = TempDir::new()?; | ||
let book_path = temp_dir.path(); | ||
|
||
Command::new("mdbook") | ||
.arg("init") | ||
.arg(book_path) | ||
.arg("--title") | ||
.arg("Dummy Book") | ||
.output()?; | ||
|
||
let book_toml_path = book_path.join("book.toml"); | ||
let mut book_toml = fs::OpenOptions::new() | ||
.append(true) | ||
.open(&book_toml_path)?; | ||
writeln!(book_toml, "[output.epub]\nepub-version = {}", epub_version)?; | ||
|
||
let build_output = Command::new("mdbook") | ||
.arg("build") | ||
.arg(book_path) | ||
.output()?; | ||
|
||
if !build_output.status.success() { | ||
let stderr = String::from_utf8_lossy(&build_output.stderr); | ||
return Err(io::Error::new(io::ErrorKind::Other, stderr.to_string())); | ||
} | ||
|
||
Ok("Book built successfully".to_string()) | ||
} | ||
|
||
|
||
#[test] | ||
fn test_valid_epub_version_2() { | ||
create_book_with_epub_version("2").expect("Expected EPUB version 2 to build successfully"); | ||
} | ||
|
||
#[test] | ||
fn test_valid_epub_version_3() { | ||
create_book_with_epub_version("3").expect("Expected EPUB version 3 to build successfully"); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_epub_version_5() { | ||
let result = create_book_with_epub_version("5").expect_err("Expected EPUB version 5 to fail"); | ||
assert!( | ||
result.to_string().contains("Unsupported epub version specified in book.toml: 5"), | ||
"Unexpected error message: {}", | ||
result | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_epub_version_string() { | ||
let result = create_book_with_epub_version("\"3.0.1\"").expect_err("Expected EPUB version '3.0.1' to fail"); | ||
assert!( | ||
result.to_string().contains("expected u8 for key `epub-version`"), | ||
"Unexpected error message: {}", | ||
result | ||
); | ||
} |