refactor: add test to check file mode preservation on write

This commit is contained in:
CJ van den Berg 2026-04-14 16:38:40 +02:00
parent 2ae8d3048d
commit 8c0cfb96d2
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");
const Buffer = @import("Buffer");
const ArrayList = std.ArrayList;
@ -99,6 +100,39 @@ test "buffer.store_to_file_and_clean" {
try std.testing.expectEqualStrings(input, output);
}
test "buffer.store_to_file_and_clean preserves file mode" {
if (comptime builtin.os.tag == .windows) return error.SkipZigTest;
const tmp_path = "test/tmp_mode_test.txt";
{
const f = try std.fs.cwd().createFile(tmp_path, .{});
defer f.close();
try f.writeAll("hello\n");
try f.chmod(0o644);
}
defer std.fs.cwd().deleteFile(tmp_path) catch {};
// Set a umask that would strip group/other read bits (0o644 -> 0o600 without the fix)
// to verify that fchmod bypasses the process umask on save.
const prev_umask = if (comptime builtin.os.tag == .linux)
std.os.linux.syscall1(.umask, 0o077)
else
@as(usize, 0);
defer if (comptime builtin.os.tag == .linux) {
_ = std.os.linux.syscall1(.umask, prev_umask);
};
const buffer = try Buffer.create(a);
defer buffer.deinit();
try buffer.load_from_file_and_update(tmp_path);
try buffer.store_to_file_and_clean(tmp_path);
const f = try std.fs.cwd().openFile(tmp_path, .{});
defer f.close();
const stat = try std.posix.fstat(f.handle);
try std.testing.expectEqual(@as(std.posix.mode_t, 0o644), stat.mode & 0o777);
}
fn get_line(buf: *const Buffer, line: usize) ![]const u8 {
var result: std.Io.Writer.Allocating = .init(a);
try buf.root.get_line(line, &result.writer, metrics());