xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/process_wrapper/rustc_quit_on_rmeta.rs (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
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 = runfiles::rlocation!(
19             r,
20             [
21                 "rules_rust",
22                 "test",
23                 "process_wrapper",
24                 if cfg!(unix) {
25                     "fake_rustc"
26                 } else {
27                     "fake_rustc.exe"
28                 },
29             ]
30             .iter()
31             .collect::<PathBuf>()
32         );
33 
34         let process_wrapper = runfiles::rlocation!(
35             r,
36             [
37                 "rules_rust",
38                 "util",
39                 "process_wrapper",
40                 if cfg!(unix) {
41                     "process_wrapper"
42                 } else {
43                     "process_wrapper.exe"
44                 },
45             ]
46             .iter()
47             .collect::<PathBuf>()
48         );
49 
50         let output = Command::new(process_wrapper)
51             .args(process_wrapper_args)
52             .arg("--")
53             .arg(fake_rustc)
54             .args(fake_rustc_args)
55             .output()
56             .unwrap();
57 
58         if should_succeed {
59             assert!(
60                 output.status.success(),
61                 "unable to run process_wrapper: {} {}",
62                 str::from_utf8(&output.stdout).unwrap(),
63                 str::from_utf8(&output.stderr).unwrap(),
64             );
65         }
66 
67         String::from_utf8(output.stderr).unwrap()
68     }
69 
70     #[test]
test_rustc_quit_on_rmeta_quits()71     fn test_rustc_quit_on_rmeta_quits() {
72         let out_content = fake_rustc(
73             &[
74                 "--rustc-quit-on-rmeta",
75                 "true",
76                 "--rustc-output-format",
77                 "rendered",
78             ],
79             &[],
80             true,
81         );
82         assert!(
83             !out_content.contains("should not be in output"),
84             "output should not contain 'should not be in output' but did",
85         );
86     }
87 
88     #[test]
test_rustc_quit_on_rmeta_output_json()89     fn test_rustc_quit_on_rmeta_output_json() {
90         let json_content = fake_rustc(
91             &[
92                 "--rustc-quit-on-rmeta",
93                 "true",
94                 "--rustc-output-format",
95                 "json",
96             ],
97             &[],
98             true,
99         );
100         assert_eq!(
101             json_content,
102             concat!(r#"{"rendered": "should be\nin output"}"#, "\n")
103         );
104     }
105 
106     #[test]
test_rustc_quit_on_rmeta_output_rendered()107     fn test_rustc_quit_on_rmeta_output_rendered() {
108         let rendered_content = fake_rustc(
109             &[
110                 "--rustc-quit-on-rmeta",
111                 "true",
112                 "--rustc-output-format",
113                 "rendered",
114             ],
115             &[],
116             true,
117         );
118         assert_eq!(rendered_content, "should be\nin output");
119     }
120 
121     #[test]
test_rustc_panic()122     fn test_rustc_panic() {
123         let rendered_content = fake_rustc(&["--rustc-output-format", "json"], &["error"], false);
124         assert_eq!(
125             rendered_content,
126             r#"{"rendered": "should be\nin output"}
127 ERROR!
128 this should all
129 appear in output.
130 Error: ProcessWrapperError("failed to process stderr: error parsing rustc output as json")
131 "#
132         );
133     }
134 }
135