authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-21 14:54:58-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-06-21 14:54:58-04:00
log126f5702df2ac86ced868d9e532db44313144452
tree7f87902f0857556d8273ce3c7e1128802ab819c7
parentb70c38c33cd00d63783c51e440a9d30035b9eee3
parent399f6b77c40d5ebd1d54ad193a70f1935c1ebdfc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5655 from squeek502/zig-fmt-cli-test

Add zig fmt to cli tests

2 files changed, 31 insertions(+), 1 deletions(-)

build.zig+4-1
......@@ -126,7 +126,10 @@ pub fn build(b: *Builder) !void {
126126 test_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));
127127 test_step.dependOn(tests.addStandaloneTests(b, test_filter, modes));
128128 test_step.dependOn(tests.addStackTraceTests(b, test_filter, modes));
129 test_step.dependOn(tests.addCliTests(b, test_filter, modes));
129 const test_cli = tests.addCliTests(b, test_filter, modes);
130 const test_cli_step = b.step("test-cli", "Run zig cli tests");
131 test_cli_step.dependOn(test_cli);
132 test_step.dependOn(test_cli);
130133 test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, modes));
131134 test_step.dependOn(tests.addRuntimeSafetyTests(b, test_filter, modes));
132135 test_step.dependOn(tests.addTranslateCTests(b, test_filter));
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 = " // 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}