Repository: ESSS/pytest-replay (language: python)

Issue:
Ignore empty lines in replay files
When cutting/pasting stuff around in the replay files, lines can be commented out, but if some blank line is added, the run errors. I suspect this is because the code always does a `json.loads` if the line is not a comment.

What about skipping empty lines along with commented ones?


Discussion / hints:
Sounds reasonable. 👍 
I can submit a PR in these days 

Fix patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index b8030f5..da67819 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,3 +1,10 @@
+UNRELEASED
+==========
+
+* Ignore empty and blank lines in the replay file (`#70`_).
+
+.. _`#70`: https://github.com/ESSS/pytest-replay/issues/70
+
 1.5.2 (2024-09-03)
 ==================
 


Test patch:
diff --git a/src/pytest_replay/__init__.py b/src/pytest_replay/__init__.py
index 4b350ba..ceb5d44 100644
--- a/src/pytest_replay/__init__.py
+++ b/src/pytest_replay/__init__.py
@@ -114,11 +114,13 @@ def pytest_collection_modifyitems(self, items, config):
         with open(replay_file, encoding="UTF-8") as f:
             all_lines = f.readlines()
             # Use a dict to deduplicate the node ids while keeping the order.
-            nodeids = dict.fromkeys(
-                json.loads(line)["nodeid"]
-                for line in all_lines
-                if not line.strip().startswith(("#", "//"))
-            )
+            nodeids = {}
+            for line in all_lines:
+                stripped = line.strip()
+                # Ignore blank linkes and comments. (#70)
+                if stripped and not stripped.startswith(("#", "//")):
+                    nodeid = json.loads(stripped)["nodeid"]
+                    nodeids[nodeid] = None
 
         items_dict = {item.nodeid: item for item in items}
         remaining = []
diff --git a/tests/test_replay.py b/tests/test_replay.py
index 63c17cc..ace97e0 100644
--- a/tests/test_replay.py
+++ b/tests/test_replay.py
@@ -1,6 +1,7 @@
 import itertools as it
 import json
 import re
+from pathlib import Path
 
 import pytest
 
@@ -403,3 +404,30 @@ def test_test_fail_skip_teardown(skip_teardown):
         "test_module.py::test_skip_teardown": "skipped",
         "test_module.py::test_test_fail_skip_teardown": "failed",
     }
+
+
+@pytest.mark.usefixtures("suite")
+def test_empty_or_blank_lines(testdir):
+    """Empty or blank line in replay files should be ignored."""
+    dir = testdir.tmpdir / "replay"
+    options = [f"--replay-record-dir={dir}"]
+    result = testdir.runpytest(*options)
+
+    replay_file: Path = dir / ".pytest-replay.txt"
+
+    with replay_file.open("r+") as f:
+        content = f.readlines()
+
+        # Add empty line
+        content.insert(1, "\n")
+        # Add blank line
+        content.insert(1, "    \n")
+        # Add empty line
+        content.append("\n")
+        # Add mixed blank line
+        content.append("\t \n")
+        f.seek(0)
+        f.writelines(content)
+
+    result = testdir.runpytest(f"--replay={replay_file}", "-v")
+    assert result.ret == 0