@@ -19,34 +19,11 @@ const os = require('os');
1919const rclnodejs = require ( '../index.js' ) ;
2020const path = require ( 'path' ) ;
2121
22- function buildTestMessage ( ) {
23- // Build the custom_msg_test package synchronously before running the test
24- const customMsgTestPath = path . join ( __dirname , 'custom_msg_test' ) ;
25- const buildResult = require ( 'child_process' ) . spawnSync ( 'colcon' , [ 'build' ] , {
26- cwd : customMsgTestPath ,
27- stdio : 'inherit' ,
28- timeout : 60000 , // 60 second timeout
29- } ) ;
30-
31- if ( buildResult . error ) {
32- throw new Error (
33- `Failed to build custom_msg_test package: ${ buildResult . error . message } `
34- ) ;
35- }
36-
37- if ( buildResult . status !== 0 ) {
38- throw new Error ( `colcon build failed with exit code ${ buildResult . status } ` ) ;
39- }
40-
22+ function sourceSetupScript ( setupPath ) {
4123 // Source the local_setup.sh to get environment variables
42- const setupScriptPath = path . join (
43- customMsgTestPath ,
44- 'install' ,
45- 'local_setup.sh'
46- ) ;
4724 const sourceResult = require ( 'child_process' ) . spawnSync (
4825 'bash' ,
49- [ '-c' , `source ${ setupScriptPath } && env` ] ,
26+ [ '-c' , `source ${ setupPath } && env` ] ,
5027 {
5128 encoding : 'utf8' ,
5229 timeout : 10000 , // 10 second timeout
@@ -83,6 +60,34 @@ function buildTestMessage() {
8360 } ) ;
8461}
8562
63+ function buildTestMessage ( ) {
64+ // Build the custom_msg_test package synchronously before running the test
65+ const customMsgTestPath = path . join ( __dirname , 'custom_msg_test' ) ;
66+ const buildResult = require ( 'child_process' ) . spawnSync ( 'colcon' , [ 'build' ] , {
67+ cwd : customMsgTestPath ,
68+ stdio : 'inherit' ,
69+ timeout : 60000 , // 60 second timeout
70+ } ) ;
71+
72+ if ( buildResult . error ) {
73+ throw new Error (
74+ `Failed to build custom_msg_test package: ${ buildResult . error . message } `
75+ ) ;
76+ }
77+
78+ if ( buildResult . status !== 0 ) {
79+ throw new Error ( `colcon build failed with exit code ${ buildResult . status } ` ) ;
80+ }
81+
82+ // Source the local_setup.sh to get environment variables
83+ const setupScriptPath = path . join (
84+ customMsgTestPath ,
85+ 'install' ,
86+ 'local_setup.sh'
87+ ) ;
88+ sourceSetupScript ( setupScriptPath ) ;
89+ }
90+
8691describe ( 'ROSIDL Node.js message generator test suite' , function ( ) {
8792 this . timeout ( 60 * 1000 ) ;
8893
@@ -289,15 +294,18 @@ describe('ROSIDL Node.js message generator test suite', function () {
289294
290295 it ( 'Generate message at runtime' , function ( ) {
291296 const amentPrefixPathOriginal = process . env . AMENT_PREFIX_PATH ;
292- buildTestMessage ( ) ;
293-
294- assert . doesNotThrow ( ( ) => {
295- const Testing = rclnodejs . require ( 'custom_msg_test/msg/Testing' ) ;
296- const t = new Testing ( ) ;
297- assert . equal ( typeof t , 'object' ) ;
298- assert . equal ( typeof t . x , 'number' ) ;
299- assert . equal ( typeof t . data , 'string' ) ;
300- } , 'This function should not throw' ) ;
301- process . env . AMENT_PREFIX_PATH = amentPrefixPathOriginal ;
297+ try {
298+ buildTestMessage ( ) ;
299+
300+ assert . doesNotThrow ( ( ) => {
301+ const Testing = rclnodejs . require ( 'custom_msg_test/msg/Testing' ) ;
302+ const t = new Testing ( ) ;
303+ assert . equal ( typeof t , 'object' ) ;
304+ assert . equal ( typeof t . x , 'number' ) ;
305+ assert . equal ( typeof t . data , 'string' ) ;
306+ } , 'This function should not throw' ) ;
307+ } finally {
308+ process . env . AMENT_PREFIX_PATH = amentPrefixPathOriginal ;
309+ }
302310 } ) ;
303311} ) ;
0 commit comments