Debian Codegraph @

debian/missing-sources/pkg-config/tests/test.rs

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