tokio-fs-ext 0.2.4

Extend tokio fs to be compatible with native and wasm
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#![feature(io_error_uncategorized)]
#![cfg(not(all(target_family = "wasm", target_os = "unknown")))]

use futures::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
use std::io;
use std::path::PathBuf;
use std::str;
use tokio_fs_ext::*;

fn get_test_path(suffix: &str) -> PathBuf {
    let path = std::env::current_dir().unwrap();
    let base_test_dir = path.join("target").join("tokio_fs_ext_test");

    std::fs::create_dir_all(&base_test_dir).unwrap();

    let suffix = suffix.trim_start_matches('/');
    base_test_dir.join(suffix)
}

#[tokio::test]
async fn test_dir_create_and_exists() {
    let path = get_test_path("/test_dir_create_and_exists");
    let _ = remove_dir_all(&path).await; // Use &path

    assert!(!try_exists(&path).await.unwrap());

    create_dir(&path).await.unwrap();
    assert!(try_exists(&path).await.unwrap());

    let _ = remove_dir_all(&path).await;
    assert!(!try_exists(&path).await.unwrap());
}

#[tokio::test]
async fn test_dir_create_all_nested() {
    let path = get_test_path("/test_dir_create_all_nested/sub/sub_sub");
    let base_path = get_test_path("/test_dir_create_all_nested");
    let _ = remove_dir_all(&base_path).await;

    assert!(!try_exists(&base_path).await.unwrap());
    assert!(!try_exists(&path).await.unwrap());

    create_dir_all(&path).await.unwrap();
    assert!(try_exists(&path).await.unwrap());
    assert!(try_exists(&base_path).await.unwrap());

    let _ = remove_dir_all(&base_path).await;
}

#[tokio::test]
#[allow(clippy::uninlined_format_args)]
async fn test_dir_read_dir_contents() {
    let base_path = get_test_path("/test_dir_read_dir_contents");
    // Use PathBuf::join for constructing paths
    let dir_path = base_path.join("dir_inside");
    let file_path = base_path.join("file_inside");
    let _ = remove_dir_all(&base_path).await;
    create_dir_all(&base_path).await.unwrap();

    create_dir(&dir_path).await.unwrap();
    write(&file_path, "some content").await.unwrap();

    let mut rd = read_dir(&base_path).await.unwrap();
    let mut entries = Vec::new();

    while let Some(entry) = rd.next_entry().await.unwrap() {
        entries.push((
            entry.file_type().await.unwrap().is_dir(),
            entry.file_name().to_string_lossy().to_string(),
        ));
    }

    entries.sort_by_key(|e| e.0);

    assert_eq!(
        entries,
        vec![
            (false, "file_inside".to_string()),
            (true, "dir_inside".to_string())
        ]
    );
    assert!(rd.next_entry().await.unwrap().is_none());

    let _ = remove_dir_all(&base_path).await;
}

#[tokio::test]
async fn test_dir_non_existent_path() {
    let path = get_test_path("/non_existent_dir_path");
    let _ = remove_dir_all(&path).await;

    assert!(!try_exists(&path).await.unwrap());
}

// --- test_file split into smaller tests ---

#[tokio::test]
async fn test_file_create_write_read() {
    let path = get_test_path("/test_file_create_write_read/file.txt");
    let data = "hello world";
    let base_dir = get_test_path("/test_file_create_write_read");
    let _ = remove_dir_all(&base_dir).await;
    create_dir_all(&base_dir).await.unwrap();

    assert!(!try_exists(&path).await.unwrap());

    write(&path, data.as_bytes()).await.unwrap();
    assert!(try_exists(&path).await.unwrap());

    let read_data = read(&path).await.unwrap();
    assert_eq!(read_data, data.as_bytes());

    let _ = remove_dir_all(&base_dir).await;
}

#[tokio::test]
#[allow(clippy::uninlined_format_args)]
async fn test_file_copy() {
    let path = get_test_path("/test_file_copy/original.txt");
    let copy_path = get_test_path("/test_file_copy/original.txt_copy"); // Construct directly
    let data = "copy me";
    let base_dir = get_test_path("/test_file_copy");
    let _ = remove_dir_all(&base_dir).await;
    create_dir_all(&base_dir).await.unwrap();

    write(&path, data.as_bytes()).await.unwrap();
    copy(&path, &copy_path).await.unwrap(); // Pass references

    assert!(try_exists(&copy_path).await.unwrap());
    assert_eq!(read(&copy_path).await.unwrap(), data.as_bytes());

    let _ = remove_dir_all(&base_dir).await;
}

#[tokio::test]
#[allow(clippy::uninlined_format_args)]
async fn test_file_rename() {
    let path = get_test_path("/test_file_rename/old_name.txt");
    let rename_path = get_test_path("/test_file_rename/old_name.txt_rename"); // Construct directly
    let data = "rename me";
    let base_dir = get_test_path("/test_file_rename");
    let _ = remove_dir_all(&base_dir).await;
    create_dir_all(&base_dir).await.unwrap();

    write(&path, data.as_bytes()).await.unwrap();
    rename(&path, &rename_path).await.unwrap(); // Pass references

    assert!(!try_exists(&path).await.unwrap());
    assert!(try_exists(&rename_path).await.unwrap());
    assert_eq!(read(&rename_path).await.unwrap(), data.as_bytes());

    let _ = remove_dir_all(&base_dir).await;
}

#[tokio::test]
async fn test_file_read_to_string() {
    let path = get_test_path("/test_file_read_to_string/string_file.txt");
    let data = "this is a string";
    let base_dir = get_test_path("/test_file_read_to_string");
    let _ = remove_dir_all(&base_dir).await;
    create_dir_all(&base_dir).await.unwrap();

    write(&path, data.as_bytes()).await.unwrap();
    assert_eq!(read_to_string(&path).await.unwrap(), data);

    let _ = remove_dir_all(&base_dir).await;
}

#[tokio::test]
async fn test_file_read_to_end_small() {
    let path = get_test_path("/test_file_read_to_end/test_file_read_to_end_small.txt");
    let data = "this is for read_to_end ";
    let base_dir = get_test_path("/test_file_read_to_end");
    let _ = remove_dir_all(&base_dir).await;
    create_dir_all(&base_dir).await.unwrap();

    write(&path, data.as_bytes()).await.unwrap();
    let mut file = OpenOptions::new().read(true).open(&path).await.unwrap();
    let mut buffer = vec![];

    assert!(file.read_to_end(&mut buffer).await.is_ok());
    assert_eq!(str::from_utf8(&buffer).unwrap(), data);

    let _ = remove_dir_all(&base_dir).await;
}

#[tokio::test]
async fn test_file_read_to_end_big() {
    let path = get_test_path("/test_file_read_to_end/test_file_read_to_end_big.txt");
    let data = "this is for read_to_end ".repeat(10);
    let base_dir = get_test_path("/test_file_read_to_end");
    let _ = remove_dir_all(&base_dir).await;
    create_dir_all(&base_dir).await.unwrap();

    write(&path, data.as_bytes()).await.unwrap();
    let mut file = OpenOptions::new().read(true).open(&path).await.unwrap();
    let mut buffer = vec![];

    assert!(file.read_to_end(&mut buffer).await.is_ok());
    assert_eq!(str::from_utf8(&buffer).unwrap(), data);

    let _ = remove_dir_all(&base_dir).await;
}

#[tokio::test]
async fn test_file_remove() {
    let path = get_test_path("/test_file_remove/file_to_remove.txt");
    let base_dir = get_test_path("/test_file_remove");
    let _ = remove_dir_all(&base_dir).await;
    create_dir_all(&base_dir).await.unwrap();

    write(&path, "content").await.unwrap();
    assert!(try_exists(&path).await.unwrap());

    remove_file(&path).await.unwrap();
    assert!(!try_exists(&path).await.unwrap());

    let _ = remove_dir_all(&base_dir).await;
}

#[tokio::test]
async fn test_open_options_create_new_fails_if_exists() {
    let path = get_test_path("/test_open_options_create_new_fails_if_exists");
    let _ = remove_file(&path).await;
    write(&path, "dummy").await.unwrap();

    let err = OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&path)
        .await
        .unwrap_err();

    assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);

    let _ = remove_file(&path).await;
}

#[tokio::test]
async fn test_open_options_create_new_succeeds_if_not_exists() {
    let path = get_test_path("/test_open_options_create_new_succeeds_if_not_exists");
    let _ = remove_file(&path).await;

    let result = OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&path)
        .await;
    assert!(result.is_ok());

    let _ = remove_file(&path).await;
}

#[tokio::test]
async fn test_open_options_create_succeeds() {
    let path = get_test_path("/test_open_options_create_succeeds");
    let _ = remove_file(&path).await;

    let result = OpenOptions::new()
        .write(true)
        .create(true)
        .open(&path)
        .await;
    assert!(result.is_ok());

    let _ = remove_file(&path).await;
}

#[tokio::test]
async fn test_open_options_readonly_permission_denied() {
    let path = get_test_path("/test_open_options_readonly_permission_denied");
    {
        let _ = remove_file(&path).await;
        let _ = OpenOptions::new()
            .create(true)
            .write(true)
            .open(&path)
            .await
            .unwrap();
    }
    let mut readonly_file = OpenOptions::new().read(true).open(&path).await.unwrap();
    readonly_file
        .write_all("try write failed".as_bytes())
        .await
        .unwrap();

    let err = readonly_file.flush().await.unwrap_err();

    assert_eq!(err.kind(), io::ErrorKind::Uncategorized);

    let _ = remove_file(&path).await;
}

#[tokio::test]
async fn test_open_options_read_write_behavior() {
    let path = get_test_path("/test_open_options_read_write_behavior");
    let contents = "somedata".repeat(16);
    let _ = remove_file(&path).await;

    {
        let mut rw_file = OpenOptions::new()
            .write(true)
            .read(true)
            .create(true)
            .open(&path)
            .await
            .unwrap();

        assert!(rw_file.write(contents.as_bytes()).await.is_ok());
        rw_file.seek(io::SeekFrom::Start(0)).await.unwrap();
        let mut data = vec![];
        assert!(rw_file.read_to_end(&mut data).await.is_ok());
        assert_eq!(data.as_slice(), contents.as_bytes());
    }

    {
        let mut rw_file = OpenOptions::new().read(true).open(&path).await.unwrap();

        let mut data = vec![];
        assert!(rw_file.read_to_end(&mut data).await.is_ok());
        assert_eq!(data.as_slice(), contents.as_bytes());
    }

    let _ = remove_dir_all(&path).await; // Changed to remove_dir_all as it might be a file or directory
}

#[tokio::test]
async fn test_open_options_truncate() {
    let path = get_test_path("/test_open_options_truncate");
    let initial_content = "initial content";
    let _ = remove_file(&path).await;

    write(&path, initial_content.as_bytes()).await.unwrap();
    assert_eq!(read(&path).await.unwrap(), initial_content.as_bytes());

    {
        let _truncate = OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(true)
            .open(&path)
            .await
            .unwrap();
    };
    assert!(read(&path).await.unwrap().is_empty());

    let _ = remove_file(&path).await;
}

#[tokio::test]
async fn test_open_options_append() {
    let path = get_test_path("/test_open_options_append");
    let initial_content = "append";
    let additional_content = "append";
    let _ = remove_file(&path).await;

    write(&path, initial_content.as_bytes()).await.unwrap();

    let mut append = OpenOptions::new()
        .read(true)
        .write(true)
        .append(true)
        .open(&path)
        .await
        .unwrap();

    append
        .write_all(additional_content.as_bytes())
        .await
        .unwrap();
    append.seek(io::SeekFrom::Start(0)).await.unwrap();
    let mut data = vec![];
    append.read_to_end(&mut data).await.unwrap();
    assert_eq!(
        data.as_slice(),
        (initial_content.to_string() + additional_content).as_bytes()
    );

    let _ = remove_file(&path).await;
}

#[tokio::test]
async fn test_metadata_not_found() {
    let path = get_test_path("/test_metadata_not_found");
    let _ = remove_dir_all(&path).await;

    let err = metadata(&path).await.unwrap_err();
    assert_eq!(err.kind(), io::ErrorKind::NotFound);
}

#[tokio::test]
async fn test_metadata_is_dir() {
    let path = get_test_path("/test_metadata_is_dir");
    let _ = remove_dir_all(&path).await;

    create_dir(&path).await.unwrap();
    let meta = metadata(&path).await.unwrap();
    assert!(meta.is_dir());

    let _ = remove_dir_all(&path).await;
}

#[tokio::test]
#[allow(clippy::uninlined_format_args)]
async fn test_metadata_is_file_and_len() {
    let dir_path = get_test_path("/test_metadata_is_file_and_len_dir");
    let file_path = dir_path.join("file_with_content.txt"); // Use PathBuf::join
    let content = "some file content";
    let _ = remove_dir_all(&dir_path).await;
    create_dir(&dir_path).await.unwrap();

    write(&file_path, content.as_bytes()).await.unwrap();
    let f_metadata = metadata(&file_path).await.unwrap();

    assert!(f_metadata.is_file());
    assert_eq!(f_metadata.len(), content.len() as u64);

    let _ = remove_dir_all(&dir_path).await;
}

#[tokio::test]
async fn test_async_seek() {
    let path = get_test_path("/test_async_seek/seek_file.txt");
    let initial_content = "Hello, world!"; // 13 bytes
    let overwrite_content = "Rust"; // 4 bytes
    let expected_content = "Hello, Rustd!"; // 13 bytes
    let base_dir = get_test_path("/test_async_seek");
    let _ = remove_dir_all(&base_dir).await;
    create_dir_all(&base_dir).await.unwrap();

    write(&path, initial_content.as_bytes()).await.unwrap();
    assert_eq!(read(&path).await.unwrap(), initial_content.as_bytes());

    let mut file = OpenOptions::new()
        .read(true)
        .write(true)
        .open(&path)
        .await
        .unwrap();

    // Seek to a specific position (e.g., after "Hello, ")
    let seek_pos = "Hello, ".len() as u64;
    let current_pos = file.seek(io::SeekFrom::Start(seek_pos)).await.unwrap();
    assert_eq!(
        current_pos, seek_pos,
        "Seek should move cursor to correct position"
    );

    file.write_all(overwrite_content.as_bytes()).await.unwrap();

    file.seek(io::SeekFrom::Start(0)).await.unwrap();
    let mut buffer = vec![];
    file.read_to_end(&mut buffer).await.unwrap();

    assert_eq!(
        str::from_utf8(&buffer).unwrap(),
        expected_content,
        "File content should be updated after seek and write"
    );

    file.seek(io::SeekFrom::Start(0)).await.unwrap();
    file.seek(io::SeekFrom::Current(6)).await.unwrap();
    let mut partial_buffer = vec![0; 6];
    file.read_exact(&mut partial_buffer).await.unwrap();
    assert_eq!(
        str::from_utf8(&partial_buffer).unwrap(),
        " Rustd",
        "Seeking from current should work"
    );

    let _ = remove_dir_all(&base_dir).await;
}