diff --git a/databusclient/filehandling/format.py b/databusclient/filehandling/format.py index c1cd4ec1..20f6e5fa 100644 --- a/databusclient/filehandling/format.py +++ b/databusclient/filehandling/format.py @@ -200,7 +200,7 @@ def get_converted_filename(original_filename: str, convert_format: str) -> str: break # strip existing format extension (longest first) - for old_ext in sorted(FORMAT_TO_EXTENSION.values(), key=len, reverse=True): + for old_ext in sorted(EXTENSION_TO_FORMAT.keys(), key=len, reverse=True): if name.lower().endswith(old_ext): name = name[: -len(old_ext)] break @@ -524,7 +524,15 @@ def convert_file( if input_format == convert_format: # Input and target format are identical. # Copy input to output path so the caller always receives an output file. - if input_file != output_file: + same_file = ( + os.path.exists(input_file) + and os.path.exists(output_file) + and os.path.samefile(input_file, output_file) + ) + if not same_file: + parent = os.path.dirname(output_file) + if parent: + os.makedirs(parent, exist_ok=True) shutil.copy2(input_file, output_file) print( f"Input and target format are both '{input_format}'. " diff --git a/tests/test_format_round_trips.py b/tests/test_format_round_trips.py index b828ddb8..6f15a778 100644 --- a/tests/test_format_round_trips.py +++ b/tests/test_format_round_trips.py @@ -473,4 +473,32 @@ def test_mapping_quads_to_csv_and_back(): assert csv_graphs == original_graph_uris, ( f"Graph URIs in CSV do not match original. " f"Expected: {original_graph_uris}, got: {csv_graphs}" - ) \ No newline at end of file + ) + + + +def test_get_converted_filename_strips_all_recognized_extensions(): + """Ensure .owl, .xml, and .json extensions are stripped just like primary ones.""" + from databusclient.filehandling.format import get_converted_filename + + assert get_converted_filename("ontology.owl", "turtle") == "ontology.ttl" + assert get_converted_filename("data.xml.gz", "ntriples") == "data.nt" + assert get_converted_filename("graph.json", "nquads") == "graph.nq" + assert get_converted_filename("graph.jsonld.bz2", "trig") == "graph.trig" + + +def test_convert_file_same_format_creates_parent_dir_and_handles_same_path( + tmp_path, monkeypatch +): + """Same-format copy creates missing parent dirs and skips equivalent paths.""" + from databusclient.filehandling.format import convert_file + + source = resource("sample.ttl") + nested_out = tmp_path / "nested" / "subdir" / "copy.ttl" + convert_file(source, str(nested_out), "turtle") + assert nested_out.exists() + + # Equivalent path via relative reference should not raise SameFileError + monkeypatch.chdir(nested_out.parent) + convert_file("copy.ttl", os.path.join(".", "copy.ttl"), "ttl") + assert nested_out.exists()