tests.zig 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. const std = @import("std");
  2. const root = @import("../build.zig");
  3. const debug = std.debug;
  4. const fmt = std.fmt;
  5. const fs = std.fs;
  6. const Allocator = std.mem.Allocator;
  7. const Build = std.build;
  8. const Step = Build.Step;
  9. const RunStep = std.Build.RunStep;
  10. const Exercise = root.Exercise;
  11. pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
  12. const step = b.step("test-cli", "Test the command line interface");
  13. // We should use a temporary path, but it will make the implementation of
  14. // `build.zig` more complex.
  15. const outdir = "patches/healed";
  16. fs.cwd().makePath(outdir) catch |err| {
  17. return fail(step, "unable to make '{s}': {s}\n", .{ outdir, @errorName(err) });
  18. };
  19. heal(b.allocator, exercises, outdir) catch |err| {
  20. return fail(step, "unable to heal exercises: {s}\n", .{@errorName(err)});
  21. };
  22. {
  23. const case_step = createCase(b, "case-1");
  24. // Test that `zig build -Dn=n -Dhealed test` selects the nth exercise.
  25. var i: usize = 0;
  26. for (exercises[0 .. exercises.len - 1]) |ex| {
  27. i += 1;
  28. if (ex.skip) continue;
  29. const cmd = b.addSystemCommand(
  30. &.{ b.zig_exe, "build", b.fmt("-Dn={}", .{i}), "-Dhealed", "test" },
  31. );
  32. cmd.setName(b.fmt("zig build -D={} -Dhealed test", .{i}));
  33. cmd.expectExitCode(0);
  34. // Some exercise output has an extra space character.
  35. if (ex.check_stdout)
  36. expectStdOutMatch(cmd, ex.output)
  37. else
  38. expectStdErrMatch(cmd, ex.output);
  39. case_step.dependOn(&cmd.step);
  40. }
  41. step.dependOn(case_step);
  42. }
  43. {
  44. const case_step = createCase(b, "case-2");
  45. // Test that `zig build -Dn=n -Dhealed test` skips disabled esercises.
  46. var i: usize = 0;
  47. for (exercises[0 .. exercises.len - 1]) |ex| {
  48. i += 1;
  49. if (!ex.skip) continue;
  50. const cmd = b.addSystemCommand(
  51. &.{ b.zig_exe, "build", b.fmt("-Dn={}", .{i}), "-Dhealed", "test" },
  52. );
  53. cmd.setName(b.fmt("zig build -D={} -Dhealed test", .{i}));
  54. cmd.expectExitCode(0);
  55. cmd.expectStdOutEqual("");
  56. expectStdErrMatch(cmd, b.fmt("{s} skipped", .{ex.main_file}));
  57. case_step.dependOn(&cmd.step);
  58. }
  59. step.dependOn(case_step);
  60. }
  61. const cleanup = b.addRemoveDirTree(outdir);
  62. step.dependOn(&cleanup.step);
  63. return step;
  64. }
  65. fn createCase(b: *Build, name: []const u8) *Step {
  66. const case_step = b.allocator.create(Step) catch @panic("OOM");
  67. case_step.* = Step.init(.{
  68. .id = .custom,
  69. .name = name,
  70. .owner = b,
  71. });
  72. return case_step;
  73. }
  74. // A step that will fail.
  75. const FailStep = struct {
  76. step: Step,
  77. error_msg: []const u8,
  78. pub fn create(owner: *Build, error_msg: []const u8) *FailStep {
  79. const self = owner.allocator.create(FailStep) catch @panic("OOM");
  80. self.* = .{
  81. .step = Step.init(.{
  82. .id = .custom,
  83. .name = "fail",
  84. .owner = owner,
  85. .makeFn = make,
  86. }),
  87. .error_msg = error_msg,
  88. };
  89. return self;
  90. }
  91. fn make(step: *Step, _: *std.Progress.Node) !void {
  92. const b = step.owner;
  93. const self = @fieldParentPtr(FailStep, "step", step);
  94. try step.result_error_msgs.append(b.allocator, self.error_msg);
  95. return error.MakeFailed;
  96. }
  97. };
  98. // A variant of `std.Build.Step.fail` that does not return an error so that it
  99. // can be used in the configuration phase. It returns a FailStep, so that the
  100. // error will be cleanly handled by the build runner.
  101. fn fail(step: *Step, comptime format: []const u8, args: anytype) *Step {
  102. const b = step.owner;
  103. const fail_step = FailStep.create(b, b.fmt(format, args));
  104. step.dependOn(&fail_step.step);
  105. return step;
  106. }
  107. // Heals all the exercises.
  108. fn heal(allocator: Allocator, exercises: []const Exercise, outdir: []const u8) !void {
  109. const join = fs.path.join;
  110. const exercises_path = "exercises";
  111. const patches_path = "patches/patches";
  112. for (exercises) |ex| {
  113. const name = ex.baseName();
  114. // Use the POSIX patch variant.
  115. const file = try join(allocator, &.{ exercises_path, ex.main_file });
  116. const patch = b: {
  117. const patch_name = try fmt.allocPrint(allocator, "{s}.patch", .{name});
  118. break :b try join(allocator, &.{ patches_path, patch_name });
  119. };
  120. const output = try join(allocator, &.{ outdir, ex.main_file });
  121. const argv = &.{ "patch", "-i", patch, "-o", output, file };
  122. var child = std.process.Child.init(argv, allocator);
  123. child.stdout_behavior = .Ignore; // the POSIX standard says that stdout is not used
  124. _ = try child.spawnAndWait();
  125. }
  126. }
  127. //
  128. // Missing functions from std.Build.RunStep
  129. //
  130. /// Adds a check for stderr match. Does not add any other checks.
  131. pub fn expectStdErrMatch(self: *RunStep, bytes: []const u8) void {
  132. const new_check: RunStep.StdIo.Check = .{
  133. .expect_stderr_match = self.step.owner.dupe(bytes),
  134. };
  135. self.addCheck(new_check);
  136. }
  137. /// Adds a check for stdout match as well as a check for exit code 0, if
  138. /// there is not already an expected termination check.
  139. pub fn expectStdOutMatch(self: *RunStep, bytes: []const u8) void {
  140. const new_check: RunStep.StdIo.Check = .{
  141. .expect_stdout_match = self.step.owner.dupe(bytes),
  142. };
  143. self.addCheck(new_check);
  144. if (!self.hasTermCheck()) {
  145. self.expectExitCode(0);
  146. }
  147. }