diff --git a/build.zig b/build.zig index 3b06530..db5a15a 100644 --- a/build.zig +++ b/build.zig @@ -77,6 +77,11 @@ pub fn build(b: *std.Build) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); + const download_geoip = b.option(bool, "download-geoip", "Download GeoIP database for tests") orelse false; + + const test_options = b.addOptions(); + test_options.addOption(bool, "download_geoip", download_geoip); + const tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), @@ -88,6 +93,7 @@ pub fn build(b: *std.Build) void { tests.root_module.addAnonymousImport("airports.dat", .{ .root_source_file = openflights.path("data/airports.dat"), }); + tests.root_module.addOptions("build_options", test_options); tests.root_module.addIncludePath(maxminddb_upstream.path("include")); tests.root_module.addConfigHeader(maxminddb_config); tests.linkLibrary(maxminddb); diff --git a/src/location/GeoIp.zig b/src/location/GeoIp.zig index a5384ae..2d853f7 100644 --- a/src/location/GeoIp.zig +++ b/src/location/GeoIp.zig @@ -95,8 +95,15 @@ test "GeoIP init with invalid path fails" { } test "isUSIP detects US IPs" { - var geoip = GeoIP.init("./GeoLite2-City.mmdb") catch { - std.debug.print("Skipping test - GeoLite2-City.mmdb not found\n", .{}); + const build_options = @import("build_options"); + const db_path = "./GeoLite2-City.mmdb"; + + if (build_options.download_geoip) { + const GeoLite2 = @import("GeoLite2.zig"); + try GeoLite2.ensureDatabase(std.testing.allocator, db_path); + } + + var geoip = GeoIP.init(db_path) catch { return error.SkipZigTest; }; defer geoip.deinit();