@@ -3,87 +3,101 @@ const { spawn } = require('child_process');
33const path = require ( 'path' ) ;
44const fs = require ( 'fs' ) ;
55
6- const projectRoot = path . resolve ( __dirname , '..' ) ;
7- const jsWrapperPath = path . join ( projectRoot , 'wrappers' , 'kscript_js_wrapper.js' ) ;
8- const testScriptPath = path . join ( projectRoot , 'examples' , 'test_wrapper.kts' ) ;
6+ function runKscriptTest ( kscriptFile , expectedOutputSubstring ) {
7+ return new Promise ( ( resolve ) => { // Removed reject, will resolve with true/false
8+ console . log ( `Running Node.js kscript test: ${ kscriptFile } ...` ) ;
99
10- console . log ( `Project root: ${ projectRoot } ` ) ;
11- console . log ( `Executing: node ${ jsWrapperPath } ${ testScriptPath } ` ) ;
10+ const projectRoot = path . resolve ( __dirname , '..' ) ;
11+ const jarPath = path . join ( projectRoot , 'wrappers' , 'kscript.jar' ) ;
12+ const wrapperScriptPath = path . join ( projectRoot , 'wrappers' , 'kscript_js_wrapper.js' ) ;
13+ const kscriptFilePath = path . join ( projectRoot , 'examples' , kscriptFile ) ;
1214
13- // Check if wrapper exists
14- if ( ! fs . existsSync ( jsWrapperPath ) ) {
15- console . error ( `JavaScript wrapper not found at ${ jsWrapperPath } ` ) ;
16- process . exit ( 1 ) ;
17- }
15+ if ( ! fs . existsSync ( jarPath ) ) {
16+ console . error ( `ERROR: kscript.jar not found at ${ jarPath } ` ) ;
17+ console . log ( `Node.js wrapper test for ${ kscriptFile } SKIPPED due to missing kscript.jar.` ) ;
18+ resolve ( false ) ;
19+ return ;
20+ }
1821
19- // Check if test kts script exists
20- if ( ! fs . existsSync ( testScriptPath ) ) {
21- console . error ( `Test kts script not found at ${ testScriptPath } ` ) ;
22- process . exit ( 1 ) ;
23- }
22+ if ( ! fs . existsSync ( wrapperScriptPath ) ) {
23+ console . error ( `ERROR: Node.js wrapper script not found at ${ wrapperScriptPath } ` ) ;
24+ resolve ( false ) ;
25+ return ;
26+ }
2427
25- const kscriptProcess = spawn ( 'node' , [ jsWrapperPath , testScriptPath ] , {
26- stdio : [ 'pipe' , 'pipe' , 'pipe' ] , // Pipe stdin, stdout, stderr
27- timeout : 30000 // 30 seconds timeout
28- } ) ;
28+ if ( ! fs . existsSync ( kscriptFilePath ) ) {
29+ console . error ( `ERROR: Test kscript file not found at ${ kscriptFilePath } ` ) ;
30+ resolve ( false ) ;
31+ return ;
32+ }
2933
30- let stdoutData = '' ;
31- let stderrData = '' ;
34+ console . log ( `Executing command: node ${ wrapperScriptPath } ${ kscriptFilePath } ` ) ;
35+ const kscriptProcess = spawn ( 'node' , [ wrapperScriptPath , kscriptFilePath ] , {
36+ stdio : [ 'pipe' , 'pipe' , 'pipe' ] ,
37+ timeout : 60000 // 60 seconds timeout, increased for kscript compilation
38+ } ) ;
3239
33- kscriptProcess . stdout . on ( 'data' , ( data ) => {
34- stdoutData += data . toString ( ) ;
35- } ) ;
40+ let stdoutData = '' ;
41+ let stderrData = '' ;
3642
37- kscriptProcess . stderr . on ( 'data' , ( data ) => {
38- stderrData += data . toString ( ) ;
39- } ) ;
43+ kscriptProcess . stdout . on ( 'data' , ( data ) => { stdoutData += data . toString ( ) ; } ) ;
44+ kscriptProcess . stderr . on ( 'data' , ( data ) => { stderrData += data . toString ( ) ; } ) ;
4045
41- kscriptProcess . on ( 'close' , ( code ) => {
42- stdoutData = stdoutData . trim ( ) ;
43- stderrData = stderrData . trim ( ) ;
46+ // Timeout handling is implicitly done by spawn's timeout option, which emits 'error' with err.code 'ETIMEDOUT'
47+ // or kills the process and it closes with a signal code.
4448
45- console . log ( `Return Code: ${ code } ` ) ;
46- console . log ( `Stdout:\n${ stdoutData } ` ) ;
47- if ( stderrData ) {
48- console . log ( `Stderr:\n${ stderrData } ` ) ;
49- }
49+ kscriptProcess . on ( 'close' , ( code , signal ) => {
50+ stdoutData = stdoutData . trim ( ) ;
51+ stderrData = stderrData . trim ( ) ;
5052
51- // Similar to the Python test, kscript.jar and Java might not be available.
52- // We're checking if the wrapper attempts the execution.
53-
54- if ( stdoutData . includes ( "kscript wrapper test successful!" ) ) {
55- console . log ( "JavaScript wrapper test potentially successful (if kscript.jar was runnable)!" ) ;
56- // In a real test environment:
57- // if (code === 0 && stdoutData.includes("kscript wrapper test successful!")) {
58- // console.log("JavaScript wrapper test successful!");
59- // process.exit(0);
60- // } else {
61- // console.error("JavaScript wrapper test failed: Output or return code mismatch.");
62- // process.exit(1);
63- // }
64- process . exit ( 0 ) ; // For now, assume success if output is seen
65-
66- } else if ( stderrData . includes ( "ENOENT" ) && stderrData . includes ( "java" ) ) {
67- // This error (ENOENT spawn java ENOENT) means the system couldn't find 'java' command
68- console . log ( "JavaScript wrapper test partially passed: 'java' command not found, as might be expected in a limited test env." ) ;
69- process . exit ( 0 ) ;
70- } else if ( stderrData . toLowerCase ( ) . includes ( "error executing jar" ) || ( stderrData . includes ( "kscript.jar" ) && ( stderrData . includes ( "not found" ) || stderrData . includes ( "no such file" ) ) ) ) {
71- console . log ( "JavaScript wrapper test partially passed: kscript.jar not found or error during Java execution, as might be expected (no Java/JAR in test env)." ) ;
53+ if ( stdoutData ) console . log ( `Stdout:\n${ stdoutData } ` ) ;
54+ if ( stderrData ) console . error ( `Stderr:\n${ stderrData } ` ) ;
55+
56+ if ( signal ) { // Process was killed, e.g. by timeout
57+ console . error ( `Test FAILED for ${ kscriptFile } due to signal: ${ signal } ` ) ;
58+ resolve ( false ) ;
59+ } else if ( code === 0 && stdoutData . includes ( expectedOutputSubstring ) ) {
60+ console . log ( `Test PASSED for ${ kscriptFile } !` ) ;
61+ resolve ( true ) ;
62+ } else {
63+ console . error ( `Test FAILED for ${ kscriptFile } . Exit code: ${ code } ` ) ;
64+ if ( ! stdoutData . includes ( expectedOutputSubstring ) ) {
65+ console . error ( `Expected substring '${ expectedOutputSubstring } ' not found in stdout.` ) ;
66+ }
67+ resolve ( false ) ;
68+ }
69+ } ) ;
70+
71+ kscriptProcess . on ( 'error' , ( err ) => {
72+ // This 'error' event usually means the process could not be spawned or was killed due to timeout.
73+ if ( err . code === 'ETIMEDOUT' ) {
74+ console . error ( `Test TIMEOUT for ${ kscriptFile } ` ) ;
75+ } else {
76+ console . error ( `Test ERRORED for ${ kscriptFile } : ${ err . message } ` ) ;
77+ }
78+ // Ensure stderrData from the process is also logged if available
79+ if ( stderrData . trim ( ) ) console . error ( `Stderr (on error event):\n${ stderrData . trim ( ) } ` ) ;
80+ resolve ( false ) ;
81+ } ) ;
82+ } ) ;
83+ }
84+
85+ async function main ( ) {
86+ console . log ( "Starting Node.js wrapper tests..." ) ;
87+ // Similar to the Python test, this version is stricter and expects kscript.jar to be present.
88+ // If java and kscript.jar are not functional, the wrapper will likely forward an error from java,
89+ // which will be caught as a test failure.
90+
91+ const test1Success = await runKscriptTest ( 'test_wrapper.kts' , 'kscript wrapper test successful!' ) ;
92+ const test2Success = await runKscriptTest ( 'test_kotlin2_feature.kts' , 'Kotlin 2.x feature (value class) test successful!' ) ;
93+
94+ if ( test1Success && test2Success ) {
95+ console . log ( "All Node.js wrapper tests PASSED!" ) ;
7296 process . exit ( 0 ) ;
73- }
74- else {
75- console . error ( "JavaScript wrapper test failed: Did not see expected success message or known error conditions for missing Java/JAR." ) ;
97+ } else {
98+ console . error ( "One or more Node.js wrapper tests FAILED or were SKIPPED." ) ;
7699 process . exit ( 1 ) ;
77100 }
78- } ) ;
79-
80- kscriptProcess . on ( 'error' , ( err ) => {
81- console . error ( `Failed to start subprocess: ${ err . message } ` ) ;
82- process . exit ( 1 ) ;
83- } ) ;
84-
85- kscriptProcess . on ( 'timeout' , ( ) => {
86- console . error ( 'JavaScript wrapper test failed: Timeout expired.' ) ;
87- kscriptProcess . kill ( ) ; // Ensure the process is killed on timeout
88- process . exit ( 1 ) ;
89- } ) ;
101+ }
102+
103+ main ( ) ;
0 commit comments