|
17 | 17 | const assert = require('assert'); |
18 | 18 | const os = require('os'); |
19 | 19 | const rclnodejs = require('../index.js'); |
| 20 | +const path = require('path'); |
| 21 | + |
| 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 | + |
| 41 | + // Source the local_setup.sh to get environment variables |
| 42 | + const setupScriptPath = path.join( |
| 43 | + customMsgTestPath, |
| 44 | + 'install', |
| 45 | + 'local_setup.sh' |
| 46 | + ); |
| 47 | + const sourceResult = require('child_process').spawnSync( |
| 48 | + 'bash', |
| 49 | + ['-c', `source ${setupScriptPath} && env`], |
| 50 | + { |
| 51 | + encoding: 'utf8', |
| 52 | + timeout: 10000, // 10 second timeout |
| 53 | + } |
| 54 | + ); |
| 55 | + |
| 56 | + if (sourceResult.error) { |
| 57 | + throw new Error( |
| 58 | + `Failed to source setup script: ${sourceResult.error.message}` |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + if (sourceResult.status !== 0) { |
| 63 | + throw new Error( |
| 64 | + `Failed to source setup script with exit code ${sourceResult.status}` |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + // Parse and apply environment variables to current process |
| 69 | + const envOutput = sourceResult.stdout; |
| 70 | + const envLines = envOutput.split('\n'); |
| 71 | + |
| 72 | + envLines.forEach((line) => { |
| 73 | + const equalIndex = line.indexOf('='); |
| 74 | + if (equalIndex > 0) { |
| 75 | + const key = line.substring(0, equalIndex); |
| 76 | + const value = line.substring(equalIndex + 1); |
| 77 | + |
| 78 | + // Only update AMENT_PREFIX_PATH from the subprocess |
| 79 | + if (key === 'AMENT_PREFIX_PATH') { |
| 80 | + process.env[key] = value; |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | +} |
20 | 85 |
|
21 | 86 | describe('ROSIDL Node.js message generator test suite', function () { |
22 | | - before(function () { |
23 | | - this.timeout(60 * 1000); |
24 | | - return rclnodejs.init(); |
| 87 | + this.timeout(60 * 1000); |
| 88 | + |
| 89 | + before(async function () { |
| 90 | + await rclnodejs.init(); |
25 | 91 | }); |
26 | 92 |
|
27 | 93 | after(function () { |
@@ -220,4 +286,18 @@ describe('ROSIDL Node.js message generator test suite', function () { |
220 | 286 | assert.equal(array.size, 6); |
221 | 287 | assert.equal(array.capacity, 6); |
222 | 288 | }); |
| 289 | + |
| 290 | + it('Generate message at runtime', function () { |
| 291 | + 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; |
| 302 | + }); |
223 | 303 | }); |
0 commit comments