From 8219948382ea3a287f7fb780023854e5a981d103 Mon Sep 17 00:00:00 2001 From: Travis Beckwith <113939491+TravisBeckwith@users.noreply.github.com> Date: Fri, 17 Jul 2026 15:40:06 -0400 Subject: [PATCH] Add files via upload --- bids_convert.sh | 30 +++++++++++++++++++----------- tests/test_bids_convert.sh | 27 +++++++++++++++++---------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/bids_convert.sh b/bids_convert.sh index 2dc6d05..bc1e185 100644 --- a/bids_convert.sh +++ b/bids_convert.sh @@ -515,7 +515,7 @@ convert_folder() { log_dbg " Renamed: $(basename "$old_file") → $(basename "$new_file")" fi done - ((run_num++)) + ((run_num++)) || true done fi @@ -800,7 +800,7 @@ detect_structure() { local imaging_child=false for gc in "$child"/*/; do [ ! -d "$gc" ] && continue - ((grandchild_count++)) + ((grandchild_count++)) || true local gc_name gc_name="$(basename "$gc")" if match_folder "$gc_name" 2>/dev/null; then @@ -833,13 +833,19 @@ normalize_subject_id() { local raw="$1" local id="$raw" - # Strip known prefixes (most specific first) + # Strip known prefixes — longest/most-specific first. Order matters: + # each ${id#pattern} only strips once, against whatever id currently is, + # so a short prefix tried before a longer one that contains it (e.g. + # "SUB" before "SUBJECT_") will consume part of the longer prefix and + # leave the rest stuck to the id (SUBJECT_001 -> SUB stripped -> JECT_001). + id="${id#SUBJECT_}" + id="${id#SUBJECT}" + id="${id#subject_}" + id="${id#subject}" id="${id#sub-}" - id="${id#sub}" id="${id#SUB-}" + id="${id#sub}" id="${id#SUB}" - id="${id#SUBJECT_}" - id="${id#SUBJECT}" # Remove leading underscores/hyphens id="${id#_}" @@ -860,12 +866,14 @@ normalize_session_id() { local raw="$1" local id="$raw" + id="${id#SESSION_}" + id="${id#SESSION}" + id="${id#session_}" + id="${id#session}" id="${id#ses-}" - id="${id#ses}" id="${id#SES-}" + id="${id#ses}" id="${id#SES}" - id="${id#SESSION_}" - id="${id#SESSION}" id="${id#VISIT}" id="${id#TIMEPOINT}" id="${id#TP}" @@ -1050,7 +1058,7 @@ fixup_run_numbers() { fi done fi - ((run_num++)) + ((run_num++)) || true done done } @@ -1295,7 +1303,7 @@ main() { process_subject "$sub_dir" "$sub_label" "$structure" fi - ((sub_count++)) + ((sub_count++)) || true echo "" done shopt -u nullglob diff --git a/tests/test_bids_convert.sh b/tests/test_bids_convert.sh index 329cdde..3a3e3d2 100644 --- a/tests/test_bids_convert.sh +++ b/tests/test_bids_convert.sh @@ -29,12 +29,12 @@ assert_eq() { local label="$1" expected="$2" actual="$3" if [ "$expected" = "$actual" ]; then echo " ✅ PASS: $label" - ((PASS++)) + ((PASS++)) || true else echo " ❌ FAIL: $label" echo " expected: '$expected'" echo " actual: '$actual'" - ((FAIL++)) + ((FAIL++)) || true fi } @@ -42,10 +42,10 @@ assert_file_exists() { local label="$1" filepath="$2" if [ -f "$filepath" ]; then echo " ✅ PASS: $label" - ((PASS++)) + ((PASS++)) || true else echo " ❌ FAIL: $label — file not found: $filepath" - ((FAIL++)) + ((FAIL++)) || true fi } @@ -53,10 +53,10 @@ assert_contains() { local label="$1" filepath="$2" pattern="$3" if grep -q "$pattern" "$filepath" 2>/dev/null; then echo " ✅ PASS: $label" - ((PASS++)) + ((PASS++)) || true else echo " ❌ FAIL: $label — pattern '$pattern' not found in $filepath" - ((FAIL++)) + ((FAIL++)) || true fi } @@ -88,9 +88,16 @@ test_dry_run_creates_no_files() { touch "${input_dir}/SUBJECT_001/SAG_T1/dummy.dcm" # Dry-run should NOT create actual NIfTI files (dcm2niix won't run) - bash "$SCRIPT" -i "$input_dir" -o "$output_dir" -n 2>&1 | grep -q "DRY-RUN" && \ - echo " ✅ PASS: dry-run mode activated" && ((PASS++)) || \ - { echo " ❌ FAIL: dry-run mode not detected"; ((FAIL++)); } + # Capture output first rather than piping straight into `grep -q`: with + # pipefail (set above) and DRY-RUN appearing early in the output, grep -q + # exits as soon as it finds a match while bids_convert.sh is still + # writing later lines, which kills it with SIGPIPE (exit 141) — pipefail + # then reports the pipeline as failed even though grep did match. + local out + out="$(bash "$SCRIPT" -i "$input_dir" -o "$output_dir" -n 2>&1)" || true + echo "$out" | grep -q "DRY-RUN" && \ + echo " ✅ PASS: dry-run mode activated" && { ((PASS++)) || true; } || \ + { echo " ❌ FAIL: dry-run mode not detected"; ((FAIL++)) || true; } teardown_tmp } @@ -111,7 +118,7 @@ test_bids_scaffold_creation() { # In dry-run the scaffold won't be created, so we test the help works # and the script doesn't crash echo " ✅ PASS: script completed without error in dry-run" - ((PASS++)) + ((PASS++)) || true teardown_tmp }