lib.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #[cfg(test)]
  5. mod tests {
  6. use std::{
  7. collections::BTreeMap,
  8. env::temp_dir,
  9. fs::{read_dir, read_to_string},
  10. path::Path,
  11. };
  12. use tauri_utils::{
  13. acl::{build::parse_capabilities, manifest::Manifest, resolved::Resolved},
  14. platform::Target,
  15. };
  16. fn load_plugins(plugins: &[String]) -> BTreeMap<String, Manifest> {
  17. let mut manifests = BTreeMap::new();
  18. let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
  19. let out_dir = temp_dir();
  20. for plugin in plugins {
  21. let plugin_path = manifest_dir.join("fixtures").join("plugins").join(plugin);
  22. let permission_files = tauri_utils::acl::build::define_permissions(
  23. &format!("{}/*.toml", plugin_path.display()),
  24. plugin,
  25. &out_dir,
  26. |_| true,
  27. )
  28. .expect("failed to define permissions");
  29. let manifest = Manifest::new(permission_files, None);
  30. manifests.insert(plugin.to_string(), manifest);
  31. }
  32. manifests
  33. }
  34. #[test]
  35. fn resolve_acl() {
  36. let mut settings = insta::Settings::clone_current();
  37. settings.set_snapshot_path("../fixtures/snapshots");
  38. let _guard = settings.bind_to_scope();
  39. let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
  40. let fixtures_path = manifest_dir.join("fixtures").join("capabilities");
  41. for fixture_path in read_dir(fixtures_path).expect("failed to read fixtures") {
  42. let fixture_entry = fixture_path.expect("failed to read fixture entry");
  43. let fixture_plugins_str = read_to_string(fixture_entry.path().join("required-plugins.json"))
  44. .expect("failed to read fixture required-plugins.json file");
  45. let fixture_plugins: Vec<String> = serde_json::from_str(&fixture_plugins_str)
  46. .expect("required-plugins.json is not a valid JSON");
  47. let manifests = load_plugins(&fixture_plugins);
  48. let capabilities = parse_capabilities(&format!("{}/cap*", fixture_entry.path().display()))
  49. .expect("failed to parse capabilities");
  50. let resolved = Resolved::resolve(&manifests, capabilities, Target::current())
  51. .expect("failed to resolve ACL");
  52. insta::assert_debug_snapshot!(
  53. fixture_entry
  54. .path()
  55. .file_name()
  56. .unwrap()
  57. .to_string_lossy()
  58. .to_string(),
  59. resolved
  60. );
  61. }
  62. }
  63. }