tests.zig 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 RunStep = std.Build.RunStep;
  9. const Step = Build.Step;
  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. // Test that `zig build -Dn=n -Dhealed test` selects the nth exercise.
  24. const case_step = createCase(b, "case-1");
  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. // Test that `zig build -Dn=n -Dhealed test` skips disabled esercises.
  45. const case_step = createCase(b, "case-2");
  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. // Don't add the cleanup step, since it may delete outdir while a test case
  62. // is running.
  63. //const cleanup = b.addRemoveDirTree(outdir);
  64. //step.dependOn(&cleanup.step);
  65. return step;
  66. }
  67. fn createCase(b: *Build, name: []const u8) *Step {
  68. const case_step = b.allocator.create(Step) catch @panic("OOM");
  69. case_step.* = Step.init(.{
  70. .id = .custom,
  71. .name = name,
  72. .owner = b,
  73. });
  74. return case_step;
  75. }
  76. // A step that will fail.
  77. const FailStep = struct {
  78. step: Step,
  79. error_msg: []const u8,
  80. pub fn create(owner: *Build, error_msg: []const u8) *FailStep {
  81. const self = owner.allocator.create(FailStep) catch @panic("OOM");
  82. self.* = .{
  83. .step = Step.init(.{
  84. .id = .custom,
  85. .name = "fail",
  86. .owner = owner,
  87. .makeFn = make,
  88. }),
  89. .error_msg = error_msg,
  90. };
  91. return self;
  92. }
  93. fn make(step: *Step, _: *std.Progress.Node) !void {
  94. const b = step.owner;
  95. const self = @fieldParentPtr(FailStep, "step", step);
  96. try step.result_error_msgs.append(b.allocator, self.error_msg);
  97. return error.MakeFailed;
  98. }
  99. };
  100. // A variant of `std.Build.Step.fail` that does not return an error so that it
  101. // can be used in the configuration phase. It returns a FailStep, so that the
  102. // error will be cleanly handled by the build runner.
  103. fn fail(step: *Step, comptime format: []const u8, args: anytype) *Step {
  104. const b = step.owner;
  105. const fail_step = FailStep.create(b, b.fmt(format, args));
  106. step.dependOn(&fail_step.step);
  107. return step;
  108. }
  109. // Heals all the exercises.
  110. fn heal(allocator: Allocator, exercises: []const Exercise, outdir: []const u8) !void {
  111. const join = fs.path.join;
  112. const exercises_path = "exercises";
  113. const patches_path = "patches/patches";
  114. for (exercises) |ex| {
  115. const name = ex.baseName();
  116. // Use the POSIX patch variant.
  117. const file = try join(allocator, &.{ exercises_path, ex.main_file });
  118. const patch = b: {
  119. const patch_name = try fmt.allocPrint(allocator, "{s}.patch", .{name});
  120. break :b try join(allocator, &.{ patches_path, patch_name });
  121. };
  122. const output = try join(allocator, &.{ outdir, ex.main_file });
  123. const argv = &.{ "patch", "-i", patch, "-o", output, file };
  124. var child = std.process.Child.init(argv, allocator);
  125. child.stdout_behavior = .Ignore; // the POSIX standard says that stdout is not used
  126. _ = try child.spawnAndWait();
  127. }
  128. }
  129. //
  130. // Missing functions from std.Build.RunStep
  131. //
  132. /// Adds a check for stderr match. Does not add any other checks.
  133. pub fn expectStdErrMatch(self: *RunStep, bytes: []const u8) void {
  134. const new_check: RunStep.StdIo.Check = .{
  135. .expect_stderr_match = self.step.owner.dupe(bytes),
  136. };
  137. self.addCheck(new_check);
  138. }
  139. /// Adds a check for stdout match as well as a check for exit code 0, if
  140. /// there is not already an expected termination check.
  141. pub fn expectStdOutMatch(self: *RunStep, bytes: []const u8) void {
  142. const new_check: RunStep.StdIo.Check = .{
  143. .expect_stdout_match = self.step.owner.dupe(bytes),
  144. };
  145. self.addCheck(new_check);
  146. if (!self.hasTermCheck()) {
  147. self.expectExitCode(0);
  148. }
  149. }