Skip to content

Commit 543b4ca

Browse files
committed
Write test case for static files hashing
1 parent 25b70ef commit 543b4ca

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

src/renderer/html_handlebars/static_files.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl StaticFiles {
243243
let mut file = File::open(input_location)?;
244244
let mut data = Vec::new();
245245
file.read_to_end(&mut data)?;
246-
resource.replace_all(&data, |captures: &Captures<'_>| {
246+
let data = resource.replace_all(&data, |captures: &Captures<'_>| {
247247
let name = captures
248248
.get(1)
249249
.expect("capture 1 in resource regex")
@@ -273,3 +273,53 @@ impl StaticFiles {
273273
Ok(ResourceHelper { hash_map })
274274
}
275275
}
276+
277+
#[cfg(test)]
278+
mod tests {
279+
use super::*;
280+
use crate::theme::Theme;
281+
use crate::config::HtmlConfig;
282+
use crate::utils::fs::write_file;
283+
use std::io::Read;
284+
#[test]
285+
fn test_write_directive() {
286+
let theme = Theme {
287+
index: Vec::new(),
288+
head: Vec::new(),
289+
redirect: Vec::new(),
290+
header: Vec::new(),
291+
chrome_css: Vec::new(),
292+
general_css: Vec::new(),
293+
print_css: Vec::new(),
294+
variables_css: Vec::new(),
295+
favicon_png: Some(Vec::new()),
296+
favicon_svg: Some(Vec::new()),
297+
js: Vec::new(),
298+
highlight_css: Vec::new(),
299+
tomorrow_night_css: Vec::new(),
300+
ayu_highlight_css: Vec::new(),
301+
highlight_js: Vec::new(),
302+
clipboard_js: Vec::new(),
303+
};
304+
let reference_js = PathBuf::from("target/static-files-test-case-reference.js");
305+
let test_case = PathBuf::from("target/static-files-test-case");
306+
let mut html_config = HtmlConfig::default();
307+
html_config.additional_js.push(reference_js.clone());
308+
write_file(&Path::new("."), &reference_js, br#"{{ resource "book.js" }}"#).unwrap();
309+
let mut static_files = StaticFiles::new(&theme, &html_config, &Path::new(".")).unwrap();
310+
static_files.hash_files().unwrap();
311+
static_files.write_files(&test_case).unwrap();
312+
// custom JS winds up referencing book.js
313+
let mut reference_js_dest = File::open("target/static-files-test-case/target/static-files-test-case-reference-635c9cdc.js").unwrap();
314+
let mut reference_js_content = Vec::new();
315+
reference_js_dest.read_to_end(&mut reference_js_content).unwrap();
316+
assert_eq!(br#"../book-e3b0c442.js"#, &reference_js_content[..]);
317+
// book.js winds up empty
318+
let mut reference_js_dest = File::open("target/static-files-test-case/book-e3b0c442.js").unwrap();
319+
let mut reference_js_content = Vec::new();
320+
reference_js_dest.read_to_end(&mut reference_js_content).unwrap();
321+
assert_eq!(br#""#, &reference_js_content[..]);
322+
std::fs::remove_dir_all(&test_case).unwrap();
323+
std::fs::remove_file(&reference_js).unwrap();
324+
}
325+
}

0 commit comments

Comments
 (0)