]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/header/tests.rs
157b42e2d17f5a52f3bfdb8870178c7a0f28c7ba
[rust.git] / src / tools / compiletest / src / header / tests.rs
1 use std::path::Path;
2
3 use crate::common::{Config, Debugger};
4 use crate::header::{make_test_description, parse_normalization_string, EarlyProps};
5
6 #[test]
7 fn test_parse_normalization_string() {
8     let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\".";
9     let first = parse_normalization_string(&mut s);
10     assert_eq!(first, Some("something (32 bits)".to_owned()));
11     assert_eq!(s, " -> \"something ($WORD bits)\".");
12
13     // Nothing to normalize (No quotes)
14     let mut s = "normalize-stderr-32bit: something (32 bits) -> something ($WORD bits).";
15     let first = parse_normalization_string(&mut s);
16     assert_eq!(first, None);
17     assert_eq!(s, r#"normalize-stderr-32bit: something (32 bits) -> something ($WORD bits)."#);
18
19     // Nothing to normalize (Only a single quote)
20     let mut s = "normalize-stderr-32bit: \"something (32 bits) -> something ($WORD bits).";
21     let first = parse_normalization_string(&mut s);
22     assert_eq!(first, None);
23     assert_eq!(s, "normalize-stderr-32bit: \"something (32 bits) -> something ($WORD bits).");
24
25     // Nothing to normalize (Three quotes)
26     let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits).";
27     let first = parse_normalization_string(&mut s);
28     assert_eq!(first, Some("something (32 bits)".to_owned()));
29     assert_eq!(s, " -> \"something ($WORD bits).");
30
31     // Nothing to normalize (No quotes, 16-bit)
32     let mut s = "normalize-stderr-16bit: something (16 bits) -> something ($WORD bits).";
33     let first = parse_normalization_string(&mut s);
34     assert_eq!(first, None);
35     assert_eq!(s, r#"normalize-stderr-16bit: something (16 bits) -> something ($WORD bits)."#);
36 }
37
38 fn config() -> Config {
39     let args = &[
40         "compiletest",
41         "--mode=ui",
42         "--suite=ui",
43         "--compile-lib-path=",
44         "--run-lib-path=",
45         "--rustc-path=",
46         "--lldb-python=",
47         "--docck-python=",
48         "--jsondocck-path=",
49         "--src-base=",
50         "--build-base=",
51         "--stage-id=stage2",
52         "--cc=c",
53         "--cxx=c++",
54         "--cflags=",
55         "--llvm-components=",
56         "--android-cross-path=",
57         "--target=x86_64-unknown-linux-gnu",
58         "--channel=nightly",
59     ];
60     let args = args.iter().map(ToString::to_string).collect();
61     crate::parse_config(args)
62 }
63
64 fn parse_rs(config: &Config, contents: &str) -> EarlyProps {
65     let bytes = contents.as_bytes();
66     EarlyProps::from_reader(config, Path::new("a.rs"), bytes)
67 }
68
69 fn check_ignore(config: &Config, contents: &str) -> bool {
70     let tn = test::DynTestName(String::new());
71     let p = Path::new("a.rs");
72     let d = make_test_description(&config, tn, p, std::io::Cursor::new(contents), None);
73     d.ignore
74 }
75
76 fn parse_makefile(config: &Config, contents: &str) -> EarlyProps {
77     let bytes = contents.as_bytes();
78     EarlyProps::from_reader(config, Path::new("Makefile"), bytes)
79 }
80
81 #[test]
82 fn should_fail() {
83     let config = config();
84     let tn = test::DynTestName(String::new());
85     let p = Path::new("a.rs");
86
87     let d = make_test_description(&config, tn.clone(), p, std::io::Cursor::new(""), None);
88     assert_eq!(d.should_panic, test::ShouldPanic::No);
89     let d = make_test_description(&config, tn, p, std::io::Cursor::new("// should-fail"), None);
90     assert_eq!(d.should_panic, test::ShouldPanic::Yes);
91 }
92
93 #[test]
94 fn revisions() {
95     let config = config();
96
97     assert_eq!(parse_rs(&config, "// revisions: a b c").revisions, vec!["a", "b", "c"],);
98     assert_eq!(
99         parse_makefile(&config, "# revisions: hello there").revisions,
100         vec!["hello", "there"],
101     );
102 }
103
104 #[test]
105 fn aux_build() {
106     let config = config();
107
108     assert_eq!(
109         parse_rs(
110             &config,
111             r"
112         // aux-build: a.rs
113         // aux-build: b.rs
114         "
115         )
116         .aux,
117         vec!["a.rs", "b.rs"],
118     );
119 }
120
121 #[test]
122 fn no_system_llvm() {
123     let mut config = config();
124
125     config.system_llvm = false;
126     assert!(!check_ignore(&config, "// no-system-llvm"));
127
128     config.system_llvm = true;
129     assert!(check_ignore(&config, "// no-system-llvm"));
130 }
131
132 #[test]
133 fn llvm_version() {
134     let mut config = config();
135
136     config.llvm_version = Some(80102);
137     assert!(check_ignore(&config, "// min-llvm-version: 9.0"));
138
139     config.llvm_version = Some(90001);
140     assert!(check_ignore(&config, "// min-llvm-version: 9.2"));
141
142     config.llvm_version = Some(90301);
143     assert!(!check_ignore(&config, "// min-llvm-version: 9.2"));
144
145     config.llvm_version = Some(100000);
146     assert!(!check_ignore(&config, "// min-llvm-version: 9.0"));
147 }
148
149 #[test]
150 fn ignore_target() {
151     let mut config = config();
152     config.target = "x86_64-unknown-linux-gnu".to_owned();
153
154     assert!(check_ignore(&config, "// ignore-x86_64-unknown-linux-gnu"));
155     assert!(check_ignore(&config, "// ignore-x86_64"));
156     assert!(check_ignore(&config, "// ignore-linux"));
157     assert!(check_ignore(&config, "// ignore-gnu"));
158     assert!(check_ignore(&config, "// ignore-64bit"));
159
160     assert!(!check_ignore(&config, "// ignore-i686"));
161     assert!(!check_ignore(&config, "// ignore-windows"));
162     assert!(!check_ignore(&config, "// ignore-msvc"));
163     assert!(!check_ignore(&config, "// ignore-32bit"));
164 }
165
166 #[test]
167 fn only_target() {
168     let mut config = config();
169     config.target = "x86_64-pc-windows-gnu".to_owned();
170
171     assert!(check_ignore(&config, "// only-x86"));
172     assert!(check_ignore(&config, "// only-linux"));
173     assert!(check_ignore(&config, "// only-msvc"));
174     assert!(check_ignore(&config, "// only-32bit"));
175
176     assert!(!check_ignore(&config, "// only-x86_64-pc-windows-gnu"));
177     assert!(!check_ignore(&config, "// only-x86_64"));
178     assert!(!check_ignore(&config, "// only-windows"));
179     assert!(!check_ignore(&config, "// only-gnu"));
180     assert!(!check_ignore(&config, "// only-64bit"));
181 }
182
183 #[test]
184 fn stage() {
185     let mut config = config();
186     config.stage_id = "stage1".to_owned();
187
188     assert!(check_ignore(&config, "// ignore-stage1"));
189     assert!(!check_ignore(&config, "// ignore-stage2"));
190 }
191
192 #[test]
193 fn cross_compile() {
194     let mut config = config();
195     config.host = "x86_64-apple-darwin".to_owned();
196     config.target = "wasm32-unknown-unknown".to_owned();
197     assert!(check_ignore(&config, "// ignore-cross-compile"));
198
199     config.target = config.host.clone();
200     assert!(!check_ignore(&config, "// ignore-cross-compile"));
201 }
202
203 #[test]
204 fn debugger() {
205     let mut config = config();
206     config.debugger = None;
207     assert!(!check_ignore(&config, "// ignore-cdb"));
208
209     config.debugger = Some(Debugger::Cdb);
210     assert!(check_ignore(&config, "// ignore-cdb"));
211
212     config.debugger = Some(Debugger::Gdb);
213     assert!(check_ignore(&config, "// ignore-gdb"));
214
215     config.debugger = Some(Debugger::Lldb);
216     assert!(check_ignore(&config, "// ignore-lldb"));
217 }
218
219 #[test]
220 fn sanitizers() {
221     let mut config = config();
222
223     // Target that supports all sanitizers:
224     config.target = "x86_64-unknown-linux-gnu".to_owned();
225     assert!(!check_ignore(&config, "// needs-sanitizer-address"));
226     assert!(!check_ignore(&config, "// needs-sanitizer-leak"));
227     assert!(!check_ignore(&config, "// needs-sanitizer-memory"));
228     assert!(!check_ignore(&config, "// needs-sanitizer-thread"));
229
230     // Target that doesn't support sanitizers:
231     config.target = "wasm32-unknown-emscripten".to_owned();
232     assert!(check_ignore(&config, "// needs-sanitizer-address"));
233     assert!(check_ignore(&config, "// needs-sanitizer-leak"));
234     assert!(check_ignore(&config, "// needs-sanitizer-memory"));
235     assert!(check_ignore(&config, "// needs-sanitizer-thread"));
236 }
237
238 #[test]
239 fn asm_support() {
240     let mut config = config();
241
242     config.target = "avr-unknown-gnu-atmega328".to_owned();
243     assert!(check_ignore(&config, "// needs-asm-support"));
244
245     config.target = "i686-unknown-netbsd".to_owned();
246     assert!(!check_ignore(&config, "// needs-asm-support"));
247 }
248
249 #[test]
250 fn channel() {
251     let mut config = config();
252     config.channel = "beta".into();
253
254     assert!(check_ignore(&config, "// ignore-beta"));
255     assert!(check_ignore(&config, "// only-nightly"));
256     assert!(check_ignore(&config, "// only-stable"));
257
258     assert!(!check_ignore(&config, "// only-beta"));
259     assert!(!check_ignore(&config, "// ignore-nightly"));
260     assert!(!check_ignore(&config, "// ignore-stable"));
261 }
262
263 #[test]
264 fn test_extract_version_range() {
265     use super::{extract_llvm_version, extract_version_range};
266
267     assert_eq!(extract_version_range("1.2.3 - 4.5.6", extract_llvm_version), Some((10203, 40506)));
268     assert_eq!(extract_version_range("0   - 4.5.6", extract_llvm_version), Some((0, 40506)));
269     assert_eq!(extract_version_range("1.2.3 -", extract_llvm_version), None);
270     assert_eq!(extract_version_range("1.2.3 - ", extract_llvm_version), None);
271     assert_eq!(extract_version_range("- 4.5.6", extract_llvm_version), None);
272     assert_eq!(extract_version_range("-", extract_llvm_version), None);
273     assert_eq!(extract_version_range(" - 4.5.6", extract_llvm_version), None);
274     assert_eq!(extract_version_range("   - 4.5.6", extract_llvm_version), None);
275     assert_eq!(extract_version_range("0  -", extract_llvm_version), None);
276 }
277
278 #[test]
279 #[should_panic(expected = "Duplicate revision: `rpass1` in line ` rpass1 rpass1`")]
280 fn test_duplicate_revisions() {
281     let config = config();
282     parse_rs(&config, "// revisions: rpass1 rpass1");
283 }