XDG library path support for OCaml via Eio capabilities
linux macos ocaml xdg
at v1.0.0 4.6 kB view raw
1let test_path_validation () = 2 Printf.printf "Testing XDG path validation...\n"; 3 (* Test absolute path validation for environment variables *) 4 let test_relative_path_rejection env_var relative_path = 5 Printf.printf "Testing rejection of relative path in %s...\n" env_var; 6 Unix.putenv env_var relative_path; 7 try 8 Eio_main.run @@ fun env -> 9 let _ = Xdge.create env#fs "test_validation" in 10 Printf.printf "ERROR: Should have rejected relative path\n"; 11 false 12 with 13 | Xdge.Invalid_xdg_path msg -> 14 Printf.printf "SUCCESS: Correctly rejected relative path: %s\n" msg; 15 true 16 | exn -> 17 Printf.printf "ERROR: Wrong exception: %s\n" (Printexc.to_string exn); 18 false 19 in 20 let old_config_home = Sys.getenv_opt "XDG_CONFIG_HOME" in 21 let old_data_dirs = Sys.getenv_opt "XDG_DATA_DIRS" in 22 let success1 = 23 test_relative_path_rejection "XDG_CONFIG_HOME" "relative/path" 24 in 25 let success2 = 26 test_relative_path_rejection "XDG_DATA_DIRS" "rel1:rel2:/abs/path" 27 in 28 (* Restore original env vars *) 29 (match old_config_home with 30 | Some v -> Unix.putenv "XDG_CONFIG_HOME" v 31 | None -> ( try Unix.putenv "XDG_CONFIG_HOME" "" with _ -> ())); 32 (match old_data_dirs with 33 | Some v -> Unix.putenv "XDG_DATA_DIRS" v 34 | None -> ( try Unix.putenv "XDG_DATA_DIRS" "" with _ -> ())); 35 success1 && success2 36 37let test_file_search () = 38 Printf.printf "\nTesting XDG file search...\n"; 39 Eio_main.run @@ fun env -> 40 let xdg = Xdge.create env#fs "search_test" in 41 (* Create test files *) 42 let config_file = Eio.Path.(Xdge.config_dir xdg / "test.conf") in 43 let data_file = Eio.Path.(Xdge.data_dir xdg / "test.dat") in 44 Eio.Path.save ~create:(`Or_truncate 0o644) config_file "config content"; 45 Eio.Path.save ~create:(`Or_truncate 0o644) data_file "data content"; 46 (* Test finding existing files *) 47 (match Xdge.find_config_file xdg "test.conf" with 48 | Some path -> 49 let content = Eio.Path.load path in 50 Printf.printf "Found config file: %s\n" (String.trim content) 51 | None -> Printf.printf "ERROR: Config file not found\n"); 52 (match Xdge.find_data_file xdg "test.dat" with 53 | Some path -> 54 let content = Eio.Path.load path in 55 Printf.printf "Found data file: %s\n" (String.trim content) 56 | None -> Printf.printf "ERROR: Data file not found\n"); 57 (* Test non-existent file *) 58 match Xdge.find_config_file xdg "nonexistent.conf" with 59 | Some _ -> Printf.printf "ERROR: Should not have found nonexistent file\n" 60 | None -> Printf.printf "Correctly handled nonexistent file\n" 61 62let () = 63 (* Check if we should run validation tests *) 64 if Array.length Sys.argv > 1 && Sys.argv.(1) = "--validate" then ( 65 let validation_success = test_path_validation () in 66 test_file_search (); 67 if validation_success then 68 Printf.printf "\nAll path validation tests passed!\n" 69 else Printf.printf "\nSome validation tests failed!\n") 70 else 71 (* Run original simple functionality test *) 72 Eio_main.run @@ fun env -> 73 let xdg = Xdge.create env#fs "path_test" in 74 (* Test config subdirectory *) 75 let profiles_path = Eio.Path.(Xdge.config_dir xdg / "profiles") in 76 let profile_file = Eio.Path.(profiles_path / "default.json") in 77 (try 78 let content = Eio.Path.load profile_file in 79 Printf.printf "config file content: %s" (String.trim content) 80 with exn -> 81 Printf.printf "config file error: %s" (Printexc.to_string exn)); 82 (* Test data subdirectory *) 83 let db_path = Eio.Path.(Xdge.data_dir xdg / "databases") in 84 let db_file = Eio.Path.(db_path / "main.db") in 85 (try 86 let content = Eio.Path.load db_file in 87 Printf.printf "\ndata file content: %s" (String.trim content) 88 with exn -> 89 Printf.printf "\ndata file error: %s" (Printexc.to_string exn)); 90 (* Test cache subdirectory *) 91 let cache_path = Eio.Path.(Xdge.cache_dir xdg / "thumbnails") in 92 let cache_file = Eio.Path.(cache_path / "thumb1.png") in 93 (try 94 let content = Eio.Path.load cache_file in 95 Printf.printf "\ncache file content: %s" (String.trim content) 96 with exn -> 97 Printf.printf "\ncache file error: %s" (Printexc.to_string exn)); 98 (* Test state subdirectory *) 99 let logs_path = Eio.Path.(Xdge.state_dir xdg / "logs") in 100 let log_file = Eio.Path.(logs_path / "app.log") in 101 try 102 let content = Eio.Path.load log_file in 103 Printf.printf "\nstate file content: %s\n" (String.trim content) 104 with exn -> 105 Printf.printf "\nstate file error: %s\n" (Printexc.to_string exn)