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
28 changes: 28 additions & 0 deletions tests/integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,31 @@ def test_error_flag():
["check", "--error", "e004", "tests/invalid/whitespace_in_key.yml"],
)
assert result.exit_code == 1, result.output


def test_convert_preserves_explicit_empty_string_fields():
"""Ensure quoted empty strings do not shift TSV columns during conversion."""
runner = CliRunner()
input_file = "tests/valid/explicit_empty_string_watermark.yml"
output_file = "/tmp/y2b_explicit_empty_string_watermark.tsv"
result = runner.invoke(
yml2block.__main__.main,
["convert", input_file, "-o", output_file],
)

assert result.exit_code == 0, result.output
assert "Invalid entry ''" not in result.output

with open(output_file, "r") as tsv_file:
lines = tsv_file.readlines()

header_idx = next(
idx for idx, line in enumerate(lines) if line.startswith("#datasetField")
)
header = lines[header_idx].rstrip("\n").split("\t")
row = lines[header_idx + 1].rstrip("\n").split("\t")

assert len(header) == len(row)
assert row[header.index("watermark")] == ""
assert row[header.index("fieldType")] == "text"
assert row[header.index("displayOrder")] == "1"
20 changes: 20 additions & 0 deletions tests/valid/explicit_empty_string_watermark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
metadataBlock:
- name: Example
dataverseAlias:
displayName: Example
datasetField:
- name: Foo
title: Foo
description: Some field
watermark: ""
fieldType: text
displayOrder: 1
displayFormat:
advancedSearchField: true
allowControlledVocabulary: false
allowmultiples: false
facetable: false
displayoncreate: true
required: true
parent:
metadatablock_id: Example
1 change: 1 addition & 0 deletions yml2block/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

https://guides.dataverse.org/en/latest/admin/metadatacustomization.html
"""

import os
import sys
import click
Expand Down
4 changes: 4 additions & 0 deletions yml2block/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def write_metadata_block(yml_metadata, output_path, longest_line, verbose):
# This catches empty values, which yaml reports
# as a Python None
new_line.append("")
elif value == "":
# Quoted empty strings in YAML are valid values and
# must produce an empty TSV cell without shifting columns.
new_line.append("")
elif str(value):
# This could be more specific using int() and float()
# The conversion of `value` to a string happens to
Expand Down