]> git.proxmox.com Git - rustc.git/blob - vendor/mdbook/tests/alternative_backends.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / vendor / mdbook / tests / alternative_backends.rs
1 //! Integration tests to make sure alternative backends work.
2
3 use mdbook::config::Config;
4 use mdbook::MDBook;
5 #[cfg(not(windows))]
6 use std::path::Path;
7 use tempfile::{Builder as TempFileBuilder, TempDir};
8
9 #[test]
10 fn passing_alternate_backend() {
11 let (md, _temp) = dummy_book_with_backend("passing", success_cmd(), false);
12
13 md.build().unwrap();
14 }
15
16 #[test]
17 fn failing_alternate_backend() {
18 let (md, _temp) = dummy_book_with_backend("failing", fail_cmd(), false);
19
20 md.build().unwrap_err();
21 }
22
23 #[test]
24 fn missing_backends_are_fatal() {
25 let (md, _temp) = dummy_book_with_backend("missing", "trduyvbhijnorgevfuhn", false);
26 assert!(md.build().is_err());
27 }
28
29 #[test]
30 fn missing_optional_backends_are_not_fatal() {
31 let (md, _temp) = dummy_book_with_backend("missing", "trduyvbhijnorgevfuhn", true);
32 assert!(md.build().is_ok());
33 }
34
35 #[test]
36 fn alternate_backend_with_arguments() {
37 let (md, _temp) = dummy_book_with_backend("arguments", "echo Hello World!", false);
38
39 md.build().unwrap();
40 }
41
42 /// Get a command which will pipe `stdin` to the provided file.
43 #[cfg(not(windows))]
44 fn tee_command<P: AsRef<Path>>(out_file: P) -> String {
45 let out_file = out_file.as_ref();
46
47 if cfg!(windows) {
48 format!("cmd.exe /c \"type > {}\"", out_file.display())
49 } else {
50 format!("tee {}", out_file.display())
51 }
52 }
53
54 #[test]
55 #[cfg(not(windows))]
56 fn backends_receive_render_context_via_stdin() {
57 use mdbook::renderer::RenderContext;
58 use std::fs::File;
59
60 let temp = TempFileBuilder::new().prefix("output").tempdir().unwrap();
61 let out_file = temp.path().join("out.txt");
62 let cmd = tee_command(&out_file);
63
64 let (md, _temp) = dummy_book_with_backend("cat-to-file", &cmd, false);
65
66 assert!(!out_file.exists());
67 md.build().unwrap();
68 assert!(out_file.exists());
69
70 let got = RenderContext::from_json(File::open(&out_file).unwrap());
71 assert!(got.is_ok());
72 }
73
74 fn dummy_book_with_backend(
75 name: &str,
76 command: &str,
77 backend_is_optional: bool,
78 ) -> (MDBook, TempDir) {
79 let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
80
81 let mut config = Config::default();
82 config
83 .set(format!("output.{}.command", name), command)
84 .unwrap();
85
86 if backend_is_optional {
87 config
88 .set(format!("output.{}.optional", name), true)
89 .unwrap();
90 }
91
92 let md = MDBook::init(temp.path())
93 .with_config(config)
94 .build()
95 .unwrap();
96
97 (md, temp)
98 }
99
100 fn fail_cmd() -> &'static str {
101 if cfg!(windows) {
102 r#"cmd.exe /c "exit 1""#
103 } else {
104 "false"
105 }
106 }
107
108 fn success_cmd() -> &'static str {
109 if cfg!(windows) {
110 r#"cmd.exe /c "exit 0""#
111 } else {
112 "true"
113 }
114 }