1 // Copyright 2019 The Fuchsia Authors
2 //
3 // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4 // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6 // This file may not be copied, modified, or distributed except according to
7 // those terms.
8
9 use std::env;
10 use testutil::set_rustflags_w_warnings;
11
test(subdir: &str)12 fn test(subdir: &str) {
13 let version = testutil::ToolchainVersion::extract_from_pwd().unwrap();
14 // See the doc comment on this method for an explanation of what this does
15 // and why we store source files in different directories.
16 let source_files_dirname = version.get_ui_source_files_dirname_and_maybe_print_warning();
17
18 // Set `-Wwarnings` in the `RUSTFLAGS` environment variable to ensure that
19 // `.stderr` files reflect what the typical user would encounter.
20 set_rustflags_w_warnings();
21
22 let t = trybuild::TestCases::new();
23 t.compile_fail(format!("tests/{}/{}/*.rs", source_files_dirname, subdir));
24 }
25
26 #[test]
27 #[cfg_attr(miri, ignore)]
ui()28 fn ui() {
29 test("");
30
31 // This tests the behavior when `--cfg zerocopy_derive_union_into_bytes` is
32 // not present, so remove it. If this logic is wrong, that's fine - it will
33 // exhibit as a test failure that we can debug at that point.
34 let rustflags = env::var("RUSTFLAGS").unwrap();
35 let new_rustflags = rustflags.replace("--cfg zerocopy_derive_union_into_bytes", "");
36
37 // SAFETY: None of our code is concurrently accessinv env vars. It's
38 // possible that the test framework has spawned other threads that are
39 // concurrently accessing env vars, but we can't do anything about that.
40 #[allow(unused_unsafe)] // `set_var` is safe on our MSRV.
41 unsafe {
42 env::set_var("RUSTFLAGS", new_rustflags)
43 };
44
45 test("union_into_bytes_cfg");
46
47 // Reset RUSTFLAGS in case we later add other tests which rely on its value.
48 // This isn't strictly necessary, but it's easier to add this now when we're
49 // thinking about the semantics of these env vars than to debug later when
50 // we've forgotten about it.
51 //
52 // SAFETY: See previous safety comment.
53 #[allow(unused_unsafe)] // `set_var` is safe on our MSRV.
54 unsafe {
55 env::set_var("RUSTFLAGS", rustflags)
56 };
57 }
58