batch_mode_json/
write_json_to_file.rs1crate::ix!();
3
4pub async fn write_to_file(
13 target_path: impl AsRef<Path>,
14 serialized_json: &str
15) -> Result<(), io::Error>
16{
17 info!("writing some json content to file: {:?}", target_path.as_ref());
18
19 let mut target_file = File::create(&target_path).await?;
21
22 target_file.write_all(serialized_json.as_bytes()).await?;
24
25 target_file.flush().await?;
27
28 Ok(())
29}
30
31#[cfg(test)]
32mod write_to_file_tests {
33 use super::*;
34
35 fn named_temp_file_with_path(prefix: &str) -> (PathBuf, NamedTempFile) {
39 let file = NamedTempFile::new().expect("Failed to create NamedTempFile");
40 let path = file.path().to_path_buf();
41 (path, file)
50 }
51
52 #[traced_test]
53 async fn test_write_to_file_success() {
54 info!("Starting test_write_to_file_success");
55 let (temp_path, _tempfile) = named_temp_file_with_path("success");
56
57 let json_content = r#"{"key": "value"}"#;
58 let result = write_to_file(&temp_path, json_content).await;
59 assert!(result.is_ok());
60
61 let written = fs::read_to_string(&temp_path).await.unwrap();
63 pretty_assert_eq!(written, json_content);
64
65 info!("test_write_to_file_success passed.");
67 }
68
69 #[traced_test]
70 async fn test_write_to_file_invalid_path() {
71 info!("Starting test_write_to_file_invalid_path");
72 let invalid_path = PathBuf::from("/invalid_path/test_output.json");
73 let json_content = r#"{"key": "value"}"#;
74
75 let result = write_to_file(&invalid_path, json_content).await;
76 assert!(result.is_err(), "Should fail writing to an invalid path");
77 info!("test_write_to_file_invalid_path passed.");
78 }
79
80 #[traced_test]
81 async fn returns_error_on_invalid_path() {
82 info!("Starting returns_error_on_invalid_path");
83 let invalid_path = PathBuf::from("/this/path/does/not/exist.json");
84 let json_content = r#"{"key": "value"}"#;
85
86 let result = write_to_file(&invalid_path, json_content).await;
87 debug!("Result from write_to_file: {:?}", result);
88 assert!(result.is_err(), "Expected an I/O error for invalid path");
89 info!("returns_error_on_invalid_path passed.");
90 }
91
92 #[traced_test]
93 async fn overwrites_existing_file() {
94 info!("Starting overwrites_existing_file");
95 let (temp_path, _tempfile) = named_temp_file_with_path("overwrite");
96
97 let initial = r#"{"initial": "data"}"#;
98 let updated = r#"{"updated": "data"}"#;
99
100 write_to_file(&temp_path, initial).await.unwrap();
102 write_to_file(&temp_path, updated).await.unwrap();
104
105 let final_contents = fs::read_to_string(&temp_path).await.unwrap();
107 pretty_assert_eq!(final_contents, updated);
108
109 info!("overwrites_existing_file passed.");
110 }
111
112 #[traced_test]
113 async fn handles_empty_content() {
114 info!("Starting handles_empty_content");
115 let (temp_path, _tempfile) = named_temp_file_with_path("empty");
116 let empty_content = "";
117
118 write_to_file(&temp_path, empty_content).await.unwrap();
119
120 let read_back = fs::read_to_string(&temp_path).await.unwrap();
121 assert!(read_back.is_empty(), "File should be empty after writing empty string");
122
123 info!("handles_empty_content passed.");
124 }
125
126 #[traced_test]
127 async fn handles_concurrent_writes() {
128 info!("Starting handles_concurrent_writes");
129 let sets = vec![
131 ("concurrent_test_1", r#"{"data": 1}"#),
132 ("concurrent_test_2", r#"{"data": 2}"#),
133 ("concurrent_test_3", r#"{"data": 3}"#),
134 ];
135
136 let mut tasks = Vec::new();
137 let mut file_paths = Vec::new();
138
139 for (prefix, content) in sets {
140 let (path, tempfile) = named_temp_file_with_path(prefix);
141 let content = content.to_string();
142 file_paths.push((path.clone(), tempfile)); tasks.push(tokio::spawn(async move {
144 write_to_file(&path, &content).await
145 }));
146 }
147
148 for task in tasks {
149 let res = task.await.expect("Task panicked");
150 assert!(res.is_ok(), "Concurrent write task failed");
151 }
152
153 for (path, _tempfile) in file_paths {
155 let data = fs::read_to_string(&path).await.unwrap();
156 debug!("Read from {:?}: {}", path, data);
157 assert!(data.contains("data"), "Content mismatch in concurrency test");
158 }
159
160 info!("handles_concurrent_writes passed.");
161 }
162
163 #[traced_test]
164 async fn writes_json_content_correctly() {
165 trace!("===== BEGIN_TEST: writes_json_content_correctly =====");
166
167 let (temp_path, _tempfile) = named_temp_file_with_path("writes_json_content_correctly");
169
170 let json_content = r#"{"key": "value"}"#;
171 let result = write_to_file(&temp_path, json_content).await;
172 debug!("write_to_file result: {:?}", result);
173 assert!(result.is_ok(), "Expected Ok from write_to_file");
174
175 let read_content = fs::read_to_string(&temp_path)
177 .await
178 .expect("Failed to read test file");
179 pretty_assert_eq!(read_content, json_content);
180
181 trace!("===== END_TEST: writes_json_content_correctly =====");
182 }
183}