1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414import os
15+ import shutil
1516import unittest
17+ from pathlib import Path
1618from unittest .mock import patch , ANY
1719from click .testing import CliRunner
1820from library_generation .cli .entry_point import (
@@ -49,6 +51,20 @@ def test_entry_point_with_invalid_config_raise_file_exception(self):
4951 self .assertEqual (FileNotFoundError , result .exc_info [0 ])
5052 self .assertRegex (result .exception .args [0 ], "/non-existent/file does not exist." )
5153
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+
5268 def test_validate_generation_config_succeeds (
5369 self ,
5470 ):
@@ -94,6 +110,7 @@ def test_generate_non_monorepo_without_library_names_full_generation(
94110 # does special handling when a method is annotated with @main.command()
95111 generate_impl (
96112 generation_config_path = config_path ,
113+ generation_input = None ,
97114 library_names = None ,
98115 repository_path = "." ,
99116 api_definitions_path = "." ,
@@ -122,6 +139,7 @@ def test_generate_non_monorepo_with_library_names_full_generation(
122139 # does special handling when a method is annotated with @main.command()
123140 generate_impl (
124141 generation_config_path = config_path ,
142+ generation_input = None ,
125143 library_names = "non-existent-library" ,
126144 repository_path = "." ,
127145 api_definitions_path = "." ,
@@ -150,6 +168,7 @@ def test_generate_monorepo_with_common_protos_without_library_names_triggers_ful
150168 # does special handling when a method is annotated with @main.command()
151169 generate_impl (
152170 generation_config_path = config_path ,
171+ generation_input = None ,
153172 library_names = None ,
154173 repository_path = "." ,
155174 api_definitions_path = "." ,
@@ -177,6 +196,7 @@ def test_generate_monorepo_with_common_protos_with_library_names_triggers_full_g
177196 # does special handling when a method is annotated with @main.command()
178197 generate_impl (
179198 generation_config_path = config_path ,
199+ generation_input = None ,
180200 library_names = "iam,non-existent-library" ,
181201 repository_path = "." ,
182202 api_definitions_path = "." ,
@@ -206,6 +226,7 @@ def test_generate_monorepo_without_library_names_trigger_full_generation(
206226 # does special handling when a method is annotated with @main.command()
207227 generate_impl (
208228 generation_config_path = config_path ,
229+ generation_input = None ,
209230 library_names = None ,
210231 repository_path = "." ,
211232 api_definitions_path = "." ,
@@ -235,6 +256,7 @@ def test_generate_monorepo_with_library_names_trigger_selective_generation(
235256 # does special handling when a method is annotated with @main.command()
236257 generate_impl (
237258 generation_config_path = config_path ,
259+ generation_input = None ,
238260 library_names = "asset" ,
239261 repository_path = "." ,
240262 api_definitions_path = "." ,
@@ -245,3 +267,44 @@ def test_generate_monorepo_with_library_names_trigger_selective_generation(
245267 api_definitions_path = ANY ,
246268 target_library_names = ["asset" ],
247269 )
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