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