Debian Codegraph @

debian/rust-vendor/pkg-config/tests/test.rs

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