tests.zig 5.0 KB

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