Skip to content

Commit 21afa13

Browse files
committed
test: support multiple zig_modules to better organize tests
1 parent 9cd4ce7 commit 21afa13

20 files changed

+91
-53
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ zig-out/
44
*.o
55

66
node_modules
7-
8-
tests/node_modules/node-api-test-module/zig-module.node
7+
tests/zig_modules/*.node

build.zig

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,60 @@ pub fn build(b: *std.Build) void {
1717

1818
node_api.addSystemIncludePath(node_api_headers.path("include"));
1919

20-
const sample_addon = b.addLibrary(.{
21-
.use_llvm = true,
22-
.linkage = .dynamic,
23-
.name = "sample-addon",
24-
.root_module = b.createModule(.{
25-
.root_source_file = b.path("tests/zig/test-module.zig"),
26-
.optimize = optimize,
27-
.target = target,
28-
}),
29-
});
30-
sample_addon.root_module.addImport("node-api", node_api);
20+
const install_step = b.getInstallStep();
3121

32-
// important
33-
sample_addon.linker_allow_shlib_undefined = true;
34-
sample_addon.linkLibC();
22+
// zig build (test modules)
23+
{
24+
var dir = std.fs.cwd().openDir("tests/zig_modules", .{ .iterate = true }) catch unreachable;
25+
defer dir.close();
3526

36-
const install_step = b.getInstallStep();
27+
var it = dir.iterate();
28+
while (it.next() catch unreachable) |entry| {
29+
switch (entry.kind) {
30+
.directory => {
31+
std.log.info("tests/zig_modules/{s}/src/root.zig", .{entry.name});
32+
33+
const mod = b.addLibrary(.{
34+
// imprtant, works without on MacOS, but not on Linux
35+
.use_llvm = true,
36+
.linkage = .dynamic,
37+
.name = entry.name,
38+
.root_module = b.createModule(.{
39+
.root_source_file = b.path(std.fmt.allocPrint(b.allocator, "tests/zig_modules/{s}/src/root.zig", .{entry.name}) catch unreachable),
40+
.optimize = optimize,
41+
.target = target,
42+
}),
43+
});
44+
mod.root_module.addImport("node-api", node_api);
3745

38-
install_step.dependOn(
39-
&b.addInstallArtifact(
40-
sample_addon,
41-
.{
42-
// custom dir is relative to ./zig-out
43-
.dest_dir = .{ .override = .{ .custom = "../tests/node_modules/node-api-test-module" } },
44-
.dest_sub_path = "zig-module.node",
45-
},
46-
).step,
47-
);
46+
// important
47+
mod.linker_allow_shlib_undefined = true;
48+
mod.linkLibC();
49+
50+
const sub_path = std.fmt.allocPrint(b.allocator, "{s}.node", .{entry.name}) catch |err| {
51+
std.log.info("{s}", .{@errorName(err)});
52+
unreachable;
53+
};
54+
std.log.info("sub path: {s}", .{sub_path});
55+
56+
install_step.dependOn(
57+
&b.addInstallArtifact(
58+
mod,
59+
.{
60+
// custom dir is relative to ./zig-out
61+
.dest_dir = .{ .override = .{
62+
.custom = "../tests/zig_modules",
63+
} },
64+
65+
.dest_sub_path = sub_path,
66+
},
67+
).step,
68+
);
69+
},
70+
else => {},
71+
}
72+
}
73+
}
4874

4975
// `zig build test`
5076
{

tests/callbacks.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import addon from "node-api-test-module";
1+
import requireTestModule from "./zig_modules";
2+
const addon = requireTestModule("test-module");
3+
24
import { describe, it, expect } from "bun:test";
35

6+
47
describe("callback parameters", () => {
58
const capturedValue = 3;
69

tests/classes.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import addon from "node-api-test-module";
21
import { describe, it, expect } from "bun:test";
32

3+
import requireTestModule from "./zig_modules";
4+
const addon = requireTestModule("test-module");
5+
46
describe("defineClass", () => {
57
it("should define class as (constructor) function", () => {
68
expect(addon.TestClass).toBeFunction();

tests/errors.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import addon from "node-api-test-module";
21
import { describe, it, expect } from "bun:test";
32

3+
import requireTestModule from "./zig_modules";
4+
const addon = requireTestModule("test-module");
5+
46
describe("Import Node-API module", () => {
57
it("should return module value", () => {
68
expect(addon).toBeDefined();

tests/functions.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import addon from "node-api-test-module";
21
import { describe, it, expect } from "bun:test";
32

3+
import requireTestModule from "./zig_modules";
4+
const addon = requireTestModule("test-module");
5+
46
describe("functions", () => {
57
it("should be defined as function", () => {
68
expect(addon.functions.fnWithSerializedParams).toBeFunction();

tests/import.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import addon from "node-api-test-module";
21
import { describe, it, expect } from "bun:test";
32

3+
import requireTestModule from "./zig_modules";
4+
const addon = requireTestModule("test-module");
5+
46
describe("Import Zig Node-API module", () => {
57
it("should return Zig struct instance with all exported members", () => {
68
expect(addon).toBeDefined();

tests/index.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/memory.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import addon from "node-api-test-module";
21
import { describe, it, expect } from "bun:test";
32
import { gc, sleep } from "bun";
43

4+
import requireTestModule from "./zig_modules";
5+
const addon = requireTestModule("test-module");
6+
57
// this will be GC'ed (and finalized)
68
new addon.TestClass(12);
79

tests/node-array.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import addon from "node-api-test-module";
21
import { describe, it, expect } from "bun:test";
32

3+
import requireTestModule from "./zig_modules";
4+
const addon = requireTestModule("test-module");
5+
46
describe("NodeArray", () => {
57
describe("len", () => {
68
it("should return zero for empty array ", () => {
@@ -20,7 +22,7 @@ describe("NodeArray", () => {
2022
});
2123
});
2224

23-
describe("has", () => {
25+
describe("has", () => {
2426
it("should return true for existing element", () => {
2527
expect(addon.nodeArray.has([123, 456], 1)).toEqual(true);
2628
});

0 commit comments

Comments
 (0)