shadow_drive_cli/
state.rs1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4use shadow_drive_sdk::Pubkey;
5
6pub const STATE_FILE_NAME: &'static str = ".shdwclistate";
7
8#[derive(Serialize, Deserialize, Debug, PartialEq)]
9pub struct CliState {
10 pub collection: Option<Pubkey>,
12
13 pub keypair: Option<PathBuf>,
15
16 pub storage_account: Option<Pubkey>,
18}
19
20#[test]
21fn test_get_home_dir() {
22 assert!(
23 dirs::home_dir().is_some(),
24 "failed to retrieve home directory"
25 );
26}
27
28#[test]
29fn test_clistate_round_trip() {
30 use std::io::{Read, Write};
31 const LOCAL_TEST_STATE_FILE_NAME: &'static str = ".shadownftroundtriptest";
32 std::panic::set_hook(Box::new(|_| {
33 drop(std::fs::remove_file(LOCAL_TEST_STATE_FILE_NAME));
35 }));
36
37 let state = CliState {
39 collection: Some(Pubkey::new_unique()),
40 keypair: Some("right_here_bro.json".into()),
41 storage_account: Some(Pubkey::new_unique()),
42 };
43
44 let Some(home_dir) = dirs::home_dir() else {
46 panic!("failed to retrieve home directory");
47 };
48 println!("{}", home_dir.display());
49
50 let Ok(mut file) = std::fs::File::create(home_dir.join(LOCAL_TEST_STATE_FILE_NAME)) else {
52 panic!("failed to create a test cli state file")
53 };
54
55 let Ok(ser_state) = serde_json::to_string_pretty(&state) else {
57 panic!("failed to serialize cli state")
58 };
59 assert!(
60 file.write(ser_state.as_ref()).is_ok(),
61 "failed to write cli state"
62 );
63 drop(file);
64
65 let Ok(mut file) = std::fs::File::open(home_dir.join(LOCAL_TEST_STATE_FILE_NAME)) else {
67 panic!("failed to open the newly created test cli state file")
68 };
69 let mut state_json = String::new();
70 assert!(
71 file.read_to_string(&mut state_json).is_ok(),
72 "failed to read cli state"
73 );
74
75 let Ok(deser_state) = serde_json::from_str::<CliState>(&state_json) else {
77 panic!("failed to deserialize state");
78 };
79 assert_eq!(
80 &state, &deser_state,
81 "deserialized state does not match serialize state"
82 );
83
84 drop(std::fs::remove_file(LOCAL_TEST_STATE_FILE_NAME));
86}