| ... | ... | @@ -790,6 +790,10 @@ pub fn addCliTests(b: *std.Build) *Step { |
| 790 | 790 | defer dir.close(); |
| 791 | 791 | dir.writeFile("fmt1.zig", unformatted_code) catch @panic("unhandled"); |
| 792 | 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"); |
| 793 | 797 | |
| 794 | 798 | // Test zig fmt affecting only the appropriate files. |
| 795 | 799 | const run1 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "fmt1.zig" }); |
| ... | ... | @@ -799,46 +803,62 @@ pub fn addCliTests(b: *std.Build) *Step { |
| 799 | 803 | // stdout should be file path + \n |
| 800 | 804 | run1.expectStdOutEqual("fmt1.zig\n"); |
| 801 | 805 | |
| 802 | | // running it on the dir, only the new file should be changed |
| 803 | | const run2 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." }); |
| 804 | | run2.setName("run zig fmt the directory"); |
| 806 | // Test excluding files and directories from a run |
| 807 | const run2 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "--exclude", "fmt2.zig", "--exclude", "subdir", "." }); |
| 808 | run2.setName("run zig fmt on directory with exclusions"); |
| 805 | 809 | run2.cwd = tmp_path; |
| 806 | 810 | run2.has_side_effects = true; |
| 807 | | run2.expectStdOutEqual("." ++ s ++ "fmt2.zig\n"); |
| 811 | run2.expectStdOutEqual(""); |
| 808 | 812 | run2.step.dependOn(&run1.step); |
| 809 | 813 | |
| 810 | | // both files have been formatted, nothing should change now |
| 811 | | const run3 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." }); |
| 812 | | run3.setName("run zig fmt with nothing to do"); |
| 814 | // Test excluding non-existent file |
| 815 | const run3 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "--exclude", "fmt2.zig", "--exclude", "nonexistent.zig", "." }); |
| 816 | run3.setName("run zig fmt on directory with non-existent exclusion"); |
| 813 | 817 | run3.cwd = tmp_path; |
| 814 | 818 | run3.has_side_effects = true; |
| 815 | | run3.expectStdOutEqual(""); |
| 819 | run3.expectStdOutEqual("." ++ s ++ "subdir" ++ s ++ "fmt3.zig\n"); |
| 816 | 820 | run3.step.dependOn(&run2.step); |
| 817 | 821 | |
| 818 | | const unformatted_code_utf16 = "\xff\xfe \x00 \x00 \x00 \x00/\x00/\x00 \x00n\x00o\x00 \x00r\x00e\x00a\x00s\x00o\x00n\x00"; |
| 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. |
| 822 | // running it on the dir, only the new file should be changed |
| 825 | 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 | 825 | run4.cwd = tmp_path; |
| 828 | 826 | run4.has_side_effects = true; |
| 829 | | run4.expectStdOutEqual("." ++ s ++ "fmt4.zig\n"); |
| 830 | | run4.step.dependOn(&write4.step); |
| 827 | run4.expectStdOutEqual("." ++ s ++ "fmt2.zig\n"); |
| 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); |
| 831 | 851 | |
| 832 | 852 | // TODO change this to an exact match |
| 833 | | const check4 = b.addCheckFile(.{ .path = fmt4_path }, .{ |
| 853 | const check6 = b.addCheckFile(.{ .path = fmt6_path }, .{ |
| 834 | 854 | .expected_matches = &.{ |
| 835 | 855 | "// no reason", |
| 836 | 856 | }, |
| 837 | 857 | }); |
| 838 | | check4.step.dependOn(&run4.step); |
| 858 | check6.step.dependOn(&run6.step); |
| 839 | 859 | |
| 840 | 860 | const cleanup = b.addRemoveDirTree(tmp_path); |
| 841 | | cleanup.step.dependOn(&check4.step); |
| 861 | cleanup.step.dependOn(&check6.step); |
| 842 | 862 | |
| 843 | 863 | step.dependOn(&cleanup.step); |
| 844 | 864 | } |