Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion tests/test_osm_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions wahoomc/osm_maps_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading