]> git.proxmox.com Git - cargo.git/blob - vendor/pkg-config/tests/test.rs
New upstream version 0.63.1
[cargo.git] / vendor / pkg-config / tests / test.rs
1 extern crate pkg_config;
2 #[macro_use]
3 extern crate lazy_static;
4
5 use pkg_config::Error;
6 use std::env;
7 use std::path::PathBuf;
8 use std::sync::Mutex;
9
10 lazy_static! {
11 static ref LOCK: Mutex<()> = Mutex::new(());
12 }
13
14 fn reset() {
15 for (k, _) in env::vars() {
16 if k.contains("DYNAMIC")
17 || k.contains("STATIC")
18 || k.contains("PKG_CONFIG_ALLOW_CROSS")
19 || k.contains("PKG_CONFIG_SYSROOT_DIR")
20 || k.contains("FOO_NO_PKG_CONFIG")
21 {
22 env::remove_var(&k);
23 }
24 }
25 env::remove_var("TARGET");
26 env::remove_var("HOST");
27 env::set_var(
28 "PKG_CONFIG_PATH",
29 &env::current_dir().unwrap().join("tests"),
30 );
31 }
32
33 fn find(name: &str) -> Result<pkg_config::Library, Error> {
34 pkg_config::probe_library(name)
35 }
36
37 fn cross_disabled() {
38 let _g = LOCK.lock();
39 reset();
40 env::set_var("TARGET", "foo");
41 env::set_var("HOST", "bar");
42 match find("foo") {
43 Err(Error::CrossCompilation) => {}
44 x => panic!("Error::CrossCompilation expected, found `{:?}`", x),
45 }
46 }
47
48 fn cross_enabled() {
49 let _g = LOCK.lock();
50 reset();
51 env::set_var("TARGET", "foo");
52 env::set_var("HOST", "bar");
53 env::set_var("PKG_CONFIG_ALLOW_CROSS", "1");
54 find("foo").unwrap();
55 }
56
57 #[test]
58 fn cross_enabled_if_customized() {
59 let _g = LOCK.lock();
60 reset();
61 env::set_var("TARGET", "foo");
62 env::set_var("HOST", "bar");
63 env::set_var("PKG_CONFIG_SYSROOT_DIR", "/tmp/cross-test");
64 find("foo").unwrap();
65 }
66
67 fn cross_disabled_if_customized() {
68 let _g = LOCK.lock();
69 reset();
70 env::set_var("TARGET", "foo");
71 env::set_var("HOST", "bar");
72 env::set_var("PKG_CONFIG_ALLOW_CROSS", "0");
73 env::set_var("PKG_CONFIG_SYSROOT_DIR", "/tmp/cross-test");
74 // match find("foo") {
75 // Err(Error::CrossCompilation) => {}
76 // _ => panic!("expected CrossCompilation failure"),
77 // }
78 }
79
80 #[test]
81 fn package_disabled() {
82 let _g = LOCK.lock();
83 reset();
84 env::set_var("FOO_NO_PKG_CONFIG", "1");
85 match find("foo") {
86 Err(Error::EnvNoPkgConfig(name)) => assert_eq!(name, "FOO_NO_PKG_CONFIG"),
87 x => panic!("Error::EnvNoPkgConfig expected, found `{:?}`", x),
88 }
89 }
90
91 #[test]
92 fn output_ok() {
93 let _g = LOCK.lock();
94 reset();
95 let lib = find("foo").unwrap();
96 assert!(lib.libs.contains(&"gcc".to_string()));
97 assert!(lib.libs.contains(&"coregrind-amd64-linux".to_string()));
98 assert!(lib.link_paths.contains(&PathBuf::from("/usr/lib/valgrind")));
99 assert!(lib
100 .include_paths
101 .contains(&PathBuf::from("/usr/include/valgrind")));
102 assert!(lib.include_paths.contains(&PathBuf::from("/usr/foo")));
103 }
104
105 #[test]
106 fn escapes() {
107 let _g = LOCK.lock();
108 reset();
109 let lib = find("escape").unwrap();
110 assert!(lib
111 .include_paths
112 .contains(&PathBuf::from("include path with spaces")));
113 assert!(lib
114 .link_paths
115 .contains(&PathBuf::from("link path with spaces")));
116 assert_eq!(
117 lib.defines.get("A"),
118 Some(&Some("\"escaped string' literal\"".to_owned()))
119 );
120 assert_eq!(
121 lib.defines.get("B"),
122 Some(&Some("ESCAPED IDENTIFIER".to_owned()))
123 );
124 assert_eq!(lib.defines.get("FOX"), Some(&Some("🦊".to_owned())));
125 }
126
127 #[test]
128 fn framework() {
129 let _g = LOCK.lock();
130 reset();
131 let lib = find("framework").unwrap();
132 assert!(lib.frameworks.contains(&"foo".to_string()));
133 assert!(lib.frameworks.contains(&"bar".to_string()));
134 assert!(lib.frameworks.contains(&"baz".to_string()));
135 assert!(lib.frameworks.contains(&"foobar".to_string()));
136 assert!(lib.frameworks.contains(&"foobaz".to_string()));
137 assert!(lib.framework_paths.contains(&PathBuf::from("/usr/lib")));
138 }
139
140 #[test]
141 fn get_variable() {
142 let _g = LOCK.lock();
143 reset();
144 let prefix = pkg_config::get_variable("foo", "prefix").unwrap();
145 assert_eq!(prefix, "/usr");
146 }
147
148 #[test]
149 fn version() {
150 let _g = LOCK.lock();
151 reset();
152 assert_eq!(&find("foo").unwrap().version[..], "3.10.0.SVN");
153 }
154
155 #[test]
156 fn atleast_version_ok() {
157 let _g = LOCK.lock();
158 reset();
159 pkg_config::Config::new()
160 .atleast_version("3.10")
161 .probe("foo")
162 .unwrap();
163 }
164
165 #[test]
166 #[should_panic]
167 fn atleast_version_ng() {
168 let _g = LOCK.lock();
169 reset();
170 pkg_config::Config::new()
171 .atleast_version("3.11")
172 .probe("foo")
173 .unwrap();
174 }
175
176 #[test]
177 fn exactly_version_ok() {
178 let _g = LOCK.lock();
179 reset();
180 pkg_config::Config::new()
181 .exactly_version("3.10.0.SVN")
182 .probe("foo")
183 .unwrap();
184 }
185
186 #[test]
187 #[should_panic]
188 fn exactly_version_ng() {
189 let _g = LOCK.lock();
190 reset();
191 pkg_config::Config::new()
192 .exactly_version("3.10.0")
193 .probe("foo")
194 .unwrap();
195 }
196
197 #[test]
198 fn range_version_range_ok() {
199 let _g = LOCK.lock();
200 reset();
201 pkg_config::Config::new()
202 .range_version("4.2.0".."4.4.0")
203 .probe("escape")
204 .unwrap();
205 }
206
207 #[test]
208 #[should_panic]
209 fn range_version_range_ng() {
210 let _g = LOCK.lock();
211 reset();
212 pkg_config::Config::new()
213 .range_version("4.0.0".."4.2.0")
214 .probe("escape")
215 .unwrap();
216 }
217
218 #[test]
219 fn range_version_range_inclusive_ok() {
220 let _g = LOCK.lock();
221 reset();
222 pkg_config::Config::new()
223 .range_version("4.0.0"..="4.2.0")
224 .probe("escape")
225 .unwrap();
226 }
227
228 #[test]
229 #[should_panic]
230 fn range_version_range_inclusive_ng() {
231 let _g = LOCK.lock();
232 reset();
233 pkg_config::Config::new()
234 .range_version("3.8.0"..="4.0.0")
235 .probe("escape")
236 .unwrap();
237 }
238
239 #[test]
240 fn range_version_range_from_ok() {
241 let _g = LOCK.lock();
242 reset();
243 pkg_config::Config::new()
244 .range_version("4.0.0"..)
245 .probe("escape")
246 .unwrap();
247 }
248
249 #[test]
250 #[should_panic]
251 fn range_version_range_from_ng() {
252 let _g = LOCK.lock();
253 reset();
254 pkg_config::Config::new()
255 .range_version("4.4.0"..)
256 .probe("escape")
257 .unwrap();
258 }
259
260 #[test]
261 fn range_version_range_to_ok() {
262 let _g = LOCK.lock();
263 reset();
264 pkg_config::Config::new()
265 .range_version(.."4.4.0")
266 .probe("escape")
267 .unwrap();
268 }
269
270 #[test]
271 #[should_panic]
272 fn range_version_range_to_ng() {
273 let _g = LOCK.lock();
274 reset();
275 pkg_config::Config::new()
276 .range_version(.."4.2.0")
277 .probe("escape")
278 .unwrap();
279 }
280
281 #[test]
282 fn range_version_range_to_inclusive_ok() {
283 let _g = LOCK.lock();
284 reset();
285 pkg_config::Config::new()
286 .range_version(..="4.2.0")
287 .probe("escape")
288 .unwrap();
289 }
290
291 #[test]
292 #[should_panic]
293 fn range_version_range_to_inclusive_ng() {
294 let _g = LOCK.lock();
295 reset();
296 pkg_config::Config::new()
297 .range_version(..="4.0.0")
298 .probe("escape")
299 .unwrap();
300 }
301
302 #[test]
303 fn range_version_full() {
304 let _g = LOCK.lock();
305 reset();
306 pkg_config::Config::new()
307 .range_version(..)
308 .probe("escape")
309 .unwrap();
310 }
311
312 #[test]
313 fn rpath() {
314 let _g = LOCK.lock();
315 reset();
316 let lib = find("rpath").unwrap();
317 assert!(lib
318 .ld_args
319 .contains(&vec!["-rpath".to_string(), "/usr/local/lib".to_string(),]));
320 }