• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::process::Command;
2 
3 #[test]
test_backends()4 fn test_backends() {
5     // Test whether `has_dependency` works. `cargo tree` no longer works in
6     // Rust 1.48 because `cargo tree` pulls in dependencies for all targets,
7     // and hermit-core isn't compatible with Rust 1.48.
8     if !has_dependency(".", &[], &[], &[], "io-lifetimes") {
9         return;
10     }
11 
12     // Pick an arbitrary platform where linux_raw is enabled by default and
13     // ensure that the use-default crate uses it.
14     #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
15     {
16         assert!(
17             has_dependency(
18                 "test-crates/use-default",
19                 &[],
20                 &[],
21                 &["RUSTFLAGS"],
22                 "linux-raw-sys"
23             ),
24             "use-default does not depend on linux-raw-sys"
25         );
26     }
27 
28     // Pick an arbitrary platform where linux_raw is enabled by default and
29     // ensure that the use-rustix-auxv crate uses it, and does not use libc.
30     #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
31     {
32         // TODO: Re-enable this test once io-lifetimes can depend on Rust 1.63
33         // and always use std, so it can drop its libc dependency.
34         /*
35         assert!(
36             !has_dependency(
37                 "test-crates/use-rustix-auxv",
38                 &[],
39                 &[],
40                 &["RUSTFLAGS"],
41                 "libc"
42             ),
43             "use-rustix-auxv depends on libc"
44         );
45         */
46 
47         assert!(
48             has_dependency(
49                 "test-crates/use-rustix-auxv",
50                 &[],
51                 &[],
52                 &["RUSTFLAGS"],
53                 "linux-raw-sys"
54             ),
55             "use-rustix-auxv does not depend on linux-raw-sys"
56         );
57     }
58 
59     #[cfg(windows)]
60     let libc_dep = "windows-sys";
61     #[cfg(any(unix, target_os = "wasi"))]
62     let libc_dep = "libc";
63 
64     // FIXME: Temporarily disable the subsequent tests on Windows until
65     // rust-errno updates to windows-sys 0.48.
66     #[cfg(windows)]
67     {
68         if true {
69             return;
70         }
71     }
72 
73     // Test the use-libc crate, which enables the "use-libc" cargo feature.
74     assert!(
75         has_dependency("test-crates/use-libc", &[], &[], &["RUSTFLAGS"], libc_dep),
76         "use-libc doesn't depend on {}",
77         libc_dep
78     );
79 
80     // Test the use-default crate with `--cfg=rustix_use_libc`.
81     assert!(
82         has_dependency(
83             "test-crates/use-default",
84             &[],
85             &[("RUSTFLAGS", "--cfg=rustix_use_libc")],
86             &[],
87             libc_dep
88         ),
89         "use-default with --cfg=rustix_use_libc does not depend on {}",
90         libc_dep
91     );
92 
93     // Test the use-default crate with `--features=rustix/use-libc`.
94     assert!(
95         has_dependency(
96             "test-crates/use-default",
97             &["--features=rustix/use-libc"],
98             &[],
99             &[],
100             libc_dep
101         ),
102         "use-default with --features=rustix/use-libc does not depend on {}",
103         libc_dep
104     );
105 }
106 
107 /// Test whether the crate at directory `dir` has a dependency on `dependency`,
108 /// setting the environment variables `envs` and unsetting the environment
109 /// variables `remove_envs` when running `cargo`.
has_dependency( dir: &str, args: &[&str], envs: &[(&str, &str)], remove_envs: &[&str], dependency: &str, ) -> bool110 fn has_dependency(
111     dir: &str,
112     args: &[&str],
113     envs: &[(&str, &str)],
114     remove_envs: &[&str],
115     dependency: &str,
116 ) -> bool {
117     let mut command = Command::new("cargo");
118 
119     command
120         .arg("tree")
121         .arg("--quiet")
122         .arg("--edges=normal")
123         .arg(&format!("--invert={}", dependency))
124         .current_dir(dir);
125 
126     command.args(args);
127     for (key, value) in envs {
128         command.env(key, value);
129     }
130     for key in remove_envs {
131         command.env_remove(key);
132     }
133 
134     let child = command.output().unwrap();
135 
136     // `cargo tree --invert=foo` can fail in two different ways: it exits with
137     // a non-zero status if the dependency is not present in the Cargo.toml
138     // configuration, and it exists with a zero status and prints nothing if
139     // the dependency is present but optional and not enabled. So we check for
140     // both here.
141     child.status.success() && !child.stdout.is_empty()
142 }
143