Skip to content

Commit

Permalink
Merge pull request #7786 from RasaHQ/4311_is_domain_raises
Browse files Browse the repository at this point in the history
Added (try..except) block for is_domain_file
  • Loading branch information
federicotdn authored Jan 25, 2021
2 parents ca12efb + fb92089 commit 149c19f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/4311.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug with `Domain.is_domain_file()` that could raise an Exception in case the potential domain file is not a valid YAML.
8 changes: 6 additions & 2 deletions rasa/shared/core/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import rasa.shared.constants
import rasa.shared.core.constants
from rasa.shared.exceptions import RasaException, YamlException
from rasa.shared.exceptions import RasaException, YamlException, YamlSyntaxException
from rasa.shared.utils.validation import YamlValidationException
import rasa.shared.nlu.constants
import rasa.shared.utils.validation
Expand Down Expand Up @@ -1579,7 +1579,11 @@ def is_domain_file(filename: Text) -> bool:
if not is_likely_yaml_file(filename):
return False

content = rasa.shared.utils.io.read_yaml_file(filename)
try:
content = rasa.shared.utils.io.read_yaml_file(filename)
except (ValueError, YamlSyntaxException):
return False

return any(key in content for key in ALL_DOMAIN_KEYS)

def slot_mapping_for_form(self, form_name: Text) -> Dict[Text, Any]:
Expand Down
33 changes: 33 additions & 0 deletions tests/shared/core/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,3 +1287,36 @@ def test_responses_text_multiline_is_preserved():

domain = Domain.from_yaml(test_yaml)
assert domain.as_yaml(clean_before_dump=True) == test_yaml


def test_is_valid_domain_doesnt_raise_with_valid_domain(tmpdir: Path):
domain_path = str(tmpdir / "domain.yml")
rasa.shared.utils.io.write_text_file(
"""
responses:
utter_greet:
- text: hey there! """,
domain_path,
)
assert Domain.is_domain_file(domain_path)


def test_is_valid_domain_doesnt_raise_with_invalid_domain(tmpdir: Path):
domain_path = str(tmpdir / "domain.yml")
rasa.shared.utils.io.write_text_file(
"""
invalid""",
domain_path,
)
assert not Domain.is_domain_file(domain_path)


def test_is_valid_domain_doesnt_raise_with_invalid_yaml(tmpdir: Path):
potential_domain_path = str(tmpdir / "domain.yml")
rasa.shared.utils.io.write_text_file(
"""
script:
- echo "Latest SDK version is ${RASA_SDK_VERSION}""",
potential_domain_path,
)
assert not Domain.is_domain_file(potential_domain_path)

0 comments on commit 149c19f

Please sign in to comment.