From 728402c64ff1a4ed1e16c4adac704193e973dc37 Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Mon, 8 May 2023 20:52:23 +0200 Subject: tests: remove the missing functions from RunStep Use directly the RunStep.addCheck method, instead. --- test/tests.zig | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) (limited to 'test') diff --git a/test/tests.zig b/test/tests.zig index a8a7c4c..702d313 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -84,10 +84,11 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { b.fmt("-Dn={}", .{n}), "test", }); + const expect = b.fmt("{s} skipped", .{ex.main_file}); cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{n})); cmd.expectExitCode(0); - cmd.expectStdOutEqual(""); - expectStdErrMatch(cmd, b.fmt("{s} skipped", .{ex.main_file})); + cmd.addCheck(.{ .expect_stdout_exact = "" }); + cmd.addCheck(.{ .expect_stderr_match = expect }); cmd.step.dependOn(&heal_step.step); @@ -172,9 +173,10 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { const case_step = createCase(b, "case-5"); const cmd = b.addSystemCommand(&.{ b.zig_exe, "build", "-Dn=1" }); + const expect = exercises[0].hint orelse ""; cmd.setName("zig build -Dn=1"); cmd.expectExitCode(1); - expectStdErrMatch(cmd, exercises[0].hint orelse ""); + cmd.addCheck(.{ .expect_stderr_match = expect }); cmd.step.dependOn(case_step); @@ -466,27 +468,3 @@ pub fn makeTempPath(b: *Build) ![]const u8 { return path; } - -// -// Missing functions from std.Build.RunStep -// - -/// Adds a check for stderr match. Does not add any other checks. -pub fn expectStdErrMatch(self: *RunStep, bytes: []const u8) void { - const new_check: RunStep.StdIo.Check = .{ - .expect_stderr_match = self.step.owner.dupe(bytes), - }; - self.addCheck(new_check); -} - -/// Adds a check for stdout match as well as a check for exit code 0, if -/// there is not already an expected termination check. -pub fn expectStdOutMatch(self: *RunStep, bytes: []const u8) void { - const new_check: RunStep.StdIo.Check = .{ - .expect_stdout_match = self.step.owner.dupe(bytes), - }; - self.addCheck(new_check); - if (!self.hasTermCheck()) { - self.expectExitCode(0); - } -} -- cgit v1.2.3 From e4e096c680fc2c8b577cd64bbffc26baf250551a Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Tue, 9 May 2023 11:00:16 +0200 Subject: build: make literal paths portable Use fs.path.sep_str instead of a slash, in literal paths. --- build.zig | 6 +++++- test/tests.zig | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/build.zig b/build.zig index 465a069..582350d 100644 --- a/build.zig +++ b/build.zig @@ -124,7 +124,11 @@ pub fn build(b: *Build) !void { const override_healed_path = b.option([]const u8, "healed-path", "Override healed path"); const exno: ?usize = b.option(usize, "n", "Select exercise"); - const healed_path = if (override_healed_path) |path| path else "patches/healed"; + const sep = std.fs.path.sep_str; + const healed_path = if (override_healed_path) |path| + path + else + "patches" ++ sep ++ "healed"; const work_path = if (healed) healed_path else "exercises"; const header_step = PrintStep.create(b, logo); diff --git a/test/tests.zig b/test/tests.zig index 702d313..2fd5ec4 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -435,10 +435,11 @@ const HealStep = struct { /// Heals all the exercises. fn heal(allocator: Allocator, exercises: []const Exercise, work_path: []const u8) !void { + const sep = std.fs.path.sep_str; const join = fs.path.join; const exercises_path = "exercises"; - const patches_path = "patches/patches"; + const patches_path = "patches" ++ sep ++ "patches"; for (exercises) |ex| { const name = ex.name(); -- cgit v1.2.3 From 7aa0737929a5de8420e4a90654ed49309add1ec2 Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Tue, 9 May 2023 17:15:22 +0200 Subject: Restore unit tests Commit dbd42bb (Cleaning up zig build output) broke the unit test. Always use exit code 2, instead of 0. This is the exit code used by the build runner to notify the compiler to not report any further diagnostics. Move the Ziglings logo from the `build` function scope to the global scope, and make it public so that tests.zig can use it to find the number of lines to skip, instead of using an hard coded value. Fixes #295 --- build.zig | 40 ++++++++++++++++++++-------------------- test/tests.zig | 5 +++-- 2 files changed, 23 insertions(+), 22 deletions(-) (limited to 'test') diff --git a/build.zig b/build.zig index 582350d..c762457 100644 --- a/build.zig +++ b/build.zig @@ -74,9 +74,22 @@ pub const Exercise = struct { } }; +pub const logo = + \\ _ _ _ + \\ ___(_) __ _| (_)_ __ __ _ ___ + \\ |_ | |/ _' | | | '_ \ / _' / __| + \\ / /| | (_| | | | | | | (_| \__ \ + \\ /___|_|\__, |_|_|_| |_|\__, |___/ + \\ |___/ |___/ + \\ + \\ "Look out! Broken programs below!" + \\ + \\ +; + pub fn build(b: *Build) !void { if (!compat.is_compatible) compat.die(); - if (!validate_exercises()) std.os.exit(1); + if (!validate_exercises()) std.os.exit(2); use_color_escapes = false; if (std.io.getStdErr().supportsAnsiEscapeCodes()) { @@ -106,19 +119,6 @@ pub fn build(b: *Build) !void { reset_text = "\x1b[0m"; } - const logo = - \\ _ _ _ - \\ ___(_) __ _| (_)_ __ __ _ ___ - \\ |_ | |/ _' | | | '_ \ / _' / __| - \\ / /| | (_| | | | | | | (_| \__ \ - \\ /___|_|\__, |_|_|_| |_|\__, |___/ - \\ |___/ |___/ - \\ - \\ "Look out! Broken programs below!" - \\ - \\ - ; - const healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false; const override_healed_path = b.option([]const u8, "healed-path", "Override healed path"); @@ -136,7 +136,7 @@ pub fn build(b: *Build) !void { if (exno) |n| { if (n == 0 or n > exercises.len - 1) { print("unknown exercise number: {}\n", .{n}); - std.os.exit(1); + std.os.exit(2); } const ex = exercises[n - 1]; @@ -280,10 +280,10 @@ const ZiglingStep = struct { self.help(); - // NOTE: Returning 0 'success' status because the *exercise* failed, - // but Ziglings did not. Otherwise the learner will see this message: - // "error: the following build command failed with exit code 1:..." - std.os.exit(0); + // NOTE: Using exit code 2 will prevent the Zig compiler to print + // the message: + // "error: the following build command failed with exit code 1:..." + std.os.exit(2); }; self.run(exe_path, prog_node) catch { @@ -293,7 +293,7 @@ const ZiglingStep = struct { self.help(); // NOTE: See note above! - std.os.exit(0); + std.os.exit(2); }; } diff --git a/test/tests.zig b/test/tests.zig index 2fd5ec4..6eab08e 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -175,7 +175,7 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { const cmd = b.addSystemCommand(&.{ b.zig_exe, "build", "-Dn=1" }); const expect = exercises[0].hint orelse ""; cmd.setName("zig build -Dn=1"); - cmd.expectExitCode(1); + cmd.expectExitCode(2); cmd.addCheck(.{ .expect_stderr_match = expect }); cmd.step.dependOn(case_step); @@ -282,10 +282,11 @@ const CheckStep = struct { for (exercises) |ex| { if (ex.number() == 1 and self.has_logo) { // Skip the logo. + const nlines = mem.count(u8, root.logo, "\n"); var buf: [80]u8 = undefined; var lineno: usize = 0; - while (lineno < 8) : (lineno += 1) { + while (lineno < nlines) : (lineno += 1) { _ = try readLine(stderr, &buf); } } -- cgit v1.2.3