Skip to content

Commit 9486b0f

Browse files
test: run integration suites and check every example
1 parent 6c77f2d commit 9486b0f

9 files changed

Lines changed: 178 additions & 61 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,14 @@ zig build
757757
# Run self-tests
758758
zig build test
759759

760-
# Run examples
760+
# Run the basic and advanced examples, and compile-check every example
761761
zig build examples
762762
```
763763

764+
The async, snapshot, progress, and timeout examples are compile-checked rather
765+
than executed by this step because they intentionally demonstrate delays,
766+
generated files, interactive output, or failure handling.
767+
764768
## Requirements
765769

766770
- The exact Zig development build recorded in [`.zig-version`](.zig-version)

‎build.zig‎

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pub fn build(b: *std.Build) void {
1919
.target = target,
2020
.optimize = optimize,
2121
.link_libc = true,
22+
.imports = &.{
23+
.{ .name = "zig_test_framework", .module = lib_module },
24+
},
2225
}),
2326
});
2427

@@ -46,6 +49,9 @@ pub fn build(b: *std.Build) void {
4649
.target = target,
4750
.optimize = optimize,
4851
.link_libc = true,
52+
.imports = &.{
53+
.{ .name = "zig_test_framework", .module = lib_module },
54+
},
4955
}),
5056
});
5157
const run_test_runner_tests = b.addRunArtifact(test_runner_tests);
@@ -57,6 +63,9 @@ pub fn build(b: *std.Build) void {
5763
.target = target,
5864
.optimize = optimize,
5965
.link_libc = true,
66+
.imports = &.{
67+
.{ .name = "zig_test_framework", .module = lib_module },
68+
},
6069
}),
6170
});
6271
const run_assertions_tests = b.addRunArtifact(assertions_tests);
@@ -68,6 +77,9 @@ pub fn build(b: *std.Build) void {
6877
.target = target,
6978
.optimize = optimize,
7079
.link_libc = true,
80+
.imports = &.{
81+
.{ .name = "zig_test_framework", .module = lib_module },
82+
},
7183
}),
7284
});
7385
const run_suite_tests = b.addRunArtifact(suite_tests);
@@ -79,6 +91,9 @@ pub fn build(b: *std.Build) void {
7991
.target = target,
8092
.optimize = optimize,
8193
.link_libc = true,
94+
.imports = &.{
95+
.{ .name = "zig_test_framework", .module = lib_module },
96+
},
8297
}),
8398
});
8499
const run_matchers_tests = b.addRunArtifact(matchers_tests);
@@ -108,6 +123,9 @@ pub fn build(b: *std.Build) void {
108123
.target = target,
109124
.optimize = optimize,
110125
.link_libc = true,
126+
.imports = &.{
127+
.{ .name = "zig_test_framework", .module = lib_module },
128+
},
111129
}),
112130
});
113131
const run_reporter_tests = b.addRunArtifact(reporter_tests);
@@ -119,6 +137,9 @@ pub fn build(b: *std.Build) void {
119137
.target = target,
120138
.optimize = optimize,
121139
.link_libc = true,
140+
.imports = &.{
141+
.{ .name = "zig_test_framework", .module = lib_module },
142+
},
122143
}),
123144
});
124145
const run_cli_tests = b.addRunArtifact(cli_tests);
@@ -144,6 +165,9 @@ pub fn build(b: *std.Build) void {
144165
.target = target,
145166
.optimize = optimize,
146167
.link_libc = true,
168+
.imports = &.{
169+
.{ .name = "zig_test_framework", .module = lib_module },
170+
},
147171
}),
148172
});
149173
const run_mock_tests = b.addRunArtifact(mock_tests);
@@ -209,11 +233,14 @@ pub fn build(b: *std.Build) void {
209233
test_step.dependOn(&run_assertions_tests.step);
210234
test_step.dependOn(&run_suite_tests.step);
211235
test_step.dependOn(&run_matchers_tests.step);
212-
// Note: hooks_test is an executable, run with 'zig build test-hooks'
236+
test_step.dependOn(&run_hooks_tests.step);
213237
test_step.dependOn(&run_reporter_tests.step);
214238
test_step.dependOn(&run_cli_tests.step);
215239
test_step.dependOn(&run_filter_tests.step);
216240
test_step.dependOn(&run_mock_tests.step);
241+
test_step.dependOn(&run_comprehensive_mock_tests.step);
242+
test_step.dependOn(&run_snapshot_usage_tests.step);
243+
test_step.dependOn(&run_time_tests.step);
217244

218245
// Examples
219246
const basic_example = b.addExecutable(.{
@@ -245,7 +272,65 @@ pub fn build(b: *std.Build) void {
245272
const run_basic_example = b.addRunArtifact(basic_example);
246273
const run_advanced_example = b.addRunArtifact(advanced_example);
247274

248-
const examples_step = b.step("examples", "Run all examples");
275+
// These showcase intentionally slow, interactive, or failure-oriented
276+
// scenarios, so keep them compile-only while still checking every example.
277+
const async_examples = b.addExecutable(.{
278+
.name = "async_examples",
279+
.root_module = b.createModule(.{
280+
.root_source_file = b.path("examples/async_tests.zig"),
281+
.target = target,
282+
.optimize = optimize,
283+
.link_libc = true,
284+
.imports = &.{
285+
.{ .name = "zig_test", .module = lib_module },
286+
},
287+
}),
288+
});
289+
290+
const snapshot_examples = b.addExecutable(.{
291+
.name = "snapshot_examples",
292+
.root_module = b.createModule(.{
293+
.root_source_file = b.path("examples/snapshot_examples.zig"),
294+
.target = target,
295+
.optimize = optimize,
296+
.link_libc = true,
297+
.imports = &.{
298+
.{ .name = "zig_test", .module = lib_module },
299+
},
300+
}),
301+
});
302+
303+
const progress_examples = b.addExecutable(.{
304+
.name = "progress_examples",
305+
.root_module = b.createModule(.{
306+
.root_source_file = b.path("examples/progress_examples.zig"),
307+
.target = target,
308+
.optimize = optimize,
309+
.link_libc = true,
310+
.imports = &.{
311+
.{ .name = "zig_test", .module = lib_module },
312+
},
313+
}),
314+
});
315+
316+
const timeout_examples = b.addExecutable(.{
317+
.name = "timeout_examples",
318+
.root_module = b.createModule(.{
319+
.root_source_file = b.path("examples/timeout_examples.zig"),
320+
.target = target,
321+
.optimize = optimize,
322+
.link_libc = true,
323+
.imports = &.{
324+
.{ .name = "zig_test", .module = lib_module },
325+
},
326+
}),
327+
});
328+
329+
const examples_step = b.step("examples", "Run core examples and compile all examples");
249330
examples_step.dependOn(&run_basic_example.step);
250331
examples_step.dependOn(&run_advanced_example.step);
332+
examples_step.dependOn(&async_examples.step);
333+
examples_step.dependOn(&snapshot_examples.step);
334+
examples_step.dependOn(&progress_examples.step);
335+
examples_step.dependOn(&timeout_examples.step);
251336
}

‎examples/async_tests.zig‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn example_basic_async_test() !void {
1818
fn run(alloc: std.mem.Allocator) !void {
1919
_ = alloc;
2020
// Simulate async work
21-
std.Thread.sleep(100 * std.time.ns_per_ms);
21+
zig_test.compat.sleep(100 * std.time.ns_per_ms);
2222
std.debug.print("Async test 1 completed\n", .{});
2323
}
2424
}.run;
@@ -54,7 +54,7 @@ pub fn example_async_timeout() !void {
5454
const slowTest = struct {
5555
fn run(alloc: std.mem.Allocator) !void {
5656
_ = alloc;
57-
std.Thread.sleep(500 * std.time.ns_per_ms); // Takes too long
57+
zig_test.compat.sleep(500 * std.time.ns_per_ms); // Takes too long
5858
}
5959
}.run;
6060

@@ -87,7 +87,7 @@ pub fn example_concurrent_async() !void {
8787
const asyncTest = struct {
8888
fn run(alloc: std.mem.Allocator) !void {
8989
_ = alloc;
90-
std.Thread.sleep(50 * std.time.ns_per_ms);
90+
zig_test.compat.sleep(50 * std.time.ns_per_ms);
9191
}
9292
}.run;
9393

@@ -97,10 +97,10 @@ pub fn example_concurrent_async() !void {
9797
try executor.registerTest("concurrent_4", asyncTest);
9898
try executor.registerTest("concurrent_5", asyncTest);
9999

100-
const start = std.time.nanoTimestamp();
100+
const start = zig_test.compat.nanoTimestamp();
101101
const results = try executor.executeAll();
102102
defer allocator.free(results);
103-
const duration = std.time.nanoTimestamp() - start;
103+
const duration = zig_test.compat.nanoTimestamp() - start;
104104

105105
std.debug.print("Total duration: {d:.2}ms\n", .{
106106
@as(f64, @floatFromInt(duration)) / 1_000_000.0,
@@ -121,15 +121,15 @@ pub fn example_async_hooks() !void {
121121
fn run(alloc: std.mem.Allocator) !void {
122122
_ = alloc;
123123
std.debug.print("Before hook executed\n", .{});
124-
std.Thread.sleep(10 * std.time.ns_per_ms);
124+
zig_test.compat.sleep(10 * std.time.ns_per_ms);
125125
}
126126
}.run;
127127

128128
const afterHook = struct {
129129
fn run(alloc: std.mem.Allocator) !void {
130130
_ = alloc;
131131
std.debug.print("After hook executed\n", .{});
132-
std.Thread.sleep(10 * std.time.ns_per_ms);
132+
zig_test.compat.sleep(10 * std.time.ns_per_ms);
133133
}
134134
}.run;
135135

@@ -150,15 +150,15 @@ pub fn example_suite_async() !void {
150150
const asyncTest1 = struct {
151151
fn run(alloc: std.mem.Allocator) !void {
152152
_ = alloc;
153-
std.Thread.sleep(50 * std.time.ns_per_ms);
153+
zig_test.compat.sleep(50 * std.time.ns_per_ms);
154154
std.debug.print("Suite async test 1 passed\n", .{});
155155
}
156156
}.run;
157157

158158
const asyncTest2 = struct {
159159
fn run(alloc: std.mem.Allocator) !void {
160160
_ = alloc;
161-
std.Thread.sleep(50 * std.time.ns_per_ms);
161+
zig_test.compat.sleep(50 * std.time.ns_per_ms);
162162
std.debug.print("Suite async test 2 passed\n", .{});
163163
}
164164
}.run;
@@ -187,7 +187,7 @@ pub fn example_mixed_tests() !void {
187187
const asyncTest = struct {
188188
fn run(alloc: std.mem.Allocator) !void {
189189
_ = alloc;
190-
std.Thread.sleep(50 * std.time.ns_per_ms);
190+
zig_test.compat.sleep(50 * std.time.ns_per_ms);
191191
std.debug.print("Async test executed\n", .{});
192192
}
193193
}.run;
@@ -212,15 +212,15 @@ pub fn example_async_errors() !void {
212212
const passingTest = struct {
213213
fn run(alloc: std.mem.Allocator) !void {
214214
_ = alloc;
215-
std.Thread.sleep(10 * std.time.ns_per_ms);
215+
zig_test.compat.sleep(10 * std.time.ns_per_ms);
216216
}
217217
}.run;
218218

219219
// Test that fails
220220
const failingTest = struct {
221221
fn run(alloc: std.mem.Allocator) !void {
222222
_ = alloc;
223-
std.Thread.sleep(10 * std.time.ns_per_ms);
223+
zig_test.compat.sleep(10 * std.time.ns_per_ms);
224224
return error.AsyncTestFailed;
225225
}
226226
}.run;

0 commit comments

Comments
 (0)