forked from platformio/platformio-core
Show encoding error when can't read a file // Issue #2796
This commit is contained in:
@ -54,6 +54,12 @@ def get_file_contents(path):
|
|||||||
with open(path) as fp:
|
with open(path) as fp:
|
||||||
return fp.read()
|
return fp.read()
|
||||||
except UnicodeDecodeError:
|
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:
|
with io.open(path, encoding="latin-1") as fp:
|
||||||
return fp.read()
|
return fp.read()
|
||||||
|
|
||||||
@ -63,13 +69,6 @@ def write_file_contents(path, contents, errors=None):
|
|||||||
with open(path, "w") as fp:
|
with open(path, "w") as fp:
|
||||||
return fp.write(contents)
|
return fp.write(contents)
|
||||||
except UnicodeEncodeError:
|
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:
|
with io.open(path, "w", encoding="latin-1", errors=errors) as fp:
|
||||||
return fp.write(contents)
|
return fp.write(contents)
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
import jsondiff
|
import jsondiff
|
||||||
import pytest
|
import pytest
|
||||||
@ -534,9 +535,13 @@ def test_examples_from_dir(tmpdir_factory):
|
|||||||
assert isinstance(raw_data["examples"], list)
|
assert isinstance(raw_data["examples"], list)
|
||||||
assert len(raw_data["examples"]) == 6
|
assert len(raw_data["examples"]) == 6
|
||||||
|
|
||||||
|
def _to_unix_path(path):
|
||||||
|
return re.sub(r"[\\/]+", "/", path)
|
||||||
|
|
||||||
def _sort_examples(items):
|
def _sort_examples(items):
|
||||||
for i, item in enumerate(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"])
|
return sorted(items, key=lambda item: item["name"])
|
||||||
|
|
||||||
raw_data["examples"] = _sort_examples(raw_data["examples"])
|
raw_data["examples"] = _sort_examples(raw_data["examples"])
|
||||||
|
Reference in New Issue
Block a user