diff --git a/pyhgtmap/main.py b/pyhgtmap/main.py index a40b8a8..abd0fa8 100644 --- a/pyhgtmap/main.py +++ b/pyhgtmap/main.py @@ -492,17 +492,17 @@ def parseCommandLine(sys_args: List[str]) -> Tuple[Values, List[str]]: opts.dataSource[opts.dataSource.index(s)] = "{0:s}v{1:.1f}".format( s, opts.srtmVersion ) - else: + elif len(args) == 0: + # No explicit source nor input provided, try to download using default opts.dataSource = [] if opts.viewfinder != 0: opts.dataSource.append("view{0:d}".format(opts.viewfinder)) opts.dataSource.append( "srtm{0:d}v{1:.1f}".format(opts.srtmResolution, opts.srtmVersion) ) - if not opts.area and not opts.polygon: - # this is a hint for makeOsmFilename() that files are specified on the - # command line - opts.dataSource = [] + else: + # Input files provided, no download source + opts.dataSource = [] needsEarthexplorerLogin = False for s in opts.dataSource: if s.startswith("srtm") and "v3" in s: diff --git a/tests/test_main.py b/tests/test_main.py index 2ca624a..2bdcbef 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,6 +1,6 @@ import optparse import os -from unittest.mock import patch +from unittest.mock import MagicMock, patch from pyhgtmap import main from . import TEST_DATA_PATH @@ -8,7 +8,9 @@ @patch("pyhgtmap.main.NASASRTMUtil") @patch("pyhgtmap.main.HgtFilesProcessor") -def test_main_download_from_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> None: +def test_main_download_from_poly( + HgtFilesProcessor_mock: MagicMock, NASASRTMUtil_mock: MagicMock +) -> None: """Only polygon option is used, without files; download tiles.""" # Prepare sys_args = [ @@ -52,7 +54,9 @@ def test_main_download_from_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> N @patch("pyhgtmap.main.NASASRTMUtil") @patch("pyhgtmap.main.HgtFilesProcessor") -def test_main_manual_input_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> None: +def test_main_manual_input_poly( + HgtFilesProcessor_mock: MagicMock, NASASRTMUtil_mock: MagicMock +) -> None: """Polygon option is used, with manual files; polygon must be applied to files.""" # Prepare sys_args = [ @@ -80,9 +84,45 @@ def test_main_manual_input_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> No ) +@patch("pyhgtmap.main.configUtil") @patch("pyhgtmap.main.NASASRTMUtil") @patch("pyhgtmap.main.HgtFilesProcessor") -def test_main_manual_input_no_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> None: +def test_main_manual_input_poly_no_source( + HgtFilesProcessor_mock: MagicMock, + NASASRTMUtil_mock: MagicMock, + configUtil_mock: MagicMock, +) -> None: + """Earthexplorer credentials shouldn't be required when providing tiles in input with a polygon.""" + # Prepare + sys_args = [ + f"--polygon={os.path.join(TEST_DATA_PATH, 'france.poly')}", + "N45E007.hgt", + "N46E007.hgt", + "N47E007.hgt", + ] + + # Test + main.main_internal(sys_args) + + # Check + configUtil_mock.Config.assert_not_called() + NASASRTMUtil_mock.getFiles.assert_not_called() + + HgtFilesProcessor_mock.assert_called_once() + parsed_options: optparse.Values = HgtFilesProcessor_mock.call_args.args[3] + # area must be properly computed from files names + assert parsed_options.area == "7:45:8:48" + # Polygon check must be enabled for all files + HgtFilesProcessor_mock.return_value.process_files.assert_called_once_with( + [("N45E007.hgt", True), ("N46E007.hgt", True), ("N47E007.hgt", True)] + ) + + +@patch("pyhgtmap.main.NASASRTMUtil") +@patch("pyhgtmap.main.HgtFilesProcessor") +def test_main_manual_input_no_poly( + HgtFilesProcessor_mock: MagicMock, NASASRTMUtil_mock: MagicMock +) -> None: """Polygon option is NOT used, with manual files.""" # Prepare sys_args = [