Skip to content

Commit

Permalink
test-debug-format-stack-traces: test against multiple target OSes
Browse files Browse the repository at this point in the history
  • Loading branch information
leroycep committed Dec 9, 2024
1 parent b30f554 commit 225f346
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ pub fn build(b: *std.Build) !void {
}));
test_step.dependOn(tests.addLinkTests(b, enable_macos_sdk, enable_ios_sdk, enable_symlinks_windows));
test_step.dependOn(tests.addStackTraceTests(b, test_filters, optimization_modes));
test_step.dependOn(tests.addDebugFormatStackTraceTests(b, optimization_modes));
test_step.dependOn(tests.addDebugFormatStackTraceTests(b, optimization_modes, skip_non_native));
test_step.dependOn(tests.addCliTests(b));
test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filters, optimization_modes));
if (tests.addDebuggerTests(b, .{
Expand Down
30 changes: 19 additions & 11 deletions test/src/DebugFormatStackTrace.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
b: *std.Build,
step: *Step,
test_index: usize,
optimize_modes: []const OptimizeMode,
targets: []const Target,
check_exe: *std.Build.Step.Compile,

pub const Target = struct {
target: std.Target.Query,
optimize_mode: OptimizeMode,
};

const Config = struct {
name: []const u8,
source: []const u8,
Expand Down Expand Up @@ -33,24 +38,28 @@ fn addExpect(
debug_format: std.builtin.DebugFormat,
mode_config: Config.PerFormat,
) void {
for (mode_config.exclude_os) |tag| if (tag == builtin.os.tag) return;

const b = this.b;
const write_files = b.addWriteFiles();
const source_zig = write_files.add("source.zig", source);

for (this.optimize_modes) |mode| {
if (mem.indexOfScalar(std.builtin.OptimizeMode, mode_config.exclude_optimize_mode, mode)) |_| continue;
add_target_loop: for (this.targets) |target| {
if (mem.indexOfScalar(std.builtin.OptimizeMode, mode_config.exclude_optimize_mode, target.optimize_mode)) |_| continue :add_target_loop;

const annotated_case_name = fmt.allocPrint(b.allocator, "check {s} ({s},{s})", .{
name, @tagName(debug_format), @tagName(mode),
const resolved_target = b.resolveTargetQuery(target.target);
for (mode_config.exclude_os) |tag| if (tag == resolved_target.result.os.tag) continue :add_target_loop;

const annotated_case_name = fmt.allocPrint(b.allocator, "check {s}-{s}-{s}-{s}", .{
name,
@tagName(debug_format),
@tagName(target.optimize_mode),
resolved_target.result.linuxTriple(b.allocator) catch @panic("OOM"),
}) catch @panic("OOM");

const exe = b.addExecutable(.{
.name = "test",
.root_source_file = source_zig,
.target = b.graph.host,
.optimize = mode,
.target = resolved_target,
.optimize = target.optimize_mode,
.debuginfo = debug_format,
});

Expand All @@ -60,7 +69,7 @@ fn addExpect(

// make sure to add term check fist, as `expectStdOutEqual` will detect no expectation for term and make it check for exit code 0
if (mode_config.expect_panic) {
switch (builtin.os.tag) {
switch (resolved_target.result.os.tag) {
// Expect exit code 3 on abort: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/abort?view=msvc-170
.windows => run.addCheck(.{ .expect_term = .{ .Exited = 3 } }),
else => run.addCheck(.{ .expect_term = .{ .Signal = 6 } }),
Expand All @@ -81,7 +90,6 @@ fn addExpect(
}

const std = @import("std");
const builtin = @import("builtin");
const OptimizeMode = std.builtin.OptimizeMode;
const Step = std.Build.Step;
const fmt = std.fmt;
Expand Down
22 changes: 21 additions & 1 deletion test/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ pub fn addStackTraceTests(
pub fn addDebugFormatStackTraceTests(
b: *std.Build,
optimize_modes: []const OptimizeMode,
skip_non_native: bool,
) *Step {
const check_exe = b.addExecutable(.{
.name = "check-debug-format-stack-trace",
Expand All @@ -1004,13 +1005,32 @@ pub fn addDebugFormatStackTraceTests(
.optimize = .Debug,
});

const targets = blk: {
var targets = std.ArrayListUnmanaged(DebugFormatStackTraceContext.Target){};
for (optimize_modes) |optimize_mode| {
targets.append(b.allocator, .{
.optimize_mode = optimize_mode,
.target = .{},
}) catch @panic("OOM");

if (skip_non_native) continue;

targets.appendSlice(b.allocator, &.{
.{ .optimize_mode = optimize_mode, .target = .{ .os_tag = .linux } },
.{ .optimize_mode = optimize_mode, .target = .{ .os_tag = .windows } },
.{ .optimize_mode = optimize_mode, .target = .{ .os_tag = .macos } },
}) catch @panic("OOM");
}
break :blk targets.toOwnedSlice(b.allocator) catch @panic("OOM");
};

const cases = b.allocator.create(DebugFormatStackTraceContext) catch @panic("OOM");
cases.* = .{
.b = b,
.step = b.step("test-debug-format-stack-traces", "Run the debug format stack trace tests"),
.test_index = 0,
.check_exe = check_exe,
.optimize_modes = optimize_modes,
.targets = targets,
};

debug_format_stack_traces.addCases(cases);
Expand Down

0 comments on commit 225f346

Please sign in to comment.