Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for string matches #18

Merged
merged 7 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
fail-fast: false
matrix:
python_version:
- "3.10"
- "3.11"
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -35,7 +35,7 @@ jobs:

# Note: currently required to import snapshot utils from localstack
# TODO remove once imports are better organized in localstack
. .venv/bin/activate; pip install localstack-core[runtime]
. .venv/bin/activate; pip install localstack-core[test]

- name: Run tests
run: |
Expand Down
3 changes: 2 additions & 1 deletion airspeed/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def dict_to_string(obj: dict) -> str:
"contains": lambda self, value: value in self,
"indexOf": lambda self, ch: self.index(ch),
"substring": lambda self, start, end=None: self[start:end],
"matches": lambda self, pattern: bool(re.fullmatch(pattern, self)),
},
list: {
"size": lambda self: len(self),
Expand Down Expand Up @@ -455,7 +456,7 @@ def calculate(self, namespace, loader):


class StringLiteral(_Element):
STRING = re.compile(r"'((?:\\['nrbt\\\\\\$]|[^'\\])*)'(.*)", re.S)
STRING = re.compile(r"'([^']*)'(.*)", re.S)
ESCAPED_CHAR = re.compile(r"\\([nrbt'\\])")

def parse(self):
Expand Down
11 changes: 8 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from localstack.testing.aws.util import (
base_aws_client_factory,
base_aws_session,
primary_testing_aws_client,
base_testing_aws_client,
)

pytest_plugins = [
"localstack.testing.pytest.fixtures",
"localstack.testing.pytest.snapshot",
"localstack_snapshot.pytest.snapshot",
]


Expand All @@ -23,4 +23,9 @@ def aws_client_factory(aws_session):

@pytest.fixture(scope="session")
def aws_client(aws_client_factory):
return primary_testing_aws_client(aws_client_factory)
return base_testing_aws_client(aws_client_factory)


@pytest.fixture(scope="function")
def snapshot(_snapshot_session):
return _snapshot_session
36 changes: 35 additions & 1 deletion tests/test_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import pytest
import requests
import six
from localstack.constants import TEST_AWS_ACCOUNT_ID
from localstack.testing.aws.util import is_aws_cloud
from localstack.testing.config import TEST_AWS_ACCOUNT_ID
from localstack.testing.pytest import fixtures
from localstack.utils.archives import unzip
from localstack.utils.files import save_file
Expand Down Expand Up @@ -845,6 +845,40 @@ def test_dict_to_string(self, test_render):
template = "#set( $myObject = {'k1': 'v1', 'k2': 'v2'} )$myObject.toString()"
test_render(template)

def test_string_matches_true(self, test_render):
template = "#set( $myString = '123456789' )$myString.matches( '[0-9]*' )"
test_render(template)

def test_string_matches_false(self, test_render):
template = "#set( $myString = 'd123' )$myString.matches( '[0-9]*' )"
test_render(template)

def test_string_matches_full_date(self, test_render):
template = (
"#set( $myString = '2020-01-20T08:00:00.000Z' )"
"$myString.matches('^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:00\.000Z$')" # noqa
)
test_render(template)

template = (
'#set( $myString = "2020-01-20T08:00:00.000Z" )'
'$myString.matches("^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:00\.000Z$")' # noqa
)
test_render(template)

def test_string_with_escaped_char(self, test_render):
template = "#set( $myString = '\{\n\t\r\%\5' )$myString" # noqa
test_render(template)

template = '#set( $myString = "\{\n\t\r\%\5" )$myString' # noqa
test_render(template)

template = "'\{\n\t\r\%\5'" # noqa
test_render(template)

template = '"\{\n\t\r\%\5"' # noqa
test_render(template)


class TestInternals:
"""
Expand Down
36 changes: 36 additions & 0 deletions tests/test_templating.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -1068,5 +1068,41 @@
"render-result-1-cli": "\n \"has-value\"\n ",
"render-result-1": "\"does-not-have-value\""
}
},
"tests/test_templating.py::TestTemplating::test_string_matches_true": {
"recorded-date": "24-10-2024, 19:59:43",
"recorded-content": {
"render-result-1-cli": "true",
"render-result-1": "true"
}
},
"tests/test_templating.py::TestTemplating::test_string_matches_false": {
"recorded-date": "24-10-2024, 19:59:21",
"recorded-content": {
"render-result-1-cli": "false",
"render-result-1": "false"
}
},
"tests/test_templating.py::TestTemplating::test_string_with_escaped_char": {
"recorded-date": "24-10-2024, 22:14:20",
"recorded-content": {
"render-result-1-cli": "\\{\n\t\r\\%\u0005",
"render-result-1": "\\{\n\t\r\\%\u0005",
"render-result-2-cli": "\\{\n\t\r\\%\u0005",
"render-result-2": "\\{\n\t\r\\%\u0005",
"render-result-3-cli": "'\\{\n\t\r\\%\u0005'",
"render-result-3": "'\\{\n\t\r\\%\u0005'",
"render-result-4-cli": "\"\\{\n\t\r\\%\u0005\"",
"render-result-4": "\"\\{\n\t\r\\%\u0005\""
}
},
"tests/test_templating.py::TestTemplating::test_string_matches_full_date": {
"recorded-date": "24-10-2024, 21:58:12",
"recorded-content": {
"render-result-1-cli": "true",
"render-result-1": "true",
"render-result-2-cli": "true",
"render-result-2": "true"
}
}
}
Loading