Skip to content

Commit

Permalink
Merge pull request #214 from valory-xyz/fix/csv-io
Browse files Browse the repository at this point in the history
[WIP] Fix CSV IO issues on windows
  • Loading branch information
DavidMinarsch authored Jul 6, 2022
2 parents 26d26b2 + 799733d commit 42df794
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
11 changes: 6 additions & 5 deletions aea/cli/ipfs_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
- https://docs.ipfs.io/guides/guides/install/
"""

import os
import sys
import traceback
from pathlib import Path
Expand Down Expand Up @@ -137,10 +136,12 @@ def hash_package(
if configuration.directory is None:
raise ValueError("configuration.directory cannot be None.")

key = os.path.join(
configuration.author,
package_type.to_plural(),
configuration.directory.name,
key = "/".join(
[
configuration.author,
package_type.to_plural(),
configuration.directory.name,
]
)
package_hash = IPFSHashOnly.hash_directory(
str(configuration.directory), wrap=(not no_wrap)
Expand Down
2 changes: 1 addition & 1 deletion aea/helpers/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def to_csv(data: Dict[str, str], path: Path) -> None:
"""Outputs a dictionary to CSV."""
try:
ordered = collections.OrderedDict(sorted(data.items()))
with open(path, "w") as csv_file:
with open(path, "w", newline="") as csv_file:
writer = csv.writer(csv_file)
writer.writerows(ordered.items())
except IOError:
Expand Down
26 changes: 25 additions & 1 deletion tests/test_helpers/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@
# ------------------------------------------------------------------------------
"""This module contains the tests for the 'aea.helpers.io' module."""
import os
import tempfile
from pathlib import Path
from unittest.mock import MagicMock

import pytest

from aea.helpers.io import open_file
from aea.helpers.io import from_csv, open_file, to_csv


DUMMY_CSV_DATA = {
"key_0": "value_0",
"key_1": "value_1",
"key_2": "value_2",
"key_3": "value_3",
}


@pytest.mark.parametrize(argnames="path_builder", argvalues=[os.path.join, Path])
Expand All @@ -46,3 +55,18 @@ def test_raise_if_binary_mode():
"""Raise if mode is binary mode."""
with pytest.raises(ValueError, match="This function can only work in text mode."):
open_file(MagicMock(), mode="rb")


def test_csv_io() -> None:
"""Test csv utils."""

with tempfile.TemporaryDirectory() as temp_dir:
csv_file = Path(temp_dir, "file.csv")

to_csv(DUMMY_CSV_DATA, csv_file)
assert csv_file.exists()

csv_data = from_csv(csv_file)
assert any(
[csv_data[key] == value for key, value in DUMMY_CSV_DATA.items()]
), csv_data

0 comments on commit 42df794

Please sign in to comment.