lib.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, plugin::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. )
  27. .expect("failed to define permissions");
  28. let manifest = Manifest::new(permission_files, None);
  29. manifests.insert(plugin.to_string(), manifest);
  30. }
  31. manifests
  32. }
  33. #[test]
  34. fn resolve_acl() {
  35. let mut settings = insta::Settings::clone_current();
  36. settings.set_snapshot_path("../fixtures/snapshots");
  37. let _guard = settings.bind_to_scope();
  38. let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
  39. let fixtures_path = manifest_dir.join("fixtures").join("capabilities");
  40. for fixture_path in read_dir(fixtures_path).expect("failed to read fixtures") {
  41. let fixture_entry = fixture_path.expect("failed to read fixture entry");
  42. let fixture_plugins_str = read_to_string(fixture_entry.path().join("required-plugins.json"))
  43. .expect("failed to read fixture required-plugins.json file");
  44. let fixture_plugins: Vec<String> = serde_json::from_str(&fixture_plugins_str)
  45. .expect("required-plugins.json is not a valid JSON");
  46. let manifests = load_plugins(&fixture_plugins);
  47. let capabilities = parse_capabilities(&format!("{}/cap*", fixture_entry.path().display()))
  48. .expect("failed to parse capabilities");
  49. let resolved = Resolved::resolve(manifests, capabilities, Target::current())
  50. .expect("failed to resolve ACL");
  51. insta::assert_debug_snapshot!(
  52. fixture_entry
  53. .path()
  54. .file_name()
  55. .unwrap()
  56. .to_string_lossy()
  57. .to_string(),
  58. resolved
  59. );
  60. }
  61. }
  62. }