• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #[cfg(test)]
2 mod test {
3     use std::path::PathBuf;
4     use std::process::Command;
5     use std::str;
6 
7     use runfiles::Runfiles;
8 
9     /// fake_rustc runs the fake_rustc binary under process_wrapper with the specified
10     /// process wrapper arguments. No arguments are passed to fake_rustc itself.
11     ///
fake_rustc( process_wrapper_args: &[&'static str], fake_rustc_args: &[&'static str], should_succeed: bool, ) -> String12     fn fake_rustc(
13         process_wrapper_args: &[&'static str],
14         fake_rustc_args: &[&'static str],
15         should_succeed: bool,
16     ) -> String {
17         let r = Runfiles::create().unwrap();
18         let fake_rustc = r.rlocation(
19             [
20                 "rules_rust",
21                 "test",
22                 "process_wrapper",
23                 if cfg!(unix) {
24                     "fake_rustc"
25                 } else {
26                     "fake_rustc.exe"
27                 },
28             ]
29             .iter()
30             .collect::<PathBuf>(),
31         );
32 
33         let process_wrapper = r.rlocation(
34             [
35                 "rules_rust",
36                 "util",
37                 "process_wrapper",
38                 if cfg!(unix) {
39                     "process_wrapper"
40                 } else {
41                     "process_wrapper.exe"
42                 },
43             ]
44             .iter()
45             .collect::<PathBuf>(),
46         );
47 
48         let output = Command::new(process_wrapper)
49             .args(process_wrapper_args)
50             .arg("--")
51             .arg(fake_rustc)
52             .args(fake_rustc_args)
53             .output()
54             .unwrap();
55 
56         if should_succeed {
57             assert!(
58                 output.status.success(),
59                 "unable to run process_wrapper: {} {}",
60                 str::from_utf8(&output.stdout).unwrap(),
61                 str::from_utf8(&output.stderr).unwrap(),
62             );
63         }
64 
65         String::from_utf8(output.stderr).unwrap()
66     }
67 
68     #[test]
test_rustc_quit_on_rmeta_quits()69     fn test_rustc_quit_on_rmeta_quits() {
70         let out_content = fake_rustc(
71             &[
72                 "--rustc-quit-on-rmeta",
73                 "true",
74                 "--rustc-output-format",
75                 "rendered",
76             ],
77             &[],
78             true,
79         );
80         assert!(
81             !out_content.contains("should not be in output"),
82             "output should not contain 'should not be in output' but did",
83         );
84     }
85 
86     #[test]
test_rustc_quit_on_rmeta_output_json()87     fn test_rustc_quit_on_rmeta_output_json() {
88         let json_content = fake_rustc(
89             &[
90                 "--rustc-quit-on-rmeta",
91                 "true",
92                 "--rustc-output-format",
93                 "json",
94             ],
95             &[],
96             true,
97         );
98         assert_eq!(
99             json_content,
100             concat!(r#"{"rendered": "should be\nin output"}"#, "\n")
101         );
102     }
103 
104     #[test]
test_rustc_quit_on_rmeta_output_rendered()105     fn test_rustc_quit_on_rmeta_output_rendered() {
106         let rendered_content = fake_rustc(
107             &[
108                 "--rustc-quit-on-rmeta",
109                 "true",
110                 "--rustc-output-format",
111                 "rendered",
112             ],
113             &[],
114             true,
115         );
116         assert_eq!(rendered_content, "should be\nin output");
117     }
118 
119     #[test]
test_rustc_panic()120     fn test_rustc_panic() {
121         let rendered_content = fake_rustc(&["--rustc-output-format", "json"], &["error"], false);
122         assert_eq!(
123             rendered_content,
124             r#"{"rendered": "should be\nin output"}
125 ERROR!
126 this should all
127 appear in output.
128 Error: ProcessWrapperError("failed to process stderr: error parsing rustc output as json")
129 "#
130         );
131     }
132 }
133