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
13 changes: 11 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ jobs:
strategy:
fail-fast: false
matrix:
runner:
- macos-latest
- ubuntu-latest
- windows-latest
node-version:
- 20.x
- 22.x
- 24.x
- 25.x
runs-on: ubuntu-latest
runs-on: ${{ matrix.runner }}
steps:
- name: Harden Runner
uses: step-security/harden-runner@f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a # v2.13.1
Expand All @@ -32,11 +36,16 @@ jobs:
npm --version
- name: Install dependencies
run: npm ci
- name: Configure and build addons
run: |
npm run addons:configure
npm run addons:build
- name: npm test
if: matrix.node-version != '20.x'
run: npm run node:test
- name: npm test with amaro
if: matrix.node-version == '20.x'
run: npm run node:test
env:
NODE_OPTIONS: --import=${{ github.workspace }}/implementors/node/ts-strip.js
# Using file scheme prefix when to enable imports on Windows
NODE_OPTIONS: --import=file://${{ github.workspace }}/implementors/node/ts-strip.js
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
node_modules/

# Shallow checkout for agents to reference
/node/

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

set(NODE_API_HEADERS_DIR ${PROJECT_SOURCE_DIR}/node_modules/node-api-headers)

if(NOT EXISTS ${NODE_API_HEADERS_DIR})
message(FATAL_ERROR "Expected ${NODE_API_HEADERS_DIR} to exist")
endif()

if(MSVC)
set(NODE_API_LIB ${PROJECT_BINARY_DIR}/node.lib)
set(NODE_API_DEF ${NODE_API_HEADERS_DIR}/def/node_api.def)
execute_process(COMMAND ${CMAKE_AR} /def:${NODE_API_DEF} /out:${NODE_API_LIB} ${CMAKE_STATIC_LINKER_FLAGS})
endif()

function(add_node_api_cts_addon ADDON_NAME SRC)
add_library(${ADDON_NAME} SHARED ${SRC})
set_target_properties(${ADDON_NAME} PROPERTIES
PREFIX ""
SUFFIX ".node"
# Co-locate the output binary with the source file
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
# (for MSVC)
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}
)
if(APPLE)
set_target_properties(${ADDON_NAME} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
endif()
target_include_directories(${ADDON_NAME} PRIVATE ${NODE_API_HEADERS_DIR}/include)
target_link_libraries(${ADDON_NAME} PRIVATE ${NODE_API_LIB})
target_compile_features(${ADDON_NAME} PRIVATE cxx_std_17)
target_compile_definitions(${ADDON_NAME} PRIVATE ADDON_NAME=${ADDON_NAME})
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()
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 });
29 changes: 21 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,20 @@ 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,
[
// Using file scheme prefix when to enable imports on Windows
"--import",
"file://" + ASSERT_MODULE_PATH,
"--import",
"file://" + LOAD_ADDON_MODULE_PATH,
filePath,
],
{ cwd }
);

let stderrOutput = "";
child.stderr.setEncoding("utf8");
Expand Down Expand Up @@ -80,8 +94,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