Skip to main content

stylex_transform/shared/structures/tests/
get_canonical_file_path_test.rs

1#[cfg(test)]
2mod get_canonical_file_path {
3  use std::{env, path::PathBuf};
4
5  use path_clean::PathClean;
6  use rustc_hash::FxHashMap;
7
8  use crate::shared::structures::state_manager::StateManager;
9  use stylex_structures::stylex_options::{CheckModuleResolution, ModuleResolution};
10
11  fn get_fixture_path(test_path: &str) -> PathBuf {
12    env::current_dir()
13      .unwrap()
14      .join("src/shared/structures/tests/fixtures")
15      .join(test_path)
16      .clean()
17  }
18
19  #[test]
20  fn get_canonical_file_path_with_name() {
21    let fixture_path = get_fixture_path("package_json_with_name");
22
23    let stage_manager = StateManager::default();
24
25    let canonical_path = stage_manager
26      .get_canonical_file_path(fixture_path.to_str().unwrap(), &mut FxHashMap::default());
27
28    assert_eq!(canonical_path, "package_json_with_name:.");
29  }
30
31  #[test]
32  fn get_canonical_file_path_without_name() {
33    let fixture_path = get_fixture_path("package_json_without_name");
34
35    let stage_manager = StateManager::default();
36
37    let canonical_path = stage_manager
38      .get_canonical_file_path(fixture_path.to_str().unwrap(), &mut FxHashMap::default());
39
40    assert_eq!(canonical_path, "_unknown_name_:.");
41  }
42
43  #[test]
44  fn get_canonical_file_unknown_path() {
45    let fixture_path = PathBuf::from("/unknown/path");
46
47    let stage_manager = StateManager::default();
48
49    let canonical_path = stage_manager
50      .get_canonical_file_path(fixture_path.to_str().unwrap(), &mut FxHashMap::default());
51
52    assert_eq!(canonical_path, "_unknown_path_:path");
53  }
54
55  #[test]
56  fn get_canonical_file_from_unknown_root_dir() {
57    let fixture_path = PathBuf::from("/unknown/path");
58
59    let mut stage_manager = StateManager::default();
60
61    stage_manager.options.unstable_module_resolution =
62      CheckModuleResolution::CommonJS(ModuleResolution {
63        r#type: "commonjs".to_string(),
64        root_dir: Some(fixture_path.parent().unwrap().to_string_lossy().into()),
65        theme_file_extension: None,
66      });
67
68    let canonical_path = stage_manager
69      .get_canonical_file_path(fixture_path.to_str().unwrap(), &mut FxHashMap::default());
70
71    assert_eq!(canonical_path, "path");
72  }
73
74  #[test]
75  fn get_canonical_file_from_root_dir() {
76    let fixture_path = get_fixture_path("package_json_with_name");
77
78    let root_dir = fixture_path.parent().unwrap();
79
80    let mut stage_manager = StateManager::default();
81
82    stage_manager.options.unstable_module_resolution =
83      CheckModuleResolution::CommonJS(ModuleResolution {
84        r#type: "commonjs".to_string(),
85        root_dir: Some(root_dir.to_string_lossy().into()),
86        theme_file_extension: None,
87      });
88
89    let canonical_path = stage_manager.get_canonical_file_path(
90      root_dir.join("src/components").to_str().unwrap(),
91      &mut FxHashMap::default(),
92    );
93
94    assert_eq!(
95      canonical_path,
96      "@stylexswc/transform:src/shared/structures/tests/fixtures/src/components"
97    );
98  }
99}