zargs/build.zig

205 lines
8.0 KiB
Zig

const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Single zargs module - all source files are in the same module
const zargs_mod = b.addModule("zargs", .{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// Test step - just run tests on main module
const test_step = b.step("test", "Run unit tests");
const tests = b.addTest(.{
.root_module = zargs_mod,
});
test_step.dependOn(&b.addRunArtifact(tests).step);
// Example executables
const example_step = b.step("examples", "Build example programs");
// Multi-module example
const multi_module_mod = b.createModule(.{
.root_source_file = b.path("examples/multi_module.zig"),
.target = target,
.optimize = optimize,
});
multi_module_mod.addImport("zargs", zargs_mod);
const multi_module = b.addExecutable(.{
.name = "multi_module",
.root_module = multi_module_mod,
});
const install_multi_module = b.addInstallArtifact(multi_module, .{});
example_step.dependOn(&install_multi_module.step);
const run_multi_module = b.addRunArtifact(multi_module);
run_multi_module.step.dependOn(&install_multi_module.step);
if (b.args) |args| {
run_multi_module.addArgs(args);
}
const run_multi_module_step = b.step("run-multi-module", "Run the multi-module example");
run_multi_module_step.dependOn(&run_multi_module.step);
// File processor example
const file_processor_mod = b.createModule(.{
.root_source_file = b.path("examples/file_processor.zig"),
.target = target,
.optimize = optimize,
});
file_processor_mod.addImport("zargs", zargs_mod);
const file_processor = b.addExecutable(.{
.name = "file_processor",
.root_module = file_processor_mod,
});
const install_file_processor = b.addInstallArtifact(file_processor, .{});
example_step.dependOn(&install_file_processor.step);
const run_file_processor = b.addRunArtifact(file_processor);
run_file_processor.step.dependOn(&install_file_processor.step);
if (b.args) |args| {
run_file_processor.addArgs(args);
}
const run_file_processor_step = b.step("run-file-processor", "Run the file processor example");
run_file_processor_step.dependOn(&run_file_processor.step);
// Integration tests for examples - test mixing short and long flags
const example_tests = b.step("test-examples", "Run integration tests on examples");
// NOTE: Examples with string arguments from command line have a memory issue with the registry
// So we test simple with non-string arguments only
// Simple example tests (testing optional short flags)
// File processor tests (demonstrating optional short flags)
// Test 6: File processor with available short flags
{
const test6 = b.addRunArtifact(file_processor);
test6.step.dependOn(&install_file_processor.step);
test6.addArgs(&.{ "-v", "-i", "input.txt", "-o", "output.txt", "--format", "json" });
test6.expectExitCode(0);
example_tests.dependOn(&test6.step);
}
// Test 7: File processor with long flags only
{
const test7 = b.addRunArtifact(file_processor);
test7.step.dependOn(&install_file_processor.step);
test7.addArgs(&.{ "--verbose", "--format", "xml", "--input", "data.xml", "--tags", "test" });
test7.expectExitCode(0);
example_tests.dependOn(&test7.step);
}
// Test 8: File processor with mixed short and long-only flags
{
const test8 = b.addRunArtifact(file_processor);
test8.step.dependOn(&install_file_processor.step);
test8.addArgs(&.{ "-v", "--format", "csv", "-i", "test.csv", "--output", "out.csv", "--max-size", "2048" });
test8.expectExitCode(0);
example_tests.dependOn(&test8.step);
}
// Test 9: File processor with long-only flags in different order
{
const test9 = b.addRunArtifact(file_processor);
test9.step.dependOn(&install_file_processor.step);
test9.addArgs(&.{ "--format", "json", "-i", "data.json", "-v", "--max-size", "2048", "--tags", "prod" });
test9.expectExitCode(0);
example_tests.dependOn(&test9.step);
}
// Test 10: File processor with all long flags
{
const test10 = b.addRunArtifact(file_processor);
test10.step.dependOn(&install_file_processor.step);
test10.addArgs(&.{ "--input", "input.xml", "--verbose", "--format", "xml", "--output", "output.xml", "--max-size", "512", "--tags", "staging" });
test10.expectExitCode(0);
example_tests.dependOn(&test10.step);
}
// Test 11: File processor with enum values (all three types)
{
const test11a = b.addRunArtifact(file_processor);
test11a.step.dependOn(&install_file_processor.step);
test11a.addArgs(&.{ "--format", "json", "--tags", "test" });
test11a.expectExitCode(0);
example_tests.dependOn(&test11a.step);
const test11b = b.addRunArtifact(file_processor);
test11b.step.dependOn(&install_file_processor.step);
test11b.addArgs(&.{ "--format", "xml", "--tags", "test" });
test11b.expectExitCode(0);
example_tests.dependOn(&test11b.step);
const test11c = b.addRunArtifact(file_processor);
test11c.step.dependOn(&install_file_processor.step);
test11c.addArgs(&.{ "--format", "csv", "--tags", "test" });
test11c.expectExitCode(0);
example_tests.dependOn(&test11c.step);
}
// Test 12: File processor with comma-separated list arguments
{
const test12 = b.addRunArtifact(file_processor);
test12.step.dependOn(&install_file_processor.step);
test12.addArgs(&.{ "--format", "json", "--tags", "prod,staging,dev", "-v" });
test12.expectExitCode(0);
example_tests.dependOn(&test12.step);
}
// Test 13: File processor with repeated list arguments
{
const test13 = b.addRunArtifact(file_processor);
test13.step.dependOn(&install_file_processor.step);
test13.addArgs(&.{ "--tags", "tag1", "--tags", "tag2", "--tags", "tag3" });
test13.expectExitCode(0);
example_tests.dependOn(&test13.step);
}
// Test 14: File processor with mix of repeated and comma-separated lists
{
const test14 = b.addRunArtifact(file_processor);
test14.step.dependOn(&install_file_processor.step);
test14.addArgs(&.{ "--tags", "a,b", "--tags", "c", "--tags", "d,e,f" });
test14.expectExitCode(0);
example_tests.dependOn(&test14.step);
}
// Test 15: File processor with all argument types in random order
{
const test15 = b.addRunArtifact(file_processor);
test15.step.dependOn(&install_file_processor.step);
test15.addArgs(&.{ "--max-size", "1024", "--tags", "prod", "-v", "--input", "file.json", "--format", "json", "--output", "out.json" });
test15.expectExitCode(0);
example_tests.dependOn(&test15.step);
}
// Test 16: File processor help with short form
{
const test16 = b.addRunArtifact(file_processor);
test16.step.dependOn(&install_file_processor.step);
test16.addArgs(&.{"-h"});
test16.expectExitCode(0);
example_tests.dependOn(&test16.step);
}
// Test 17: File processor help with long form
{
const test17 = b.addRunArtifact(file_processor);
test17.step.dependOn(&install_file_processor.step);
test17.addArgs(&.{"--help"});
test17.expectExitCode(0);
example_tests.dependOn(&test17.step);
}
// Test 18: File processor help mixed with other arguments (help should take precedence)
{
const test18 = b.addRunArtifact(file_processor);
test18.step.dependOn(&install_file_processor.step);
test18.addArgs(&.{ "-v", "--help", "-f", "json" });
test18.expectExitCode(0);
example_tests.dependOn(&test18.step);
}
}