diff --git a/tests/test_osm_maps.py b/tests/test_osm_maps.py index 9eb8db04..695019a1 100644 --- a/tests/test_osm_maps.py +++ b/tests/test_osm_maps.py @@ -4,16 +4,64 @@ import os # import sys import unittest +from unittest import mock # import custom python packages from wahoomc.osm_data import CountryOsmData, XYOsmData -from wahoomc.osm_maps_functions import OsmMaps +from wahoomc.osm_maps_functions import OsmMaps, run_subprocess_and_log_output # from wahoomc.osm_maps_functions import TileNotFoundError from wahoomc.input import InputData from wahoomc import file_directory_functions as fd_fct from wahoomc import constants +class TestSubprocessOutput(unittest.TestCase): + """ + tests for launching external tooling + """ + + @ mock.patch("wahoomc.osm_maps_functions.subprocess.run") + def test_subprocess_output_uses_utf8_decode_error_handler(self, mock_run): + """ + Decode non-UTF-8 output bytes without raising from the reader thread. + """ + mock_run.return_value.returncode = 0 + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = "" + + run_subprocess_and_log_output(["tool"], "error") + + mock_run.assert_called_once_with( + ["tool"], + capture_output=True, + text=True, + encoding="utf-8", + errors="backslashreplace", + check=False, + ) + + @ mock.patch("wahoomc.osm_maps_functions.subprocess.run") + def test_subprocess_output_decode_error_handler_with_cwd(self, mock_run): + """ + Keep the same decode behavior when running in a specific directory. + """ + mock_run.return_value.returncode = 0 + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = "" + + run_subprocess_and_log_output(["tool"], "error", cwd="working-dir") + + mock_run.assert_called_once_with( + ["tool"], + capture_output=True, + cwd="working-dir", + text=True, + encoding="utf-8", + errors="backslashreplace", + check=False, + ) + + class TestOsmMapsCalculation(unittest.TestCase): """ tests for the OSM maps file diff --git a/wahoomc/osm_maps_functions.py b/wahoomc/osm_maps_functions.py index 94128805..24bcdb83 100644 --- a/wahoomc/osm_maps_functions.py +++ b/wahoomc/osm_maps_functions.py @@ -40,11 +40,24 @@ def run_subprocess_and_log_output(cmd, error_message, cwd=""): """ if not cwd: process = subprocess.run( - cmd, capture_output=True, text=True, encoding="utf-8", check=False) + cmd, + capture_output=True, + text=True, + encoding="utf-8", + errors="backslashreplace", + check=False, + ) else: process = subprocess.run( # pylint: disable=consider-using-with - cmd, capture_output=True, cwd=cwd, text=True, encoding="utf-8", check=False) + cmd, + capture_output=True, + cwd=cwd, + text=True, + encoding="utf-8", + errors="backslashreplace", + check=False, + ) if error_message and process.returncode != 0: # 0 means success