12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
import os
15
+ import shutil
15
16
import unittest
17
+ from pathlib import Path
16
18
from unittest .mock import patch , ANY
17
19
from click .testing import CliRunner
18
20
from library_generation .cli .entry_point import (
@@ -49,6 +51,20 @@ def test_entry_point_with_invalid_config_raise_file_exception(self):
49
51
self .assertEqual (FileNotFoundError , result .exc_info [0 ])
50
52
self .assertRegex (result .exception .args [0 ], "/non-existent/file does not exist." )
51
53
54
+ def test_entry_point_with_invalid_generation_input_raise_file_exception (
55
+ self ,
56
+ ):
57
+ os .chdir (script_dir )
58
+ runner = CliRunner ()
59
+ # noinspection PyTypeChecker
60
+ result = runner .invoke (generate , ["--generation-input=/non-existent/folder" ])
61
+ self .assertEqual (1 , result .exit_code )
62
+ self .assertEqual (FileNotFoundError , result .exc_info [0 ])
63
+ self .assertRegex (
64
+ result .exception .args [0 ],
65
+ "/non-existent/folder/generation_config.yaml does not exist." ,
66
+ )
67
+
52
68
def test_validate_generation_config_succeeds (
53
69
self ,
54
70
):
@@ -94,6 +110,7 @@ def test_generate_non_monorepo_without_library_names_full_generation(
94
110
# does special handling when a method is annotated with @main.command()
95
111
generate_impl (
96
112
generation_config_path = config_path ,
113
+ generation_input = None ,
97
114
library_names = None ,
98
115
repository_path = "." ,
99
116
api_definitions_path = "." ,
@@ -122,6 +139,7 @@ def test_generate_non_monorepo_with_library_names_full_generation(
122
139
# does special handling when a method is annotated with @main.command()
123
140
generate_impl (
124
141
generation_config_path = config_path ,
142
+ generation_input = None ,
125
143
library_names = "non-existent-library" ,
126
144
repository_path = "." ,
127
145
api_definitions_path = "." ,
@@ -150,6 +168,7 @@ def test_generate_monorepo_with_common_protos_without_library_names_triggers_ful
150
168
# does special handling when a method is annotated with @main.command()
151
169
generate_impl (
152
170
generation_config_path = config_path ,
171
+ generation_input = None ,
153
172
library_names = None ,
154
173
repository_path = "." ,
155
174
api_definitions_path = "." ,
@@ -177,6 +196,7 @@ def test_generate_monorepo_with_common_protos_with_library_names_triggers_full_g
177
196
# does special handling when a method is annotated with @main.command()
178
197
generate_impl (
179
198
generation_config_path = config_path ,
199
+ generation_input = None ,
180
200
library_names = "iam,non-existent-library" ,
181
201
repository_path = "." ,
182
202
api_definitions_path = "." ,
@@ -206,6 +226,7 @@ def test_generate_monorepo_without_library_names_trigger_full_generation(
206
226
# does special handling when a method is annotated with @main.command()
207
227
generate_impl (
208
228
generation_config_path = config_path ,
229
+ generation_input = None ,
209
230
library_names = None ,
210
231
repository_path = "." ,
211
232
api_definitions_path = "." ,
@@ -235,6 +256,7 @@ def test_generate_monorepo_with_library_names_trigger_selective_generation(
235
256
# does special handling when a method is annotated with @main.command()
236
257
generate_impl (
237
258
generation_config_path = config_path ,
259
+ generation_input = None ,
238
260
library_names = "asset" ,
239
261
repository_path = "." ,
240
262
api_definitions_path = "." ,
@@ -245,3 +267,44 @@ def test_generate_monorepo_with_library_names_trigger_selective_generation(
245
267
api_definitions_path = ANY ,
246
268
target_library_names = ["asset" ],
247
269
)
270
+
271
+ @patch ("library_generation.cli.entry_point.from_yaml" )
272
+ def test_generate_provide_generation_input (
273
+ self ,
274
+ from_yaml ,
275
+ ):
276
+ """
277
+ This test confirms that when no generation_config_path and
278
+ only generation_input is provided, it looks inside this path
279
+ for generation config and creates versions file when not exists
280
+ """
281
+ config_path = f"{ test_resource_dir } /generation_config.yaml"
282
+ self ._create_folder_in_current_dir ("test-output" )
283
+ # we call the implementation method directly since click
284
+ # does special handling when a method is annotated with @main.command()
285
+ generate_impl (
286
+ generation_config_path = None ,
287
+ generation_input = test_resource_dir ,
288
+ library_names = "asset" ,
289
+ repository_path = "./test-output" ,
290
+ api_definitions_path = "." ,
291
+ )
292
+ from_yaml .assert_called_with (os .path .abspath (config_path ))
293
+ self .assertTrue (os .path .exists (f"test-output/versions.txt" ))
294
+
295
+ def tearDown (self ):
296
+ # clean up after
297
+ if os .path .exists ("./output" ):
298
+ shutil .rmtree (Path ("./output" ))
299
+ if os .path .exists ("./test-output" ):
300
+ shutil .rmtree (Path ("./test-output" ))
301
+
302
+ def _create_folder_in_current_dir (self , folder_name ):
303
+ """Creates a folder in the current directory."""
304
+ try :
305
+ os .makedirs (
306
+ folder_name , exist_ok = True
307
+ ) # exist_ok prevents errors if folder exists
308
+ print (f"Folder '{ folder_name } ' created successfully." )
309
+ except OSError as e :
310
+ print (f"Error creating folder '{ folder_name } ': { e } " )
0 commit comments