From a3540000909bdc6a59ba07c85d21afeb3a7e54e2 Mon Sep 17 00:00:00 2001 From: Ersikan Date: Sun, 14 Mar 2021 07:03:22 +0100 Subject: zig fmt: fix non-UTF-8 encoding #2820 Fixes #2820 After reading the source code, the first two bytes are inspected, and if they correspond to a UTF-16 BOM in little-endian order, the source code is converted to UTF-8. --- test/cli.zig | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test/cli.zig') diff --git a/test/cli.zig b/test/cli.zig index c0702fa54c..db58511d14 100644 --- a/test/cli.zig +++ b/test/cli.zig @@ -174,4 +174,13 @@ fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void { const run_result3 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path }); // both files have been formatted, nothing should change now testing.expect(run_result3.stdout.len == 0); + + // Check UTF-16 decoding + const fmt4_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt4.zig" }); + var unformatted_code_utf16 = "\xff\xfe \x00 \x00 \x00 \x00/\x00/\x00 \x00n\x00o\x00 \x00r\x00e\x00a\x00s\x00o\x00n\x00"; + try fs.cwd().writeFile(fmt4_zig_path, unformatted_code_utf16); + + const run_result4 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path }); + testing.expect(std.mem.startsWith(u8, run_result4.stdout, fmt4_zig_path)); + testing.expect(run_result4.stdout.len == fmt4_zig_path.len + 1 and run_result4.stdout[run_result4.stdout.len - 1] == '\n'); } -- cgit v1.2.3 From 36db4b7cc48e9e2b1bed5a977f36ef3a0f158ee8 Mon Sep 17 00:00:00 2001 From: Ersikan Date: Sun, 14 Mar 2021 18:12:42 +0100 Subject: test-cli: Remove temporary directory after tests --- test/cli.zig | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test/cli.zig') diff --git a/test/cli.zig b/test/cli.zig index db58511d14..dedea67a59 100644 --- a/test/cli.zig +++ b/test/cli.zig @@ -28,6 +28,8 @@ pub fn main() !void { const zig_exe = try fs.path.resolve(a, &[_][]const u8{zig_exe_rel}); const dir_path = try fs.path.join(a, &[_][]const u8{ cache_root, "clitest" }); + defer fs.cwd().deleteTree(dir_path) catch {}; + const TestFn = fn ([]const u8, []const u8) anyerror!void; const test_fns = [_]TestFn{ testZigInitLib, -- cgit v1.2.3