also looking at this
build: single-source the package and CLI version
#20
7 files
+40
-11
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -1,19 +1,37 @@ | ||
| 1 | 1 | const std = ; |
| 2 | const manifest_source = @embedFile("build.zig.zon"); | |
| 3 | const package_version = manifestVersion(manifest_source); | |
| 4 | ||
| 5 | fn manifestVersion(source: []const u8) []const u8 { | |
| 6 | const marker = ".version = \""; | |
| 7 | const marker_start = std.mem.indexOf(u8, source, marker) orelse | |
| 8 | @compileError("build.zig.zon must declare .version"); | |
| 9 | const start = marker_start + marker.len; | |
| 10 | const end_offset = std.mem.indexOfScalar(u8, source[start..], '"') orelse | |
| 11 | @compileError("build.zig.zon contains an unterminated .version"); | |
| 12 | return source[start .. start + end_offset]; | |
| 13 | } | |
| 2 | 14 | |
| 3 | 15 | pub fn build(b: *std.Build) void { |
| 4 | 16 | const target = b.standardTargetOptions(.{}); |
| 5 | 17 | const optimize = b.standardOptimizeOption(.{}); |
| 6 | 18 | |
| 19 | const build_options = b.addOptions(); | |
| 20 | build_options.addOption([]const u8, "version", package_version); | |
| 21 | ||
| 7 | 22 | // Create the main library module |
| 8 | 23 | const lib_module = b.addModule("zig_test_framework", .{ |
| 9 | 24 | .root_source_file = b.path("src/lib.zig"), |
| 10 | 25 | .target = target, |
| 11 | 26 | .link_libc = true, |
| 12 | 27 | }); |
| 28 | lib_module.addOptions("build_options", build_options); | |
| 13 | 29 | |
| 14 | 30 | // Create the test runner executable |
| 15 | 31 | const exe = b.addExecutable(.{ |
| 16 | 32 | .name = "zig-test", |
| 33 | .version = std.SemanticVersion.parse(package_version) catch | |
| 34 | @panic("build.zig.zon contains an invalid semantic version"), | |
| 17 | 35 | .root_module = b.createModule(.{ |
| 18 | 36 | .root_source_file = b.path("src/main.zig"), |
| 19 | 37 | .target = target, |
| @@ -258,6 +276,11 @@ pub fn build(b: *std.Build) void { | ||
| 258 | 276 | run_invalid_config.expectExitCode(2); |
| 259 | 277 | test_step.dependOn(&run_invalid_config.step); |
| 260 | 278 | |
| 279 | const run_version = b.addRunArtifact(exe); | |
| 280 | run_version.addArg("--version"); | |
| 281 | run_version.expectStdErrEqual(b.fmt("Zig Test Framework v{s}\n", .{package_version})); | |
| 282 | test_step.dependOn(&run_version.step); | |
| 283 | ||
| 261 | 284 | // Examples |
| 262 | 285 | const basic_example = b.addExecutable(.{ |
| 263 | 286 | .name = "basic_example", |
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | const std = ; |
| 2 | const build_options = ; | |
| 2 | 3 | const config_mod = ; |
| 3 | 4 | const test_runner = ; |
| 4 | 5 | |
| @@ -44,6 +45,8 @@ pub const CLIOptions = struct { | ||
| 44 | 45 | timeout: ?u64 = null, // Global timeout in milliseconds |
| 45 | 46 | }; |
| 46 | 47 | |
| 48 | pub const version = build_options.version; | |
| 49 | ||
| 47 | 50 | pub const CLIError = error{ |
| 48 | 51 | InvalidArgument, |
| 49 | 52 | MissingValue, |
| @@ -383,8 +386,7 @@ pub const CLI = struct { | ||
| 383 | 386 | /// Print version information |
| 384 | 387 | pub fn printVersion(self: Self) void { |
| 385 | 388 | _ = self; |
| 386 | std.debug.print("Zig Test Framework v2.5.0\n", .{}); | |
| 387 | std.debug.print("Features: Parallel, Coverage, UI, Snapshots, Watch, Memory Profiling, TAP/JUnit, Progress Indicators, Timeout\n", .{}); | |
| 389 | std.debug.print("Zig Test Framework v{s}\n", .{version}); | |
| 388 | 390 | } |
| 389 | 391 | |
| 390 | 392 | /// Convert CLI options to RunnerOptions |
| @@ -423,6 +425,10 @@ test "CLI parse version" { | ||
| 423 | 425 | try std.testing.expect(cli.options.version); |
| 424 | 426 | } |
| 425 | 427 | |
| 428 | test "CLI build version is valid semantic version metadata" { | |
| 429 | _ = try std.SemanticVersion.parse(version); | |
| 430 | } | |
| 431 | ||
| 426 | 432 | test "CLI parse bail" { |
| 427 | 433 | var cli = CLI.init(std.testing.allocator); |
| 428 | 434 | const args = [_][]const u8{ "zig-test", "--bail" }; |