1use std::env;
2use std::fs::File;
3use std::io::prelude::*;
4use std::path::PathBuf;
5
6pub fn test_project_path() -> PathBuf {
7 if cfg!(any(
8 target_os = "ios",
9 target_os = "watchos",
10 target_os = "tvos",
11 target_os = "android"
12 )) || env::var("DINGHY").is_ok()
13 {
14 let current_exe = env::current_exe().expect("Current exe path not accessible");
15
16 current_exe
17 .parent()
18 .expect(&format!(
19 "Current exe path is invalid {}",
20 current_exe.display()
21 ))
22 .into()
23 } else {
24 PathBuf::from(".")
25 }
26}
27
28pub fn test_file_path(test_data_id: &str) -> PathBuf {
29 try_test_file_path(test_data_id).expect(&format!("Couldn't find test data {}", test_data_id))
30}
31
32pub fn try_test_file_path(test_data_id: &str) -> Option<PathBuf> {
33 let current_exe = env::current_exe().expect("Current exe path not accessible");
34
35 if cfg!(any(
36 target_os = "ios",
37 target_os = "watchos",
38 target_os = "tvos",
39 target_os = "android"
40 )) || env::var("DINGHY").is_ok()
41 {
42 current_exe
43 .parent()
44 .map(|it| it.join("test_data"))
45 .map(|it| it.join(test_data_id))
46 } else {
47 let test_data_path = current_exe
48 .parent()
49 .and_then(|it| it.parent())
50 .map(|it| it.join("dinghy"))
51 .map(|it| it.join(current_exe.file_name().unwrap()))
52 .map(|it| it.join("test_data"));
53 let test_data_path = match test_data_path {
54 None => return None,
55 Some(test_data_cfg_path) => test_data_cfg_path,
56 };
57
58 let test_data_cfg_path = test_data_path.join("test_data.cfg");
59
60 let mut contents = String::new();
61 let test_data_cfg =
62 File::open(&test_data_cfg_path).and_then(|mut f| f.read_to_string(&mut contents));
63 if let Err(_) = test_data_cfg {
64 return None;
65 }
66
67 contents
68 .lines()
69 .map(|line| line.split(":"))
70 .map(|mut line| (line.next(), line.next()))
71 .find(|&(id, _)| id.map(|it| it == test_data_id).unwrap_or(false))
72 .and_then(|(_, path)| path)
73 .map(PathBuf::from)
74 }
75}