Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions bids_convert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ convert_folder() {
log_dbg " Renamed: $(basename "$old_file") → $(basename "$new_file")"
fi
done
((run_num++))
((run_num++)) || true
done
fi

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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#_}"
Expand All @@ -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}"
Expand Down Expand Up @@ -1050,7 +1058,7 @@ fixup_run_numbers() {
fi
done
fi
((run_num++))
((run_num++)) || true
done
done
}
Expand Down Expand Up @@ -1295,7 +1303,7 @@ main() {
process_subject "$sub_dir" "$sub_label" "$structure"
fi

((sub_count++))
((sub_count++)) || true
echo ""
done
shopt -u nullglob
Expand Down
27 changes: 17 additions & 10 deletions tests/test_bids_convert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,34 @@ 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
}

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
}

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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
Loading