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