global.rs 4.3 KB

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