diff --git a/snakebids/paths/_factory.py b/snakebids/paths/_factory.py index 85648a04..a6bc3325 100644 --- a/snakebids/paths/_factory.py +++ b/snakebids/paths/_factory.py @@ -228,13 +228,21 @@ def bids( if datatype: path_parts.append(datatype) + tail = [suffix] if suffix is not None else [] path_parts.append( - "_".join(it.chain(spec_parts[:split], custom_parts, spec_parts[split:])) + "_".join( + it.chain( + spec_parts[:split], + custom_parts, + spec_parts[split:], + tail, + ) + ) ) + result = os.path.join(*path_parts) + if extension is not None: + result += extension - tail = f"_{suffix}{extension or ''}" if suffix else extension or "" - - result = os.path.join(*path_parts) + tail if custom_parts and _implicit and not in_interactive_session(): wrn_msg = ( f"Path generated with unrecognized entities, and a snakebids spec has " diff --git a/snakebids/tests/test_paths/test_bids.py b/snakebids/tests/test_paths/test_bids.py index ac905847..e6cc866a 100644 --- a/snakebids/tests/test_paths/test_bids.py +++ b/snakebids/tests/test_paths/test_bids.py @@ -144,6 +144,12 @@ def test_underscore_does_not_precede_extension( def test_no_underscore_at_end_if_no_suffix(self, entities: dict[str, str]): assert bids(**entities)[-1] != "_" + @given(entities=_bids_args(nonstandard=True, entities=None)) + def test_no_underscore_at_beginning_if_only_suffix( + self, entities: dict[str, str] + ): + assert bids(**entities)[0] != "_" + @given(entities=_bids_args(), root=_roots()) def test_beginning_of_path_always_root( self, entities: dict[str, str], root: str @@ -322,4 +328,6 @@ def test_benchmark_bids(benchmark: Benchmark): england="britain", space="cosmos", rome="fell", + suffix="suffix", + extension=".ext", )