Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions library/std/src/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,7 @@ fn open_flavors() {

// This error string is set by std itself so we are not at the whim of the OS here.
let invalid_options = "creating or truncating a file requires write or append access";
let append_truncate_error = "append and truncate cannot both be enabled";

// Test various combinations of creation modes and access modes.
//
Expand Down Expand Up @@ -1543,15 +1544,21 @@ fn open_flavors() {

// append
check!(c(&a).create_new(true).open(&tmpdir.join("d")));
error_contains!(c(&a).create(true).truncate(true).open(&tmpdir.join("d")), invalid_options);
error_contains!(c(&a).truncate(true).open(&tmpdir.join("d")), invalid_options);
error_contains!(
c(&a).create(true).truncate(true).open(&tmpdir.join("d")),
append_truncate_error
);
error_contains!(c(&a).truncate(true).open(&tmpdir.join("d")), append_truncate_error);
check!(c(&a).create(true).open(&tmpdir.join("d")));
check!(c(&a).open(&tmpdir.join("d")));

// read-append
check!(c(&ra).create_new(true).open(&tmpdir.join("e")));
error_contains!(c(&ra).create(true).truncate(true).open(&tmpdir.join("e")), invalid_options);
error_contains!(c(&ra).truncate(true).open(&tmpdir.join("e")), invalid_options);
error_contains!(
c(&ra).create(true).truncate(true).open(&tmpdir.join("e")),
append_truncate_error
);
error_contains!(c(&ra).truncate(true).open(&tmpdir.join("e")), append_truncate_error);
check!(c(&ra).create(true).open(&tmpdir.join("e")));
check!(c(&ra).open(&tmpdir.join("e")));

Expand Down Expand Up @@ -2362,7 +2369,6 @@ fn test_open_options_invalid_combinations() {
(|| OO::new().create(true).read(true).clone(), "create without write"),
(|| OO::new().create_new(true).read(true).clone(), "create_new without write"),
(|| OO::new().truncate(true).read(true).clone(), "truncate without write"),
(|| OO::new().truncate(true).append(true).clone(), "truncate with append"),
];

for (make_opts, desc) in test_cases {
Expand All @@ -2377,7 +2383,13 @@ fn test_open_options_invalid_combinations() {
"{desc} - wrong error message"
);
}
let result = OO::new().truncate(true).append(true).open("nonexistent.txt");

assert!(result.is_err(), "truncate with append should fail");

let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidInput);
assert_eq!(err.to_string(), "append and truncate cannot both be enabled");
let result = OO::new().open("nonexistent.txt");
assert!(result.is_err(), "no access mode should fail");
let err = result.unwrap_err();
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/fs/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ impl OpenOptions {
if self.truncate && !self.create_new {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"creating or truncating a file requires write or append access",
"append and truncate cannot both be enabled",
));
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/fs/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl OpenOptions {
if self.truncate && !self.create_new {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"creating or truncating a file requires write or append access",
"append and truncate cannot both be enabled",
));
}
}
Expand Down
Loading