| ... | ... | @@ -34,6 +34,7 @@ pub fn main() !void { |
| 34 | 34 | testZigInitExe, |
| 35 | 35 | testGodboltApi, |
| 36 | 36 | testMissingOutputPath, |
| 37 | testZigFmt, |
| 37 | 38 | }; |
| 38 | 39 | for (test_fns) |testFn| { |
| 39 | 40 | try fs.cwd().deleteTree(dir_path); |
| ... | ... | @@ -143,3 +144,29 @@ fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void { |
| 143 | 144 | zig_exe, "build-exe", source_path, "--output-dir", output_path, |
| 144 | 145 | }); |
| 145 | 146 | } |
| 147 | |
| 148 | fn 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 = " // no reason for indent"; |
| 152 | |
| 153 | const fmt1_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt1.zig" }); |
| 154 | try fs.cwd().writeFile(fmt1_zig_path, unformatted_code); |
| 155 | |
| 156 | const run_result1 = try exec(dir_path, &[_][]const u8{ zig_exe, "fmt", fmt1_zig_path }); |
| 157 | // stderr should be file path + \n |
| 158 | testing.expect(std.mem.startsWith(u8, run_result1.stderr, fmt1_zig_path)); |
| 159 | testing.expect(run_result1.stderr.len == fmt1_zig_path.len + 1 and run_result1.stderr[run_result1.stderr.len - 1] == '\n'); |
| 160 | |
| 161 | const fmt2_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt2.zig" }); |
| 162 | try fs.cwd().writeFile(fmt2_zig_path, unformatted_code); |
| 163 | |
| 164 | const run_result2 = try exec(dir_path, &[_][]const u8{ zig_exe, "fmt", dir_path }); |
| 165 | // running it on the dir, only the new file should be changed |
| 166 | testing.expect(std.mem.startsWith(u8, run_result2.stderr, fmt2_zig_path)); |
| 167 | testing.expect(run_result2.stderr.len == fmt2_zig_path.len + 1 and run_result2.stderr[run_result2.stderr.len - 1] == '\n'); |
| 168 | |
| 169 | const run_result3 = try exec(dir_path, &[_][]const u8{ zig_exe, "fmt", dir_path }); |
| 170 | // both files have been formatted, nothing should change now |
| 171 | testing.expect(run_result3.stderr.len == 0); |
| 172 | } |