From ca9d8a1337554a5e117a59d96345d763ef1ad18e Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sat, 20 Jun 2020 20:49:45 -0700 Subject: [PATCH 1/4] 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 --- test/cli.zig | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/cli.zig b/test/cli.zig index 3af78e1857..4c28827ba8 100644 --- a/test/cli.zig +++ b/test/cli.zig @@ -34,6 +34,7 @@ pub fn main() !void { testZigInitExe, testGodboltApi, testMissingOutputPath, + testZigFmt, }; for (test_fns) |testFn| { try fs.cwd().deleteTree(dir_path); @@ -143,3 +144,29 @@ fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void { zig_exe, "build-exe", source_path, "--output-dir", output_path, }); } + +fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void { + _ = try exec(dir_path, &[_][]const u8{ zig_exe, "init-exe" }); + + const unformatted_code = + \\fn square(num: i32) i32 { + \\return num * num; + \\} + ; + + const fmt1_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt1.zig" }); + try fs.cwd().writeFile(fmt1_zig_path, unformatted_code); + + const run_result1 = try exec(dir_path, &[_][]const u8{ zig_exe, "fmt", fmt1_zig_path }); + // stderr should be file path + \n + testing.expect(std.mem.startsWith(u8, run_result1.stderr, fmt1_zig_path)); + testing.expect(run_result1.stderr.len == fmt1_zig_path.len + 1 and run_result1.stderr[run_result1.stderr.len - 1] == '\n'); + + const fmt2_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt2.zig" }); + try fs.cwd().writeFile(fmt2_zig_path, unformatted_code); + + const run_result2 = try exec(dir_path, &[_][]const u8{ zig_exe, "fmt", dir_path }); + // running it on the dir, only the new file should be changed + testing.expect(std.mem.startsWith(u8, run_result2.stderr, fmt2_zig_path)); + testing.expect(run_result2.stderr.len == fmt2_zig_path.len + 1 and run_result2.stderr[run_result2.stderr.len - 1] == '\n'); +} From b5f90244a436b74470caedcbfbb459cb2ad90203 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sat, 20 Jun 2020 20:50:28 -0700 Subject: [PATCH 2/4] temporary: Add test-cli step for only running cli tests --- build.zig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 4d71b0fb36..2b5863a2b9 100644 --- a/build.zig +++ b/build.zig @@ -126,7 +126,10 @@ pub fn build(b: *Builder) !void { test_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes)); test_step.dependOn(tests.addStandaloneTests(b, test_filter, modes)); test_step.dependOn(tests.addStackTraceTests(b, test_filter, modes)); - test_step.dependOn(tests.addCliTests(b, test_filter, modes)); + const test_cli = tests.addCliTests(b, test_filter, modes); + const test_cli_step = b.step("test-cli", "Run zig cli tests"); + test_cli_step.dependOn(test_cli); + test_step.dependOn(test_cli); test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, modes)); test_step.dependOn(tests.addRuntimeSafetyTests(b, test_filter, modes)); test_step.dependOn(tests.addTranslateCTests(b, test_filter)); From b216d8de886b1d3b3cebb279864ec6bc91216d3e Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sat, 20 Jun 2020 22:20:23 -0700 Subject: [PATCH 3/4] Simplify unformatted code in zig fmt cli test --- test/cli.zig | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/cli.zig b/test/cli.zig index 4c28827ba8..410fa97c8f 100644 --- a/test/cli.zig +++ b/test/cli.zig @@ -148,11 +148,7 @@ fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void { fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void { _ = try exec(dir_path, &[_][]const u8{ zig_exe, "init-exe" }); - const unformatted_code = - \\fn square(num: i32) i32 { - \\return num * num; - \\} - ; + const unformatted_code = " // no reason for indent"; const fmt1_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt1.zig" }); try fs.cwd().writeFile(fmt1_zig_path, unformatted_code); From 399f6b77c40d5ebd1d54ad193a70f1935c1ebdfc Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sat, 20 Jun 2020 22:21:23 -0700 Subject: [PATCH 4/4] Add 'no changes' test to zig fmt cli test --- test/cli.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/cli.zig b/test/cli.zig index 410fa97c8f..77d79ed98e 100644 --- a/test/cli.zig +++ b/test/cli.zig @@ -165,4 +165,8 @@ fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void { // running it on the dir, only the new file should be changed testing.expect(std.mem.startsWith(u8, run_result2.stderr, fmt2_zig_path)); testing.expect(run_result2.stderr.len == fmt2_zig_path.len + 1 and run_result2.stderr[run_result2.stderr.len - 1] == '\n'); + + const run_result3 = try exec(dir_path, &[_][]const u8{ zig_exe, "fmt", dir_path }); + // both files have been formatted, nothing should change now + testing.expect(run_result3.stderr.len == 0); }