diff options
| author | Loris Cro <kappaloris@gmail.com> | 2025-02-15 20:03:04 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-02-26 14:41:33 -0500 |
| commit | c75fdd96d2cb9bd211ff20c7e9ee2ef119cc189f (patch) | |
| tree | ace11890aff81e32716f4f9bd227ba619b3c80fa /test | |
| parent | 06a66745a0b5c666608482645bc61523618dab85 (diff) | |
| download | zig-c75fdd96d2cb9bd211ff20c7e9ee2ef119cc189f.tar.gz zig-c75fdd96d2cb9bd211ff20c7e9ee2ef119cc189f.zip | |
`@deprecated`: add tests
Diffstat (limited to 'test')
| -rw-r--r-- | test/compile_errors.zig | 14 | ||||
| -rw-r--r-- | test/tests.zig | 94 |
2 files changed, 108 insertions, 0 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 109347cde8..03e9324b02 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -250,4 +250,18 @@ pub fn addCases(ctx: *Cases, b: *std.Build) !void { ":1:5: error: expected expression, found 'invalid token'", }); } + + { + const case = ctx.obj("usage of deprecated code", b.graph.host); + + case.addError( + \\const bad = @deprecated(42); + \\ + \\pub export fn foo() usize { + \\ return bad; + \\} + , &[_][]const u8{ + ":1:13: error: found deprecated code", + }); + } } diff --git a/test/tests.zig b/test/tests.zig index 7ef5a33a03..45cba01c7f 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -1176,6 +1176,100 @@ pub fn addCliTests(b: *std.Build) *Step { step.dependOn(&cleanup.step); } + { + // Test `zig build -fallow-deprecated`. + + const deprecated_check: std.Build.Step.Run.StdIo.Check = .{ + .expect_stderr_match = "found deprecated code", + }; + + const tmp_path = b.makeTempPath(); + + // create custom main.zig file containing a deprecated decl + { + const new_main_src = + \\const bad = @deprecated(42); + \\ + \\pub fn main() u8 { + \\ return bad; + \\} + \\ + \\test { + \\ if (bad != 42) return error.Bad; + \\} + ; + + var src_dir = std.fs.cwd().makeOpenPath(b.pathJoin(&.{ tmp_path, "src" }), .{}) catch unreachable; + defer src_dir.close(); + + var main = src_dir.createFile("main.zig", .{}) catch unreachable; + defer main.close(); + + main.writeAll(new_main_src) catch unreachable; + } + + const init_exe = b.addSystemCommand(&.{ b.graph.zig_exe, "init" }); + init_exe.setCwd(.{ .cwd_relative = tmp_path }); + init_exe.setName("zig init"); + init_exe.expectStdOutEqual(""); + init_exe.expectStdErrEqual("info: created build.zig\n" ++ + "info: created build.zig.zon\n" ++ + "info: preserving already existing file: src" ++ s ++ "main.zig\n" ++ + "info: created src" ++ s ++ "root.zig\n"); + + const run_test_bad = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "test", "--color", "off" }); + run_test_bad.setCwd(.{ .cwd_relative = tmp_path }); + run_test_bad.setName("zig build test"); + run_test_bad.expectExitCode(1); + run_test_bad.expectStdOutEqual(""); + run_test_bad.addCheck(deprecated_check); + run_test_bad.step.dependOn(&init_exe.step); + + const run_test = b.addSystemCommand(&.{ + b.graph.zig_exe, + "build", + "test", + "--color", + "off", + "-fallow-deprecated", + }); + run_test.setCwd(.{ .cwd_relative = tmp_path }); + run_test.setName("zig build test"); + run_test.expectExitCode(0); + run_test.expectStdOutEqual(""); + run_test.expectStdErrEqual(""); + run_test.step.dependOn(&init_exe.step); + + const run_build_bad = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "--color", "off" }); + run_build_bad.setCwd(.{ .cwd_relative = tmp_path }); + run_build_bad.setName("zig build test"); + run_build_bad.expectExitCode(1); + run_build_bad.expectStdOutEqual(""); + run_build_bad.addCheck(deprecated_check); + run_build_bad.step.dependOn(&init_exe.step); + + const run_build = b.addSystemCommand(&.{ + b.graph.zig_exe, + "build", + "--color", + "off", + "-fallow-deprecated", + }); + run_build.setCwd(.{ .cwd_relative = tmp_path }); + run_build.setName("zig build test"); + run_build.expectExitCode(0); + run_build.expectStdOutEqual(""); + run_build.expectStdErrEqual(""); + run_build.step.dependOn(&init_exe.step); + + const cleanup = b.addRemoveDirTree(.{ .cwd_relative = tmp_path }); + cleanup.step.dependOn(&run_test.step); + cleanup.step.dependOn(&run_test_bad.step); + cleanup.step.dependOn(&run_build.step); + cleanup.step.dependOn(&run_build_bad.step); + + step.dependOn(&cleanup.step); + } // Test Godbolt API if (builtin.os.tag == .linux and builtin.cpu.arch == .x86_64) { const tmp_path = b.makeTempPath(); |
