xref: /DADK/crates/test_base/src/global.rs (revision e2cc487b9b00279c1bb550f2e614bac16a38120f)
1 use std::path::PathBuf;
2 
3 use test_context::TestContext;
4 
5 #[derive(Debug, Clone)]
6 pub struct BaseGlobalTestContext {
7     /// 项目的根目录
8     project_base_path: PathBuf,
9 }
10 
11 impl BaseGlobalTestContext {
12     const CONFIG_V1_DIR: &'static str = "tests/data/dadk_config_v1";
13     const FAKE_DRAGONOS_SYSROOT: &'static str = "tests/data/fake_dragonos_sysroot";
14     const FAKE_DADK_CACHE_ROOT: &'static str = "tests/data/fake_dadk_cache_root";
15 
16     /// 获取项目的根目录
17     pub fn project_base_path(&self) -> &PathBuf {
18         &self.project_base_path
19     }
20 
21     /// 获取项目目录下的文件的的绝对路径
22     pub fn abs_path(&self, relative_path: &str) -> PathBuf {
23         self.project_base_path.join(relative_path)
24     }
25 
26     /// 获取`xxx.dadk`配置文件的目录
27     pub fn config_v1_dir(&self) -> PathBuf {
28         self.abs_path(Self::CONFIG_V1_DIR)
29     }
30 
31     fn ensure_fake_dragonos_dir_exist(&self) {
32         let fake_dragonos_dir = self.fake_dragonos_sysroot();
33         if !fake_dragonos_dir.exists() {
34             std::fs::create_dir_all(&fake_dragonos_dir).ok();
35         }
36     }
37 
38     fn ensure_fake_dadk_cache_root_exist(&self) {
39         std::env::set_var(
40             "DADK_CACHE_ROOT",
41             self.fake_dadk_cache_root().to_str().unwrap(),
42         );
43         let fake_dadk_cache_root = self.fake_dadk_cache_root();
44         if !fake_dadk_cache_root.exists() {
45             std::fs::create_dir_all(&fake_dadk_cache_root).ok();
46         }
47     }
48 
49     pub fn fake_dadk_cache_root(&self) -> PathBuf {
50         self.abs_path(Self::FAKE_DADK_CACHE_ROOT)
51     }
52 
53     /// 获取假的DragonOS sysroot目录
54     pub fn fake_dragonos_sysroot(&self) -> PathBuf {
55         self.abs_path(Self::FAKE_DRAGONOS_SYSROOT)
56     }
57 }
58 
59 impl TestContext for BaseGlobalTestContext {
60     fn setup() -> Self {
61         env_logger::try_init_from_env(env_logger::Env::default().default_filter_or("info")).ok();
62 
63         // 获取DADK项目的根目录
64         let mut project_base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
65         project_base_path.pop();
66         project_base_path.pop();
67         // 设置workdir
68         std::env::set_current_dir(&project_base_path).expect("Failed to setup project_base_path");
69 
70         let r = BaseGlobalTestContext { project_base_path };
71         r.ensure_fake_dragonos_dir_exist();
72         r.ensure_fake_dadk_cache_root_exist();
73         r
74     }
75 }
76 #[cfg(test)]
77 mod tests {
78     use super::*;
79     use std::env;
80 
81     #[test]
82     fn test_project_base_path() {
83         let context = BaseGlobalTestContext::setup();
84         let binding = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
85         let expected_path = binding.parent().unwrap().parent().unwrap();
86         assert_eq!(context.project_base_path(), &expected_path);
87     }
88 
89     #[test]
90     fn test_abs_path() {
91         let context = BaseGlobalTestContext::setup();
92         let relative_path = "some/relative/path";
93         let expected_path = context.project_base_path().join(relative_path);
94         assert_eq!(context.abs_path(relative_path), expected_path);
95     }
96 
97     #[test]
98     fn test_config_v1_dir() {
99         let context = BaseGlobalTestContext::setup();
100         let expected_path = context.abs_path(BaseGlobalTestContext::CONFIG_V1_DIR);
101         assert_eq!(context.config_v1_dir(), expected_path);
102     }
103 
104     #[test]
105     fn test_fake_dadk_cache_root() {
106         let context = BaseGlobalTestContext::setup();
107         let expected_path = context.abs_path(BaseGlobalTestContext::FAKE_DADK_CACHE_ROOT);
108         assert_eq!(context.fake_dadk_cache_root(), expected_path);
109         assert!(expected_path.exists());
110     }
111 
112     #[test]
113     fn test_fake_dragonos_sysroot() {
114         let context = BaseGlobalTestContext::setup();
115         let expected_path = context.abs_path(BaseGlobalTestContext::FAKE_DRAGONOS_SYSROOT);
116         assert_eq!(context.fake_dragonos_sysroot(), expected_path);
117         assert!(expected_path.exists());
118     }
119 
120     #[test]
121     fn test_setup() {
122         let context = BaseGlobalTestContext::setup();
123         assert!(context.project_base_path().is_dir());
124         assert_eq!(
125             env::var("DADK_CACHE_ROOT").unwrap(),
126             context.fake_dadk_cache_root().to_str().unwrap()
127         );
128     }
129 }
130