Good morning, nice zig community
The Context: I am trying to organize testing of a zig project so that if the zig build test is run from the root directory then all the tests in all the subprojects are run; but if I execute the zig build test from within a subproject then only the subprojectβs tests are run.
Here is the tree of the project:
.
βββ build.zig
βββ build.zig.zon
βββ LICENSE
βββ src
β βββ hdr
β βββ build.zig
β βββ build.zig.zon
β βββ hdr.md
β βββ src
β βββ domain
β β βββ Header.zig
β β βββ processors
β β β βββ errors
β β β β βββ BufferError.zig
β β β β βββ TokenError.zig
β β β βββ processFILE.zig
β β β βββ ProcessFn.zig
β β βββ Token.zig
β βββ hdr_parser.zig
β βββ hdr.zig
βββ utils
βββ build.zig.zon
βββ test_utils.zig
The utils/test_utils.zig contains a addTests function:
pub fn addTests(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, test_step: *std.Build.Step, src_dir: []const u8) !void {
var dir = try std.fs.cwd().openDir(src_dir, .{ .iterate = true });
defer dir.close();
var walker = try dir.walk(b.allocator);
defer walker.deinit();
while (try walker.next()) |entry| {
if (isTestableZigFile(entry)) {
//
const test_artifact = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path(b.fmt("{s}/{s}", .{ src_dir, entry.path })),
.target = target,
.optimize = optimize,
}),
});
// const run_test_artifact = b.addRunArtifact(test_artifact);
test_step.dependOn(&test_artifact.step);
}
}
}
I would like to use this function in the src/hdr subproject.
The Problem: if I do this:
const addTests = @import("../../utils/test_utils.zig").addTests;
from the src/hdr/build.zig I will get the following error:
error: import of file outside module path: '../../utils/test_utils.zig'
I am a bit lost figuring out ways of importing the addTests
function into the src/hdr/build.zig together with ChatGPT. sorry, doing baby steps in zig, need a source of ideas.
Please show me a direction.
best regards,
Dmitry