Skip to content

Bug: StopIteration crash when diffing empty CSV files #52

Description

@its-Sohan

when running csv-diff on two csv files that contain only headers
and no data rows, the tool crashes with stopiteration instead of
returning "no differences" or an empty diff.

steps to reproduce:

echo "id,name,age" > a.csv
echo "id,name,age" > b.csv
csv-diff a.csv b.csv --key=id
expected behavior:
should exit cleanly with no output or "0 rows changed" message.
actual behavior:
crashes with stopiteration:
traceback (most recent call last):
  file "/home/sohan/projects/csv-diff/venv/bin/csv-diff", line 8, in <module>
    sys.exit(cli())
  ...
  file "/home/sohan/projects/csv-diff/csv_diff/__init__.py", line 64, in compare
    previous_columns = set(next(iter(previous.values())).keys())
                           ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
stopiteration
root cause:
in csv_diff/__init__.py lines 64-65:
previous_columns = set(next(iter(previous.values())).keys())
current_columns = set(next(iter(current.values())).keys())
when both files have no data rows, load_csv returns {},
and next(iter({}.values())) raises stopiteration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions