def do_generate(location, runtime, dependency_manager, output_dir, name, no_input, extra_context): try: generate_project(location, runtime, dependency_manager, output_dir, name, no_input, extra_context) except (GenerateProjectFailedError, ArbitraryProjectDownloadFailed) as e: raise UserException(str(e))
def test_init_arbitrary_project_with_location_is_not_cookiecutter( self, generate_non_cookiecutter_project_mock, cookiecutter_mock ): cookiecutter_mock.side_effect = RepositoryNotFound("msg") generate_project(location=self.location, output_dir=self.output_dir) generate_non_cookiecutter_project_mock.assert_called_with(location=self.location, output_dir=self.output_dir)
def do_cli(ctx, location, runtime, dependency_manager, output_dir, name, no_input): """ Implementation of the ``cli`` method, just separated out for unit testing purposes """ from samcli.commands.exceptions import UserException from samcli.local.init import generate_project from samcli.local.init.exceptions import GenerateProjectFailedError LOG.debug("Init command") click.secho("[+] Initializing project structure...", fg="green") no_build_msg = """ Project generated: {output_dir}/{name} Steps you can take next within the project folder =================================================== [*] Invoke Function: sam local invoke HelloWorldFunction --event event.json [*] Start API Gateway locally: sam local start-api """.format( output_dir=output_dir, name=name ) build_msg = """ Project generated: {output_dir}/{name} Steps you can take next within the project folder =================================================== [*] Install dependencies [*] Invoke Function: sam local invoke HelloWorldFunction --event event.json [*] Start API Gateway locally: sam local start-api """.format( output_dir=output_dir, name=name ) no_build_step_required = ( "python", "python3.7", "python3.6", "python2.7", "nodejs", "nodejs4.3", "nodejs6.10", "nodejs8.10", "nodejs10.x", "ruby2.5", ) next_step_msg = no_build_msg if runtime in no_build_step_required else build_msg try: generate_project(location, runtime, dependency_manager, output_dir, name, no_input) if not location: click.secho(next_step_msg, bold=True) click.secho("Read {name}/README.md for further instructions\n".format(name=name), bold=True) click.secho("[*] Project initialization is now complete", fg="green") except GenerateProjectFailedError as e: raise UserException(str(e))
def test_must_not_set_extra_content(self, cookiecutter_patch): custom_location = "mylocation" generate_project(location=custom_location, runtime=self.runtime, output_dir=self.output_dir, name=self.name, no_input=False) # THEN we should receive no errors cookiecutter_patch.assert_called_once_with( template=custom_location, no_input=False, output_dir=self.output_dir)
def test_init_arbitrary_project_with_named_folder(self, generate_non_cookiecutter_project_mock, cookiecutter_mock): cookiecutter_mock.side_effect = RepositoryNotFound("msg") generate_project(location=self.location, output_dir=self.output_dir, name=self.name) expected_output_dir = str(Path(self.output_dir, self.name)) generate_non_cookiecutter_project_mock.assert_called_with( location=self.location, output_dir=expected_output_dir )
def test_init_successful(self, cookiecutter_patch): # GIVEN generate_project successfully created a project # WHEN a project name has been passed generate_project( location=self.location, runtime=self.runtime, output_dir=self.output_dir, name=self.name, no_input=self.no_input) # THEN we should receive no errors cookiecutter_patch.assert_called_once_with( extra_context=self.extra_context, no_input=self.no_input, output_dir=self.output_dir, template=self.template)
def test_must_set_cookiecutter_context_when_location_and_extra_context_is_provided(self, cookiecutter_patch): cookiecutter_context = {"key1": "value1", "key2": "value2"} custom_location = "mylocation" generate_project( location=custom_location, output_dir=self.output_dir, no_input=False, extra_context=cookiecutter_context ) # THEN we should receive no errors cookiecutter_patch.assert_called_once_with( extra_context=cookiecutter_context, template=custom_location, no_input=False, output_dir=self.output_dir )
def test_must_not_set_extra_content(self, cookiecutter_patch): custom_location = "mylocation" generate_project(location=custom_location, runtime=self.runtime, output_dir=self.output_dir, name=self.name, no_input=False) # THEN we should receive no errors cookiecutter_patch.assert_called_once_with(template=custom_location, no_input=False, output_dir=self.output_dir)
def test_init_error_with_non_compatible_dependency_manager(self): with self.assertRaises(GenerateProjectFailedError) as ctx: generate_project(location=self.location, runtime=self.runtime, dependency_manager="gradle", output_dir=self.output_dir, name=self.name, no_input=self.no_input) self.assertEquals( "An error occurred while generating this " "testing project: Lambda Runtime python3.6 " "does not support dependency manager: gradle", str(ctx.exception))
def test_must_not_set_name_when_location_is_given(self, cookiecutter_patch): generate_project(runtime=self.runtime, output_dir=self.output_dir, name=self.name, no_input=False) expected_extra_content = { "project_name": self.name, "runtime": self.runtime } # THEN we should receive no errors cookiecutter_patch.assert_called_once_with( template=self.template, extra_context=expected_extra_content, no_input=True, output_dir=self.output_dir)
def test_init_successful_with_no_dep_manager(self, cookiecutter_patch): generate_project(location=self.location, runtime=self.runtime, dependency_manager=None, output_dir=self.output_dir, name=self.name, no_input=self.no_input) # THEN we should receive no errors cookiecutter_patch.assert_called_once_with( extra_context=self.extra_context, no_input=self.no_input, output_dir=self.output_dir, template=self.template)
def test_init_successful(self, cookiecutter_patch): # GIVEN generate_project successfully created a project # WHEN a project name has been passed generate_project(location=self.location, runtime=self.runtime, output_dir=self.output_dir, name=self.name, no_input=self.no_input) # THEN we should receive no errors cookiecutter_patch.assert_called_once_with( extra_context=self.extra_context, no_input=self.no_input, output_dir=self.output_dir, template=self.template)
def test_when_generate_project_returns_error(self, cookiecutter_patch): # GIVEN generate_project fails to create a project ex = CookiecutterException("something is wrong") cookiecutter_patch.side_effect = ex expected_msg = str(GenerateProjectFailedError(project=self.name, provider_error=ex)) # WHEN generate_project returns an error # THEN we should receive a GenerateProjectFailedError Exception with self.assertRaises(GenerateProjectFailedError) as ctx: generate_project( location=self.location, runtime=self.runtime, output_dir=self.output_dir, name=self.name, no_input=self.no_input) self.assertEquals(expected_msg, str(ctx.exception))
def do_generate(location, runtime, dependency_manager, output_dir, name, no_input, extra_context): no_build_msg = """ Project generated: {output_dir}/{name} Steps you can take next within the project folder =================================================== [*] Invoke Function: sam local invoke HelloWorldFunction --event event.json [*] Start API Gateway locally: sam local start-api """.format( output_dir=output_dir, name=name ) build_msg = """ Project generated: {output_dir}/{name} Steps you can take next within the project folder =================================================== [*] Install dependencies [*] Invoke Function: sam local invoke HelloWorldFunction --event event.json [*] Start API Gateway locally: sam local start-api """.format( output_dir=output_dir, name=name ) no_build_step_required = ( "python", "python3.7", "python3.6", "python2.7", "nodejs", "nodejs4.3", "nodejs6.10", "nodejs8.10", "nodejs10.x", "ruby2.5", ) next_step_msg = no_build_msg if runtime in no_build_step_required else build_msg try: generate_project(location, runtime, dependency_manager, output_dir, name, no_input, extra_context) if not location: click.secho(next_step_msg, bold=True) click.secho("Read {name}/README.md for further instructions\n".format(name=name), bold=True) click.secho("[*] Project initialization is now complete", fg="green") except GenerateProjectFailedError as e: raise UserException(str(e))
def do_cli(ctx, location, runtime, output_dir, name, no_input): """ Implementation of the ``cli`` method, just separated out for unit testing purposes """ LOG.debug("Init command") click.secho("[+] Initializing project structure...", fg="green") try: generate_project(location, runtime, output_dir, name, no_input) # Custom templates can implement their own visual cues so let's not repeat the message if not location: click.secho( "[SUCCESS] - Read {name}/README.md for further instructions on how to proceed" .format(name=name), bold=True) click.secho("[*] Project initialization is now complete", fg="green") except GenerateProjectFailedError as e: raise UserException(str(e))
def test_must_set_cookiecutter_context_when_app_template_is_provided(self, cookiecutter_patch): cookiecutter_context = {"key1": "value1", "key2": "value2"} generate_project( runtime=self.runtime, dependency_manager=self.dependency_manager, output_dir=self.output_dir, name=self.name, no_input=self.no_input, extra_context=cookiecutter_context, ) # THEN we should receive no errors cookiecutter_patch.assert_called_once_with( extra_context=cookiecutter_context, no_input=self.no_input, output_dir=self.output_dir, template=self.template, )
def test_when_generate_project_returns_error(self, cookiecutter_patch): # GIVEN generate_project fails to create a project ex = CookiecutterException("something is wrong") cookiecutter_patch.side_effect = ex expected_msg = str( GenerateProjectFailedError(project=self.name, provider_error=ex)) # WHEN generate_project returns an error # THEN we should receive a GenerateProjectFailedError Exception with self.assertRaises(GenerateProjectFailedError) as ctx: generate_project(location=self.location, runtime=self.runtime, output_dir=self.output_dir, name=self.name, no_input=self.no_input) self.assertEquals(expected_msg, str(ctx.exception))
def do_cli(ctx, location, runtime, output_dir, name, no_input): """ Implementation of the ``cli`` method, just separated out for unit testing purposes """ LOG.debug("Init command") click.secho("[+] Initializing project structure...", fg="green") no_build_msg = """ Project generated: {output_dir}/{name} Steps you can take next within the project folder =================================================== [*] Invoke Function: sam local invoke HelloWorldFunction --event event.json [*] Start API Gateway locally: sam local start-api """.format(output_dir=output_dir, name=name) build_msg = """ Project generated: {output_dir}/{name} Steps you can take next within the project folder =================================================== [*] Install dependencies [*] Invoke Function: sam local invoke HelloWorldFunction --event event.json [*] Start API Gateway locally: sam local start-api """.format(output_dir=output_dir, name=name) no_build_step_required = ( "python", "python3.7", "python3.6", "python2.7", "nodejs", "nodejs4.3", "nodejs6.10", "nodejs8.10", "ruby2.5") next_step_msg = no_build_msg if runtime in no_build_step_required else build_msg try: generate_project(location, runtime, output_dir, name, no_input) if not location: click.secho(next_step_msg, bold=True) click.secho("Read {name}/README.md for further instructions\n".format(name=name), bold=True) click.secho("[*] Project initialization is now complete", fg="green") except GenerateProjectFailedError as e: raise UserException(str(e))