Skip to content

Commit 2911ffd

Browse files
committed
add error handling for invalid input
1 parent 2053749 commit 2911ffd

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

hermetic_build/library_generation/generate_repo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import copy
16-
import re
1716
import shutil
1817
from pathlib import Path
1918
from typing import Optional
@@ -137,6 +136,8 @@ def _get_target_libraries_from_api_path(
137136
A list of LibraryConfig objects matching the target API path, or an
138137
empty list if no matches are found.
139138
"""
139+
if not ends_with_version(target_api_path):
140+
raise ValueError("api_path is not ending with a version is not supported")
140141
target_libraries = []
141142
for library in config.libraries:
142143
target_library = copy.deepcopy(library)

hermetic_build/library_generation/tests/generate_repo_unit_tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ def test_get_target_library_returns_selected_api_path_plus_dep(self):
114114

115115
self.assertEqual([another_library_v1], target_libraries)
116116

117+
def test_get_target_library_invalid_target_api_path(self):
118+
"""
119+
Tests when the api_path is invalid and sys.exit is called.
120+
"""
121+
config = GenerateRepoTest.__get_an_empty_library_config()
122+
with self.assertRaises(ValueError) as context:
123+
_target_libraries = get_target_libraries(
124+
config, target_api_path="google/cloud/another/library/type"
125+
)
126+
127+
self.assertEqual(
128+
str(context.exception),
129+
"api_path is not ending with a version is not supported",
130+
)
131+
117132
@staticmethod
118133
def __get_an_empty_generation_config() -> GenerationConfig:
119134
return GenerationConfig(

0 commit comments

Comments
 (0)