aboutsummaryrefslogtreecommitdiff
path: root/pkg-config/tests/test.rs
blob: fad0fcfdf0ab027b12f1eb6586f7dfae6fe68e73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
extern crate pkg_config;
#[macro_use]
extern crate lazy_static;

use pkg_config::Error;
use std::env;
use std::sync::Mutex;
use std::path::PathBuf;

lazy_static! {
    static ref LOCK: Mutex<()> = Mutex::new(());
}

fn reset() {
    for (k, _) in env::vars() {
        if k.contains("DYNAMIC") ||
           k.contains("STATIC") ||
           k.contains("PKG_CONFIG_ALLOW_CROSS") ||
           k.contains("FOO_NO_PKG_CONFIG") {
            env::remove_var(&k);
        }
    }
    env::remove_var("TARGET");
    env::remove_var("HOST");
    env::set_var("PKG_CONFIG_PATH", &env::current_dir().unwrap().join("tests"));
}

fn find(name: &str) -> Result<pkg_config::Library, Error> {
    pkg_config::probe_library(name)
}

#[test]
fn cross_disabled() {
    let _g = LOCK.lock();
    reset();
    env::set_var("TARGET", "foo");
    env::set_var("HOST", "bar");
    match find("foo") {
        Err(Error::CrossCompilation) => {},
        x => panic!("Error::CrossCompilation expected, found `{:?}`", x),
    }
}

#[test]
fn cross_enabled() {
    let _g = LOCK.lock();
    reset();
    env::set_var("TARGET", "foo");
    env::set_var("HOST", "bar");
    env::set_var("PKG_CONFIG_ALLOW_CROSS", "1");
    find("foo").unwrap();
}

#[test]
fn package_disabled() {
    let _g = LOCK.lock();
    reset();
    env::set_var("FOO_NO_PKG_CONFIG", "1");
    match find("foo") {
        Err(Error::EnvNoPkgConfig(name)) => {
            assert_eq!(name, "FOO_NO_PKG_CONFIG")
        }
        x => panic!("Error::EnvNoPkgConfig expected, found `{:?}`", x),
    }
}

#[test]
fn output_ok() {
    let _g = LOCK.lock();
    reset();
    let lib = find("foo").unwrap();
    assert!(lib.libs.contains(&"gcc".to_string()));
    assert!(lib.libs.contains(&"coregrind-amd64-linux".to_string()));
    assert!(lib.link_paths.contains(&PathBuf::from("/usr/lib/valgrind")));
}

#[test]
fn escapes() {
    let _g = LOCK.lock();
    reset();
    let lib = find("escape").unwrap();
    assert!(lib.include_paths.contains(&PathBuf::from("include path with spaces")));
    assert!(lib.link_paths.contains(&PathBuf::from("link path with spaces")));
    assert_eq!(lib.defines.get("A"),
               Some(&Some("\"escaped string' literal\"".to_owned())));
    assert_eq!(lib.defines.get("B"),
               Some(&Some("ESCAPED IDENTIFIER".to_owned())));
    assert_eq!(lib.defines.get("FOX"),
               Some(&Some("🦊".to_owned())));
}

#[test]
fn framework() {
    let _g = LOCK.lock();
    reset();
    let lib = find("framework").unwrap();
    assert!(lib.frameworks.contains(&"foo".to_string()));
    assert!(lib.frameworks.contains(&"bar".to_string()));
    assert!(lib.frameworks.contains(&"baz".to_string()));
    assert!(lib.frameworks.contains(&"foobar".to_string()));
    assert!(lib.frameworks.contains(&"foobaz".to_string()));
    assert!(lib.framework_paths.contains(&PathBuf::from("/usr/lib")));
}

#[test]
fn get_variable() {
    let _g = LOCK.lock();
    reset();
    let prefix = pkg_config::get_variable("foo", "prefix").unwrap();
    assert_eq!(prefix, "/usr");
}

#[test]
fn version() {
    let _g = LOCK.lock();
    reset();
    assert_eq!(&find("foo").unwrap().version[..], "3.10.0.SVN");
}