@@ -448,10 +448,114 @@ main() {
448448 # Run JavaScript test files...
449449 add_task ' run_javascript_tests'
450450 if [[ -z " ${skip_javascript_tests} " ]]; then
451- files=$( echo " ${changed_files} " | grep ' /test/.*\.js$' | grep -v ' /test/fixtures/.*\.js$' | grep -v ' /test/.*/fixtures/.*\.js$' | tr ' \n' ' ' )
452- if [[ -n " ${files} " ]]; then
451+ # Find test files for a given package directory
452+ find_test_files () {
453+ local pkg_dir=" $1 "
454+ local pattern=" $2 "
455+
456+ if [[ -d " $pkg_dir /test" ]]; then
457+ find " $pkg_dir /test" -name " $pattern " 2> /dev/null | grep -v ' fixtures'
458+ fi
459+ }
460+
461+ # Extract package directory from a file path
462+ get_pkg_dir () {
463+ dirname " $( dirname " $1 " ) "
464+ }
465+
466+ # Extract package pattern for compilation
467+ get_pkg_pattern () {
468+ local pkg_dir=" $1 "
469+ local pkg_name=$( basename " $pkg_dir " )
470+ local pkg_parent=$( dirname " $pkg_dir " )
471+ echo " $( echo " $pkg_parent " | sed ' s|.*/node_modules/@stdlib/||' ) /$pkg_name "
472+ }
473+
474+ # Get changed test files directly
475+ test_files=$( echo " ${changed_files} " | grep ' /test/.*\.js$' | grep -v ' /test/fixtures/.*\.js$' | grep -v ' /test/.*/fixtures/.*\.js$' )
476+
477+ # Get changed source files
478+ js_source_files=$( echo " ${changed_files} " | grep -E ' /(lib)/.*\.js$' )
479+ c_source_files=$( echo " ${changed_files} " | grep -E ' /src/.*\.c$' )
480+ manifest_files=$( echo " ${changed_files} " | grep ' manifest.json$' )
481+
482+ source_test_files=" "
483+ packages_to_compile=" "
484+
485+ # Process JavaScript source files
486+ if [[ -n " ${js_source_files} " ]]; then
487+ echo ' Identifying tests for changed JavaScript source files...' >&2
488+ while IFS= read -r file; do
489+ pkg_dir=$( get_pkg_dir " $file " )
490+ potential_tests=$( find_test_files " $pkg_dir " " *.js" )
491+ if [[ -n " ${potential_tests} " ]]; then
492+ source_test_files=" ${source_test_files} ${potential_tests} "
493+ fi
494+ done <<< " ${js_source_files}"
495+ fi
496+
497+ # Process C source files and manifest files
498+ if [[ -n " ${c_source_files} " || -n " ${manifest_files} " ]]; then
499+ echo ' Identifying tests for changed C source files and manifests...' >&2
500+
501+ # Process C source files
502+ if [[ -n " ${c_source_files} " ]]; then
503+ while IFS= read -r file; do
504+ pkg_dir=$( get_pkg_dir " $file " )
505+ pkg_pattern=$( get_pkg_pattern " $pkg_dir " )
506+
507+ # Add to packages to compile
508+ if [[ -n " $pkg_pattern " && " $packages_to_compile " != * " $pkg_pattern " * ]]; then
509+ packages_to_compile=" ${packages_to_compile} ${pkg_pattern} "
510+ fi
511+
512+ potential_tests=$( find_test_files " $pkg_dir " " *.native.js" )
513+ if [[ -n " ${potential_tests} " ]]; then
514+ source_test_files=" ${source_test_files} ${potential_tests} "
515+ fi
516+ done <<< " ${c_source_files}"
517+ fi
518+
519+ # Process manifest files
520+ if [[ -n " ${manifest_files} " ]]; then
521+ while IFS= read -r file; do
522+ pkg_dir=$( dirname " $file " )
523+ pkg_pattern=$( get_pkg_pattern " $pkg_dir " )
524+
525+ # Add to packages to compile if it has a src directory with C files
526+ if [[ -d " $pkg_dir /src" ]] && ls " $pkg_dir /src" /* .c > /dev/null 2>&1 ; then
527+ if [[ -n " $pkg_pattern " && " $packages_to_compile " != * " $pkg_pattern " * ]]; then
528+ packages_to_compile=" ${packages_to_compile} ${pkg_pattern} "
529+ fi
530+
531+ potential_tests=$( find_test_files " $pkg_dir " " *.native.js" )
532+ if [[ -n " ${potential_tests} " ]]; then
533+ source_test_files=" ${source_test_files} ${potential_tests} "
534+ fi
535+ fi
536+ done <<< " ${manifest_files}"
537+ fi
538+
539+ # Compile native add-ons if needed
540+ if [[ -n " ${packages_to_compile} " ]]; then
541+ echo " Compiling native add-ons for: ${packages_to_compile} " >&2
542+ for pkg in ${packages_to_compile} ; do
543+ make install-node-addons NODE_ADDONS_PATTERN=" ${pkg} " > /dev/null >&2
544+ if [[ " $? " -ne 0 ]]; then
545+ echo " Failed to compile native add-on for ${pkg} " >&2
546+ task_status ' failed'
547+ on_error 1
548+ fi
549+ done
550+ fi
551+ fi
552+
553+ # Combine test files from both sources and remove duplicates
554+ all_test_files=$( echo " ${test_files} ${source_test_files} " | tr ' ' ' \n' | sort | uniq | tr ' \n' ' ' )
555+
556+ if [[ -n " ${all_test_files} " ]]; then
453557 echo ' Running JavaScript test files...' >&2
454- make FILES=" ${files } " test-javascript-files > /dev/null >&2
558+ make FILES=" ${all_test_files } " test-javascript-files > /dev/null >&2
455559 if [[ " $? " -ne 0 ]]; then
456560 task_status ' failed'
457561 echo ' ' >&2
0 commit comments