@@ -6,20 +6,35 @@ const sinon = require('sinon');
66const fs = require ( 'fs' ) ;
77const child_process = require ( 'child_process' ) ;
88
9+ // Require the module once without clearing cache to avoid triggering
10+ // loadNativeAddon() repeatedly. The TestHelpers functions read process
11+ // state at call time, so they can be tested with platform/env changes
12+ // without re-requiring the module.
13+ // NOTE: The module may have been loaded by other tests before NODE_ENV was set,
14+ // so TestHelpers might not be present. We clear cache and re-require with
15+ // NODE_ENV='test' to ensure TestHelpers is exported.
16+ const nativeLoaderPath = require . resolve ( '../lib/native_loader.js' ) ;
17+ if (
18+ ! require . cache [ nativeLoaderPath ] ||
19+ ! require . cache [ nativeLoaderPath ] . exports . TestHelpers
20+ ) {
21+ delete require . cache [ nativeLoaderPath ] ;
22+ process . env . NODE_ENV = 'test' ;
23+ }
24+ const nativeLoader = require ( '../lib/native_loader.js' ) ;
25+
926describe ( 'NativeLoader testing' , function ( ) {
1027 const sandbox = sinon . createSandbox ( ) ;
1128 let originalPlatform ;
1229 let originalArch ;
1330 let originalEnv ;
31+ let loader ;
1432
1533 beforeEach ( function ( ) {
1634 originalPlatform = process . platform ;
1735 originalArch = process . arch ;
1836 originalEnv = { ...process . env } ;
19- process . env . NODE_ENV = 'test' ;
20-
21- // Clear cache to reload module
22- delete require . cache [ require . resolve ( '../lib/native_loader.js' ) ] ;
37+ loader = nativeLoader . TestHelpers ;
2338 } ) ;
2439
2540 afterEach ( function ( ) {
@@ -29,20 +44,14 @@ describe('NativeLoader testing', function () {
2944 process . env = originalEnv ;
3045 } ) ;
3146
32- function getLoader ( ) {
33- return require ( '../lib/native_loader.js' ) . TestHelpers ;
34- }
35-
3647 it ( 'customFallbackLoader returns null on non-linux' , function ( ) {
3748 Object . defineProperty ( process , 'platform' , { value : 'win32' } ) ;
38- const loader = getLoader ( ) ;
3949 assert . strictEqual ( loader . customFallbackLoader ( ) , null ) ;
4050 } ) ;
4151
4252 it ( 'customFallbackLoader returns null if env info missing' , function ( ) {
4353 Object . defineProperty ( process , 'platform' , { value : 'linux' } ) ;
4454 process . env . ROS_DISTRO = '' ;
45- const loader = getLoader ( ) ;
4655 assert . strictEqual ( loader . customFallbackLoader ( ) , null ) ;
4756 } ) ;
4857
@@ -52,8 +61,6 @@ describe('NativeLoader testing', function () {
5261 process . env . ROS_DISTRO = 'humble' ;
5362
5463 sandbox . stub ( fs , 'existsSync' ) . returns ( false ) ;
55-
56- const loader = getLoader ( ) ;
5764 assert . strictEqual ( loader . customFallbackLoader ( ) , null ) ;
5865 } ) ;
5966
@@ -64,8 +71,6 @@ describe('NativeLoader testing', function () {
6471
6572 // Stub fs.existsSync to return true
6673 const existsSync = sandbox . stub ( fs , 'existsSync' ) . returns ( true ) ;
67-
68- const loader = getLoader ( ) ;
6974 assert . strictEqual ( loader . customFallbackLoader ( ) , null ) ;
7075
7176 // Verify it checked for the file
@@ -79,17 +84,12 @@ describe('NativeLoader testing', function () {
7984 process . env . RCLNODEJS_FORCE_BUILD = '1' ;
8085 const execSync = sandbox . stub ( child_process , 'execSync' ) ;
8186
82- // We expect it to try loading bindings after build
83- // Since we don't mock bindings, it will load the real one (if present) or fail.
84- // If it loads real one, test passes.
85- // If it fails, loadNativeAddon throws. We might need to handle that.
86- // But usually in dev env, the binding exists.
87+ // Clear cache and re-require to trigger loadNativeAddon with RCLNODEJS_FORCE_BUILD.
88+ // execSync is stubbed so no real rebuild (which deletes generated/) occurs.
89+ delete require . cache [ require . resolve ( '../lib/native_loader.js' ) ] ;
8790
8891 try {
89- getLoader ( ) ;
90- // Wait, getLoader requires the file.
91- // The file calls loadNativeAddon() immediately.
92- // So valid test is just requiring the file.
92+ require ( '../lib/native_loader.js' ) ;
9393 } catch ( e ) {
9494 // Ignore if loading binding fails, as long as execSync was called
9595 }
0 commit comments