authorgravatar for ian@ianjohnson.devIan Johnson <ian@ianjohnson.dev> 2023-06-24 22:50:28-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-24 19:41:59-07:00
log9892ec31a06d2c5806ccfbdf4cd03f1a40de3ab8
treefd3f77739ed46bc04f0be0f836f97196a0be4390
parent10d940d7f4c081f4dd7e15513b972940335b465c

zig fmt: make `--exclude` work on files

Closes #16178

2 files changed, 48 insertions(+), 25 deletions(-)

src/main.zig+7-4
...@@ -4752,13 +4752,16 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void...@@ -4752,13 +4752,16 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
4752 // Mark any excluded files/directories as already seen,4752 // Mark any excluded files/directories as already seen,
4753 // so that they are skipped later during actual processing4753 // so that they are skipped later during actual processing
4754 for (excluded_files.items) |file_path| {4754 for (excluded_files.items) |file_path| {
4755 var dir = fs.cwd().openDir(file_path, .{}) catch |err| switch (err) {4755 const stat = fs.cwd().statFile(file_path) catch |err| switch (err) {
4756 error.FileNotFound => continue,4756 error.FileNotFound => continue,
4757 // On Windows, statFile does not work for directories
4758 error.IsDir => dir: {
4759 var dir = try fs.cwd().openDir(file_path, .{});
4760 defer dir.close();
4761 break :dir try dir.stat();
4762 },
4757 else => |e| return e,4763 else => |e| return e,
4758 };4764 };
4759 defer dir.close();
4760
4761 const stat = try dir.stat();
4762 try fmt.seen.put(stat.inode, {});4765 try fmt.seen.put(stat.inode, {});
4763 }4766 }
47644767
test/tests.zig+41-21
...@@ -790,6 +790,10 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -790,6 +790,10 @@ pub fn addCliTests(b: *std.Build) *Step {
790 defer dir.close();790 defer dir.close();
791 dir.writeFile("fmt1.zig", unformatted_code) catch @panic("unhandled");791 dir.writeFile("fmt1.zig", unformatted_code) catch @panic("unhandled");
792 dir.writeFile("fmt2.zig", unformatted_code) catch @panic("unhandled");792 dir.writeFile("fmt2.zig", unformatted_code) catch @panic("unhandled");
793 dir.makeDir("subdir") catch @panic("unhandled");
794 var subdir = dir.openDir("subdir", .{}) catch @panic("unhandled");
795 defer subdir.close();
796 subdir.writeFile("fmt3.zig", unformatted_code) catch @panic("unhandled");
793797
794 // Test zig fmt affecting only the appropriate files.798 // Test zig fmt affecting only the appropriate files.
795 const run1 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "fmt1.zig" });799 const run1 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "fmt1.zig" });
...@@ -799,46 +803,62 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -799,46 +803,62 @@ pub fn addCliTests(b: *std.Build) *Step {
799 // stdout should be file path + \n803 // stdout should be file path + \n
800 run1.expectStdOutEqual("fmt1.zig\n");804 run1.expectStdOutEqual("fmt1.zig\n");
801805
802 // running it on the dir, only the new file should be changed806 // Test excluding files and directories from a run
803 const run2 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });807 const run2 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "--exclude", "fmt2.zig", "--exclude", "subdir", "." });
804 run2.setName("run zig fmt the directory");808 run2.setName("run zig fmt on directory with exclusions");
805 run2.cwd = tmp_path;809 run2.cwd = tmp_path;
806 run2.has_side_effects = true;810 run2.has_side_effects = true;
807 run2.expectStdOutEqual("." ++ s ++ "fmt2.zig\n");811 run2.expectStdOutEqual("");
808 run2.step.dependOn(&run1.step);812 run2.step.dependOn(&run1.step);
809813
810 // both files have been formatted, nothing should change now814 // Test excluding non-existent file
811 const run3 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });815 const run3 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "--exclude", "fmt2.zig", "--exclude", "nonexistent.zig", "." });
812 run3.setName("run zig fmt with nothing to do");816 run3.setName("run zig fmt on directory with non-existent exclusion");
813 run3.cwd = tmp_path;817 run3.cwd = tmp_path;
814 run3.has_side_effects = true;818 run3.has_side_effects = true;
815 run3.expectStdOutEqual("");819 run3.expectStdOutEqual("." ++ s ++ "subdir" ++ s ++ "fmt3.zig\n");
816 run3.step.dependOn(&run2.step);820 run3.step.dependOn(&run2.step);
817821
818 const unformatted_code_utf16 = "\xff\xfe \x00 \x00 \x00 \x00/\x00/\x00 \x00n\x00o\x00 \x00r\x00e\x00a\x00s\x00o\x00n\x00";822 // running it on the dir, only the new file should be changed
819 const fmt4_path = std.fs.path.join(b.allocator, &.{ tmp_path, "fmt4.zig" }) catch @panic("OOM");
820 const write4 = b.addWriteFiles();
821 write4.addBytesToSource(unformatted_code_utf16, fmt4_path);
822 write4.step.dependOn(&run3.step);
823
824 // Test `zig fmt` handling UTF-16 decoding.
825 const run4 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });823 const run4 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });
826 run4.setName("run zig fmt convert UTF-16 to UTF-8");824 run4.setName("run zig fmt the directory");
827 run4.cwd = tmp_path;825 run4.cwd = tmp_path;
828 run4.has_side_effects = true;826 run4.has_side_effects = true;
829 run4.expectStdOutEqual("." ++ s ++ "fmt4.zig\n");827 run4.expectStdOutEqual("." ++ s ++ "fmt2.zig\n");
830 run4.step.dependOn(&write4.step);828 run4.step.dependOn(&run3.step);
829
830 // both files have been formatted, nothing should change now
831 const run5 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });
832 run5.setName("run zig fmt with nothing to do");
833 run5.cwd = tmp_path;
834 run5.has_side_effects = true;
835 run5.expectStdOutEqual("");
836 run5.step.dependOn(&run4.step);
837
838 const unformatted_code_utf16 = "\xff\xfe \x00 \x00 \x00 \x00/\x00/\x00 \x00n\x00o\x00 \x00r\x00e\x00a\x00s\x00o\x00n\x00";
839 const fmt6_path = std.fs.path.join(b.allocator, &.{ tmp_path, "fmt6.zig" }) catch @panic("OOM");
840 const write6 = b.addWriteFiles();
841 write6.addBytesToSource(unformatted_code_utf16, fmt6_path);
842 write6.step.dependOn(&run5.step);
843
844 // Test `zig fmt` handling UTF-16 decoding.
845 const run6 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });
846 run6.setName("run zig fmt convert UTF-16 to UTF-8");
847 run6.cwd = tmp_path;
848 run6.has_side_effects = true;
849 run6.expectStdOutEqual("." ++ s ++ "fmt6.zig\n");
850 run6.step.dependOn(&write6.step);
831851
832 // TODO change this to an exact match852 // TODO change this to an exact match
833 const check4 = b.addCheckFile(.{ .path = fmt4_path }, .{853 const check6 = b.addCheckFile(.{ .path = fmt6_path }, .{
834 .expected_matches = &.{854 .expected_matches = &.{
835 "// no reason",855 "// no reason",
836 },856 },
837 });857 });
838 check4.step.dependOn(&run4.step);858 check6.step.dependOn(&run6.step);
839859
840 const cleanup = b.addRemoveDirTree(tmp_path);860 const cleanup = b.addRemoveDirTree(tmp_path);
841 cleanup.step.dependOn(&check4.step);861 cleanup.step.dependOn(&check6.step);
842862
843 step.dependOn(&cleanup.step);863 step.dependOn(&cleanup.step);
844 }864 }