Skip to content

Commit

Permalink
refactor: init, generate_event and providers code move (aws#1685)
Browse files Browse the repository at this point in the history
* refactor: Move /samcli/local/init -> /samcli/lib/init

* refactor: rename @patch from 'samcli.local.init' to 'samcli.lib.init'

* refactor: fixing imports after automatic refactor

* refactor: fix imports for unit test

* refactor: remove extra __init__.py created by pycharm during automatic move

* refactor: move /samcli/commands/local/lib/generate_sample_events to /samcli/lib/generate_sample_events

* refactor: move init unit tests to right folder

* refactor: Move providers to lib folder

* Move `samcli/commands/local/lib/*providers.py` to `samcli/lib/providers/`
* Update imports
* Update patch annotations
* Move `InvalidLayerReference` to an exception.py file within the destination

* refactor: update manifest.in
  • Loading branch information
sanathkr authored Dec 18, 2019
1 parent 389830e commit 382a814
Showing 197 changed files with 251 additions and 261 deletions.
4 changes: 2 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
include LICENSE
include requirements/base.txt
include requirements/dev.txt
recursive-include samcli/local/init/templates *
recursive-include samcli/lib/init/templates *
recursive-include samcli/lib *.json
recursive-include samcli/commands/local/lib/generated_sample_events *.json
recursive-include samcli/lib/generated_sample_events *.json
prune tests

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ exclude = '''
| build
| dist
| pip-wheel-metadata
| samcli/local/init/templates
| samcli/lib/init/templates
| tests/integration/testdata
)/
)
38 changes: 21 additions & 17 deletions samcli/commands/build/build_context.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
import pathlib

from samcli.local.docker.manager import ContainerManager
from samcli.commands.local.lib.sam_function_provider import SamFunctionProvider
from samcli.lib.providers.sam_function_provider import SamFunctionProvider
from samcli.commands._utils.template import get_template_data
from samcli.commands.exceptions import UserException
from samcli.local.lambdafn.exceptions import FunctionNotFound
@@ -23,18 +23,20 @@ class BuildContext:
# This is usually a optimal permission for directories
_BUILD_DIR_PERMISSIONS = 0o755

def __init__(self,
function_identifier,
template_file,
base_dir,
build_dir,
mode,
manifest_path=None,
clean=False,
use_container=False,
parameter_overrides=None,
docker_network=None,
skip_pull_image=False):
def __init__(
self,
function_identifier,
template_file,
base_dir,
build_dir,
mode,
manifest_path=None,
clean=False,
use_container=False,
parameter_overrides=None,
docker_network=None,
skip_pull_image=False,
):

self._function_identifier = function_identifier
self._template_file = template_file
@@ -68,8 +70,9 @@ def __enter__(self):
self._build_dir = self._setup_build_dir(self._build_dir, self._clean)

if self._use_container:
self._container_manager = ContainerManager(docker_network_id=self._docker_network,
skip_pull_image=self._skip_pull_image)
self._container_manager = ContainerManager(
docker_network_id=self._docker_network, skip_pull_image=self._skip_pull_image
)

return self

@@ -143,8 +146,9 @@ def functions_to_build(self):

if not function:
all_functions = [f.name for f in self._function_provider.get_all()]
available_function_message = "{} not found. Possible options in your template: {}" \
.format(self._function_identifier, all_functions)
available_function_message = "{} not found. Possible options in your template: {}".format(
self._function_identifier, all_functions
)
LOG.info(available_function_message)
raise FunctionNotFound("Unable to find a Function with name '{}'".format(self._function_identifier))

5 changes: 2 additions & 3 deletions samcli/commands/init/init_generator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""
Cookiecutter-based generation logic for project templates.
"""
import click

from samcli.commands.exceptions import UserException
from samcli.local.init import generate_project
from samcli.local.init.exceptions import GenerateProjectFailedError, ArbitraryProjectDownloadFailed
from samcli.lib.init import generate_project
from samcli.lib.init.exceptions import GenerateProjectFailedError, ArbitraryProjectDownloadFailed


def do_generate(location, runtime, dependency_manager, output_dir, name, no_input, extra_context):
1 change: 0 additions & 1 deletion samcli/commands/init/interactive_init_flow.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@
"""
import tempfile
import logging
import traceback
import click

from botocore.exceptions import ClientError, WaiterError
2 changes: 1 addition & 1 deletion samcli/commands/local/cli_common/invoke_context.py
Original file line number Diff line number Diff line change
@@ -16,8 +16,8 @@
from samcli.local.docker.manager import ContainerManager
from samcli.commands._utils.template import get_template_data
from samcli.local.layers.layer_downloader import LayerDownloader
from samcli.lib.providers.sam_function_provider import SamFunctionProvider
from .user_exceptions import InvokeContextException, DebugContextException
from ..lib.sam_function_provider import SamFunctionProvider


class InvokeContext:
2 changes: 1 addition & 1 deletion samcli/commands/local/generate_event/event_generation.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

import click

import samcli.commands.local.lib.generated_sample_events.events as events
import samcli.lib.generated_sample_events.events as events
from samcli.cli.cli_config_file import TomlProvider, get_ctx_defaults
from samcli.cli.options import debug_option
from samcli.lib.telemetry.metrics import track_command
2 changes: 1 addition & 1 deletion samcli/commands/local/invoke/cli.py
Original file line number Diff line number Diff line change
@@ -110,7 +110,7 @@ def do_cli( # pylint: disable=R0914
"""

from samcli.commands.exceptions import UserException
from samcli.commands.local.lib.exceptions import InvalidLayerReference
from samcli.lib.providers.exceptions import InvalidLayerReference
from samcli.commands.local.cli_common.invoke_context import InvokeContext
from samcli.local.lambdafn.exceptions import FunctionNotFound
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
11 changes: 0 additions & 11 deletions samcli/commands/local/lib/exceptions.py
Original file line number Diff line number Diff line change
@@ -13,14 +13,3 @@ class OverridesNotWellDefinedError(Exception):
"""
Raised when the overrides file is invalid
"""


class InvalidLayerReference(Exception):
"""
Raised when the LayerVersion LogicalId does not exist in the template
"""

def __init__(self):
super(InvalidLayerReference, self).__init__(
"Layer References need to be of type " "'AWS::Serverless::LayerVersion' or 'AWS::Lambda::LayerVersion'"
)
2 changes: 1 addition & 1 deletion samcli/commands/local/lib/local_api_service.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

from samcli.commands.local.lib.exceptions import NoApisDefined
from samcli.local.apigw.local_apigw_service import LocalApigwService
from samcli.commands.local.lib.api_provider import ApiProvider
from samcli.lib.providers.api_provider import ApiProvider

LOG = logging.getLogger(__name__)

3 changes: 2 additions & 1 deletion samcli/commands/local/start_api/cli.py
Original file line number Diff line number Diff line change
@@ -110,7 +110,8 @@ def do_cli( # pylint: disable=R0914
"""

from samcli.commands.local.cli_common.invoke_context import InvokeContext
from samcli.commands.local.lib.exceptions import NoApisDefined, InvalidLayerReference
from samcli.commands.local.lib.exceptions import NoApisDefined
from samcli.lib.providers.exceptions import InvalidLayerReference
from samcli.commands.exceptions import UserException
from samcli.commands.local.lib.local_api_service import LocalApiService
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
2 changes: 1 addition & 1 deletion samcli/commands/local/start_lambda/cli.py
Original file line number Diff line number Diff line change
@@ -120,7 +120,7 @@ def do_cli( # pylint: disable=R0914

from samcli.commands.local.cli_common.invoke_context import InvokeContext
from samcli.commands.local.cli_common.user_exceptions import UserException
from samcli.commands.local.lib.exceptions import InvalidLayerReference
from samcli.lib.providers.exceptions import InvalidLayerReference
from samcli.commands.local.lib.local_lambda_service import LocalLambdaService
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
from cookiecutter.main import cookiecutter

from samcli.local.common.runtime_template import RUNTIME_DEP_TEMPLATE_MAPPING
from samcli.local.init.exceptions import GenerateProjectFailedError
from .exceptions import GenerateProjectFailedError
from .arbitrary_project import generate_non_cookiecutter_project

LOG = logging.getLogger(__name__)
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
import shutil
import logging

from pathlib import Path
from cookiecutter import repository
from cookiecutter import exceptions
from cookiecutter import config
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Tests cookiecutter baking process and rendered content
"""


def test_project_tree(cookies):
result = cookies.bake(extra_context={"project_name": "hello sam"})
assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == "hello sam"
assert result.project.isdir()
assert result.project.join(".gitignore").isfile()
assert result.project.join("template.yaml").isfile()
assert result.project.join("README.md").isfile()
assert result.project.join("src").isdir()
assert result.project.join("test").isdir()
assert result.project.join("src", "HelloWorld").isdir()
assert result.project.join("src", "HelloWorld", "HelloWorld.csproj").isfile()
assert result.project.join("src", "HelloWorld", "Function.cs").isfile()
assert result.project.join("src", "HelloWorld", "aws-lambda-tools-defaults.json").isfile()
assert result.project.join("test", "HelloWorld.Test", "FunctionTest.cs").isfile()
assert result.project.join("test", "HelloWorld.Test", "HelloWorld.Tests.csproj").isfile()


def test_app_content(cookies):
result = cookies.bake(extra_context={"project_name": "my_lambda"})
app_file = result.project.join("src", "HelloWorld", "Function.cs")
app_content = app_file.readlines()
app_content = "".join(app_content)

contents = ("GetCallingIP", "GetStringAsync", "location", "message", "hello world", "StatusCode")

for content in contents:
assert content in app_content
Original file line number Diff line number Diff line change
@@ -19,61 +19,58 @@ def inside_dir(dirpath):


def test_project_tree(cookies):
result = cookies.bake(extra_context={'project_name': 'test_project'})
result = cookies.bake(extra_context={"project_name": "test_project"})
assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == 'test_project'
assert result.project.basename == "test_project"

assert result.project.isdir()
assert result.project.join('README.md').isfile()
assert result.project.join('template.yaml').isfile()
assert result.project.join('hello-world').isdir()
assert result.project.join('hello-world', 'main.go').isfile()
assert result.project.join('hello-world', 'main_test.go').isfile()
assert result.project.join("README.md").isfile()
assert result.project.join("template.yaml").isfile()
assert result.project.join("hello-world").isdir()
assert result.project.join("hello-world", "main.go").isfile()
assert result.project.join("hello-world", "main_test.go").isfile()


def test_app_content(cookies):
result = cookies.bake(extra_context={'project_name': 'test_project'})
app_file = result.project.join('hello-world', 'main.go')
result = cookies.bake(extra_context={"project_name": "test_project"})
app_file = result.project.join("hello-world", "main.go")
app_content = app_file.readlines()
app_content = ''.join(app_content)
app_content = "".join(app_content)

contents = (
"github.com/aws/aws-lambda-go/events",
"resp, err := http.Get(DefaultHTTPGetAddress)",
"lambda.Start(handler)"
"lambda.Start(handler)",
)

for content in contents:
assert content in app_content


def test_app_test_content(cookies):
result = cookies.bake(extra_context={'project_name': 'test_project'})
app_file = result.project.join('hello-world', 'main_test.go')
result = cookies.bake(extra_context={"project_name": "test_project"})
app_file = result.project.join("hello-world", "main_test.go")
app_content = app_file.readlines()
app_content = ''.join(app_content)
app_content = "".join(app_content)

contents = (
"DefaultHTTPGetAddress = \"http://127.0.0.1:12345\"",
'DefaultHTTPGetAddress = "http://127.0.0.1:12345"',
"DefaultHTTPGetAddress = ts.URL",
"Successful Request"
"Successful Request",
)

for content in contents:
assert content in app_content


def test_app_template_content(cookies):
result = cookies.bake(extra_context={'project_name': 'test_project'})
app_file = result.project.join('template.yaml')
result = cookies.bake(extra_context={"project_name": "test_project"})
app_file = result.project.join("template.yaml")
app_content = app_file.readlines()
app_content = ''.join(app_content)
app_content = "".join(app_content)

contents = (
"Runtime: go1.x",
"HelloWorldFunction",
)
contents = ("Runtime: go1.x", "HelloWorldFunction")

for content in contents:
assert content in app_content
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Tests cookiecutter baking process and rendered content
"""


def test_project_tree(cookies):
result = cookies.bake(extra_context={"project_name": "hello sam"})
assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == "hello sam"
assert result.project.isdir()
assert result.project.join("template.yaml").isfile()
assert result.project.join("README.md").isfile()
assert result.project.join("src").isdir()
assert result.project.join("src", "main").isdir()
assert result.project.join("src", "main", "java").isdir()
assert result.project.join("src", "main", "java", "helloworld").isdir()
assert result.project.join("src", "main", "java", "helloworld", "App.java").isfile()
assert result.project.join("src", "main", "java", "helloworld", "GatewayResponse.java").isfile()
assert result.project.join("src", "test", "java").isdir()
assert result.project.join("src", "test", "java", "helloworld").isdir()
assert result.project.join("src", "test", "java", "helloworld", "AppTest.java").isfile()


def test_app_content(cookies):
result = cookies.bake(extra_context={"project_name": "my_lambda"})
app_file = result.project.join("src", "main", "java", "helloworld", "App.java")
app_content = app_file.readlines()
app_content = "".join(app_content)

contents = (
"package helloword",
"class App implements RequestHandler<Object, Object>",
"https://checkip.amazonaws.com",
"return new GatewayResponse",
"getPageContents",
)

for content in contents:
assert content in app_content
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Tests cookiecutter baking process and rendered content
"""


def test_project_tree(cookies):
result = cookies.bake(extra_context={"project_name": "hello sam"})
assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == "hello sam"
assert result.project.isdir()
assert result.project.join("template.yaml").isfile()
assert result.project.join("README.md").isfile()
assert result.project.join("src").isdir()
assert result.project.join("src", "main").isdir()
assert result.project.join("src", "main", "java").isdir()
assert result.project.join("src", "main", "java", "helloworld").isdir()
assert result.project.join("src", "main", "java", "helloworld", "App.java").isfile()
assert result.project.join("src", "main", "java", "helloworld", "GatewayResponse.java").isfile()
assert result.project.join("src", "test", "java").isdir()
assert result.project.join("src", "test", "java", "helloworld").isdir()
assert result.project.join("src", "test", "java", "helloworld", "AppTest.java").isfile()


def test_app_content(cookies):
result = cookies.bake(extra_context={"project_name": "my_lambda"})
app_file = result.project.join("src", "main", "java", "helloworld", "App.java")
app_content = app_file.readlines()
app_content = "".join(app_content)

contents = (
"package helloword",
"class App implements RequestHandler<Object, Object>",
"https://checkip.amazonaws.com",
"return new GatewayResponse",
"getPageContents",
)

for content in contents:
assert content in app_content
Loading

0 comments on commit 382a814

Please sign in to comment.