authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-06-20 20:49:45-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-06-20 22:02:55-07:00
logca9d8a1337554a5e117a59d96345d763ef1ad18e
treed9efd2a15aa93505c99b34a5241eeccc4208b398
parentedea7a46e5484e30c338699207c8f4b4d8370c97

Add zig fmt test to cli tests for both files and directories

Should catch basic `zig fmt` regressions that were previously going uncaught and breaking things

1 files changed, 27 insertions(+), 0 deletions(-)

test/cli.zig+27
......@@ -34,6 +34,7 @@ pub fn main() !void {
3434 testZigInitExe,
3535 testGodboltApi,
3636 testMissingOutputPath,
37 testZigFmt,
3738 };
3839 for (test_fns) |testFn| {
3940 try fs.cwd().deleteTree(dir_path);
......@@ -143,3 +144,29 @@ fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void {
143144 zig_exe, "build-exe", source_path, "--output-dir", output_path,
144145 });
145146}
147
148fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
149 _ = try exec(dir_path, &[_][]const u8{ zig_exe, "init-exe" });
150
151 const unformatted_code =
152 \\fn square(num: i32) i32 {
153 \\return num * num;
154 \\}
155 ;
156
157 const fmt1_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt1.zig" });
158 try fs.cwd().writeFile(fmt1_zig_path, unformatted_code);
159
160 const run_result1 = try exec(dir_path, &[_][]const u8{ zig_exe, "fmt", fmt1_zig_path });
161 // stderr should be file path + \n
162 testing.expect(std.mem.startsWith(u8, run_result1.stderr, fmt1_zig_path));
163 testing.expect(run_result1.stderr.len == fmt1_zig_path.len + 1 and run_result1.stderr[run_result1.stderr.len - 1] == '\n');
164
165 const fmt2_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt2.zig" });
166 try fs.cwd().writeFile(fmt2_zig_path, unformatted_code);
167
168 const run_result2 = try exec(dir_path, &[_][]const u8{ zig_exe, "fmt", dir_path });
169 // running it on the dir, only the new file should be changed
170 testing.expect(std.mem.startsWith(u8, run_result2.stderr, fmt2_zig_path));
171 testing.expect(run_result2.stderr.len == fmt2_zig_path.len + 1 and run_result2.stderr[run_result2.stderr.len - 1] == '\n');
172}