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
// Copyright 2025 Shingo OKAWA. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! This module contains a set of testing utilities of IO operations.
use BufRead;
use Write;
use ;
use NamedTempFile;
/// Reads the contents of a file line by line using buffered I/O.
///
/// This function opens the file at the specified path and returns an iterator over its lines,
/// where each line is lazily read and returned as a `Result<String, io::Error>`. It utilizes
/// a `BufReader` for efficient I/O operations.
///
/// # Parameters
/// - `path`: The path to the file to be read. Accepts any type that implements `AsRef<Path>`.
///
/// # Returns
/// - An `Result` containing an iterator over the lines of the file, where each line
/// is represented as a `Result<String, io::Error>`.
///
/// # Examples
/// ```no_run
/// use regd_testing;
///
/// let lines = regd_testing::io::read_lines("Cargo.toml").expect("failed to open file");
/// for line in lines {
/// let line = line.expect("failed to read line");
/// println!("{}", line);
/// }
/// ```
/// Creates a new file at the specified path and writes the given content to it.
///
/// This function attempts to create a file at the provided path, writes the specified string
/// content to it, and flushes the buffer to ensure all data is persisted to disk.
///
/// # Parameters
/// - `path`: The target path for the new file. Accepts any type implementing `AsRef<Path>`.
/// - `content`: The string content to write into the newly created file. Accepts any type implementing `AsRef<str>`.
///
/// # Returns
/// - An `Result` containing the created `File` handle if successful, or an error if file creation or writing fails.
///
/// # Examples
/// ```no_run
/// use regd_testing;
///
/// let file = regd_testing::io::try_new_file("output.txt", "Hello, world!")
/// .expect("failed to create or write to file");
/// ```
/// Creates a new temporary file and writes the given content into it.
///
/// This function generates a temporary file using the system’s default temporary
/// directory, writes the provided string content into it, and returns a handle to the file.
/// The temporary file will be automatically deleted when dropped.
///
/// # Parameters
/// - `content`: The string content to write into the temporary file. Accepts any type implementing `AsRef<str>`.
///
/// # Returns
/// - An `Result` containing a `NamedTempFile` handle if the operation succeeds, or an error if it fails.
///
/// # Examples
/// ```no_run
/// use regd_testing;
///
/// let tempfile = regd_testing::io::try_new_tempfile("Temporary data")
/// .expect("failed to create temporary file");
///
/// println!("Temp file path: {:?}", tempfile.path());
/// ```
/// Attempts to remove a file at the specified path, retrying up to 4 times on failure.
///
/// This function tries to delete the file located at the given path. If the removal
/// fails (e.g., due to temporary filesystem locks or race conditions), it will retry
/// up to four times before returning the final error.
///
/// # Parameters
/// - `path`: The path to the file to be removed. Accepts any type implementing `AsRef<Path>`.
///
/// # Returns
/// - An `Result` indicating success or failure. Returns `Ok(())` if the file is removed,
/// or an `Err` if all attempts fail.
///
/// # Examples
/// ```no_run
/// use regd_testing;
///
/// regd_testing::io::try_remove_file("temp.txt").expect("failed to remove file");
/// ```