wasmer_integration_tests_cli/
assets.rs

1use std::path::PathBuf;
2use std::{env, path::Path};
3
4pub fn c_asset_path() -> &'static Path {
5    Path::new(concat!(
6        env!("CARGO_MANIFEST_DIR"),
7        "/../../../lib/c-api/examples/assets/"
8    ))
9}
10
11pub fn asset_path() -> &'static Path {
12    Path::new(concat!(
13        env!("CARGO_MANIFEST_DIR"),
14        "/../../../tests/examples/"
15    ))
16}
17
18pub fn wasmer_include_path() -> &'static Path {
19    Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../../../lib/c-api/"))
20}
21
22pub fn wasmer_target_path() -> &'static Path {
23    let path = if cfg!(feature = "debug") {
24        concat!(env!("CARGO_MANIFEST_DIR"), "/../../../target/debug/")
25    } else {
26        concat!(env!("CARGO_MANIFEST_DIR"), "/../../../target/release/")
27    };
28    Path::new(path)
29}
30
31pub fn wasmer_target_path_2() -> &'static Path {
32    let path = if cfg!(feature = "debug") {
33        concat!(
34            env!("CARGO_MANIFEST_DIR"),
35            "/../../../target/",
36            env!("CARGO_BUILD_TARGET"),
37            "/debug/"
38        )
39    } else {
40        concat!(
41            env!("CARGO_MANIFEST_DIR"),
42            "/../../../target/",
43            env!("CARGO_BUILD_TARGET"),
44            "/release/"
45        )
46    };
47    Path::new(path)
48}
49
50/* env var TARGET is set by tests/integration/cli/build.rs on compile-time */
51
52pub const LIBWASMER_FILENAME: &str = {
53    if cfg!(windows) {
54        "wasmer.lib"
55    } else {
56        "libwasmer.a"
57    }
58};
59
60/// Get the path to the `libwasmer.a` static library.
61pub fn get_libwasmer_path() -> PathBuf {
62    let mut ret = env::var("WASMER_TEST_LIBWASMER_PATH")
63        .map(PathBuf::from)
64        .unwrap_or_else(|_| wasmer_target_path().join(LIBWASMER_FILENAME));
65
66    if !ret.exists() {
67        ret = wasmer_target_path_2().join(LIBWASMER_FILENAME);
68    }
69    if !ret.exists() {
70        panic!("Could not find libwasmer path! {ret:?}");
71    }
72    ret
73}
74
75/// Get the path to the `wasmer` executable to be used in this test.
76pub fn get_wasmer_path() -> PathBuf {
77    let mut ret = env::var("WASMER_TEST_WASMER_PATH")
78        .map(PathBuf::from)
79        .unwrap_or_else(|_| wasmer_target_path().join("wasmer"));
80
81    if !ret.exists() {
82        ret = wasmer_target_path_2().join("wasmer");
83    }
84    if !ret.exists() {
85        ret = match get_repo_root_path() {
86            Some(s) => {
87                let release_dir = s.join("target").join("release");
88
89                if cfg!(windows) {
90                    release_dir.join("wasmer.exe")
91                } else {
92                    release_dir.join("wasmer")
93                }
94            }
95            None => {
96                panic!("Could not find wasmer executable path! {ret:?}");
97            }
98        };
99    }
100
101    if !ret.exists() {
102        ret = match get_repo_root_path() {
103            Some(s) => {
104                let executable = if cfg!(windows) {
105                    "wasmer.exe"
106                } else {
107                    "wasmer"
108                };
109                s.join("target")
110                    .join(target_lexicon::HOST.to_string())
111                    .join("release")
112                    .join(executable)
113            }
114            None => {
115                panic!("Could not find wasmer executable path! {ret:?}");
116            }
117        };
118    }
119
120    if !ret.exists() {
121        if let Some(root) = get_repo_root_path() {
122            use std::process::Stdio;
123            let _ = std::process::Command::new("ls")
124                .arg(root.join("target"))
125                .stdout(Stdio::inherit())
126                .stderr(Stdio::inherit())
127                .stdin(Stdio::null())
128                .output();
129        }
130        panic!(
131            "cannot find wasmer / wasmer.exe for integration test at '{}'!",
132            ret.display()
133        );
134    }
135    ret
136}
137
138pub fn get_repo_root_path() -> Option<PathBuf> {
139    let mut current_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
140    let mut counter = 0;
141    let mut result = None;
142    'outer: while counter < 50 {
143        counter += 1;
144        if current_dir.join("CHANGELOG.md").exists() && current_dir.join("LICENSE").exists() {
145            result = Some(current_dir.to_path_buf());
146            break 'outer;
147        } else {
148            current_dir = current_dir.parent()?;
149        }
150    }
151    result
152}
153
154pub fn get_wasmer_dir() -> Result<PathBuf, anyhow::Error> {
155    if let Ok(s) = std::env::var("WASMER_DIR") {
156        Ok(Path::new(&s).to_path_buf())
157    } else if let Some(root_dir) = get_repo_root_path().and_then(|root| {
158        if root.join("package").exists() {
159            Some(root.join("package"))
160        } else {
161            None
162        }
163    }) {
164        Ok(root_dir)
165    } else {
166        let home_dir = dirs::home_dir()
167            .ok_or(anyhow::anyhow!("no home dir"))?
168            .join(".wasmer");
169        if home_dir.exists() {
170            Ok(home_dir)
171        } else {
172            Err(anyhow::anyhow!("no .wasmer home dir"))
173        }
174    }
175}