Show encoding error when can't read a file // Issue #2796

This commit is contained in:
Ivan Kravets
2019-10-18 22:00:28 +03:00
parent 77f8414c63
commit f78a1a7b15
2 changed files with 12 additions and 8 deletions

View File

@ -54,6 +54,12 @@ def get_file_contents(path):
with open(path) as fp:
return fp.read()
except UnicodeDecodeError:
click.secho(
"Unicode decode error has occurred, please remove invalid "
"(non-ASCII or non-UTF8) characters from %s file" % path,
fg="yellow",
err=True,
)
with io.open(path, encoding="latin-1") as fp:
return fp.read()
@ -63,13 +69,6 @@ def write_file_contents(path, contents, errors=None):
with open(path, "w") as fp:
return fp.write(contents)
except UnicodeEncodeError:
if errors:
click.secho(
"Warning! There is a problem with contents encoding, please remove "
"invalid characters (non-ASCII or non-UT8) in %s" % path,
fg="yellow",
err=True,
)
with io.open(path, "w", encoding="latin-1", errors=errors) as fp:
return fp.write(contents)

View File

@ -13,6 +13,7 @@
# limitations under the License.
import os
import re
import jsondiff
import pytest
@ -534,9 +535,13 @@ def test_examples_from_dir(tmpdir_factory):
assert isinstance(raw_data["examples"], list)
assert len(raw_data["examples"]) == 6
def _to_unix_path(path):
return re.sub(r"[\\/]+", "/", path)
def _sort_examples(items):
for i, item in enumerate(items):
items[i]["files"] = sorted(item["files"])
items[i]["base"] = _to_unix_path(items[i]["base"])
items[i]["files"] = [_to_unix_path(f) for f in sorted(items[i]["files"])]
return sorted(items, key=lambda item: item["name"])
raw_data["examples"] = _sort_examples(raw_data["examples"])