Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:
npm --version
- name: Install dependencies
run: npm ci
- name: Build addons
run: npm run build-addons
- name: npm test
if: matrix.node-version != '20.x'
run: npm run node:test
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
node_modules/

# Shallow checkout for agents to reference
/node/

# Artifacts from building addons
/build/
/tests/**/*.node
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.15...3.31)
project(node-api-cts)

function(add_node_api_cts_addon ADDON_NAME SRC)
add_library(${ADDON_NAME} SHARED ${SRC} ${CMAKE_JS_SRC})
set_target_properties(${ADDON_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_include_directories(${ADDON_NAME} PRIVATE ${CMAKE_JS_INC})
target_link_libraries(${ADDON_NAME} PRIVATE ${CMAKE_JS_LIB})
target_compile_features(${ADDON_NAME} PRIVATE cxx_std_17)
target_compile_definitions(${ADDON_NAME} PRIVATE ADDON_NAME=${ADDON_NAME})
set_target_properties(${ADDON_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
endfunction()

file(GLOB_RECURSE cmake_dirs RELATIVE ${CMAKE_SOURCE_DIR} tests/*/CMakeLists.txt)

foreach(cmake_file ${cmake_dirs})
get_filename_component(subdir ${cmake_file} DIRECTORY)
add_subdirectory(${subdir})
endforeach()

if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
# Generate node.lib
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
endif()
17 changes: 17 additions & 0 deletions implementors/node/load-addon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import assert from "node:assert/strict";
import { dlopen } from "node:process";
import { constants } from "node:os";
import path from "node:path";
import fs from "node:fs";

const loadAddon = (addonFileName) => {
assert(typeof addonFileName === "string", "Expected a string as addon filename");
assert(!addonFileName.endsWith(".node"), "Expected addon filename without the .node extension");
const addonPath = path.join(process.cwd(), addonFileName + ".node");
assert(fs.existsSync(addonPath), `Expected ${addonPath} to exist - did you build the addons?`);
const addon = { exports: {} };
dlopen(addon, addonPath, constants.dlopen.RTLD_NOW);
return addon.exports;
};

Object.assign(globalThis, { loadAddon });
28 changes: 20 additions & 8 deletions implementors/node/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import { test, type TestContext } from "node:test";

const ROOT_PATH = path.resolve(import.meta.dirname, "..", "..");
const TESTS_ROOT_PATH = path.join(ROOT_PATH, "tests");

const ASSERT_MODULE_PATH = path.join(
ROOT_PATH,
"implementors",
"node",
"assert.js"
);
const LOAD_ADDON_MODULE_PATH = path.join(
ROOT_PATH,
"implementors",
"node",
"load-addon.js"
);

async function listDirectoryEntries(dir: string) {
const entries = await fs.readdir(dir, { withFileTypes: true });
Expand All @@ -31,13 +38,19 @@ async function listDirectoryEntries(dir: string) {
return { directories, files };
}

function runFileInSubprocess(filePath: string): Promise<void> {
function runFileInSubprocess(cwd: string, filePath: string): Promise<void> {
return new Promise((resolve, reject) => {
const child = spawn(process.execPath, [
"--import",
ASSERT_MODULE_PATH,
filePath,
]);
const child = spawn(
process.execPath,
[
"--import",
ASSERT_MODULE_PATH,
"--import",
LOAD_ADDON_MODULE_PATH,
filePath,
],
{ cwd }
);

let stderrOutput = "";
child.stderr.setEncoding("utf8");
Expand Down Expand Up @@ -80,8 +93,7 @@ async function populateSuite(
const { directories, files } = await listDirectoryEntries(dir);

for (const file of files) {
const filePath = path.join(dir, file);
await testContext.test(file, () => runFileInSubprocess(filePath));
await testContext.test(file, () => runFileInSubprocess(dir, file));
}

for (const directory of directories) {
Expand Down
Loading