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

Syntax errors branch #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
55 changes: 38 additions & 17 deletions env_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class EnvSpecSyntaxError(Exception):
Excpetion used for syntax errors.
"""

pass
def __init__(self, message, line):
super().__init__(message)
self.line = line


valid_types_list = [
Expand Down Expand Up @@ -124,50 +126,59 @@ def parse(env_spec_text):
comment_regex = r"^(.+)\#(.+)$"

lines = env_spec_text.split("\n")
print(lines)
enumerated_list = enumerate(lines)

for line in lines:
for line in enumerated_list:
line_number = line[0]
env_spec_line = line[1]
env_spec_entry = {}

line_comment_match = re.match(line_comment_regex, line)
line_comment_match = re.match(line_comment_regex, env_spec_line)

if line_comment_match:
continue

env_spec_entry["comment"] = None
comment_match = re.match(comment_regex, line)
comment_match = re.match(comment_regex, env_spec_line)

if comment_match:
line = comment_match.groups()[0]
env_spec_line = comment_match.groups()[0]
comment = comment_match.groups()[1]
env_spec_entry["comment"] = comment

name_match = re.match(name_regex, line)
name_match = re.match(name_regex, env_spec_line)

if name_match:
name = name_match.groups()[0]
line = name_match.groups()[1]
env_spec_line = name_match.groups()[1]

is_variable_name_valid = re.match(
alphanumeric_that_does_not_start_with_digit, name
)

if not is_variable_name_valid:
raise EnvSpecSyntaxError("SYNTAX ERROR: Invalid variable name.")
raise EnvSpecSyntaxError(
"Invalid variable name; it should contain only latin alphanumeric characters, underscores and not start with a digit.",
line_number,
)

env_spec_entry["name"] = name
env_spec_entry["choices"] = None
env_spec_entry["default_value"] = None

choices_match = re.match(choices_regex, line)
default_value_match = re.match(default_values_regex, line)
choices_match = re.match(choices_regex, env_spec_line)
default_value_match = re.match(default_values_regex, env_spec_line)

if choices_match:
choices_str = choices_match.groups()[0]

choices = create_list(choices_str)

if not choices:
raise EnvSpecSyntaxError("SYNTAX ERROR: Invalid choices list.")
raise EnvSpecSyntaxError(
"Invalid restricted choices syntax.", line_number
)

env_spec_entry["choices"] = choices

Expand All @@ -177,32 +188,42 @@ def parse(env_spec_text):

if choices_match:
if default_value not in choices or default_value == "":
raise EnvSpecSyntaxError("SYNTAX ERROR: Invalid default value.")
raise EnvSpecSyntaxError(
"Invalid default value; it is not included in the provided restricted choices.",
line_number,
)

env_spec_entry["default_value"] = default_value

if not choices_match:
if default_value_match:
env_spec_type = default_value_match.groups()[0]
else:
env_spec_type = line
env_spec_type = env_spec_line

env_spec_type = env_spec_type.strip()

if env_spec_type not in valid_types_list:
raise EnvSpecSyntaxError("SYNTAX ERROR: Invalid type.")
raise EnvSpecSyntaxError(
"Invalid variable type; it should be one of: \n"
+ (", ".join(map(str, valid_types_list))),
line_number,
)

env_spec_entry["type"] = env_spec_type
else:
env_spec_entry["type"] = "text"
else:
name = line.strip()
name = env_spec_line.strip()
is_variable_name_valid = re.match(
alphanumeric_that_does_not_start_with_digit, name
)

if not is_variable_name_valid:
raise EnvSpecSyntaxError("SYNTAX ERROR: Invalid variable name.")
raise EnvSpecSyntaxError(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the error message to this please?

Invalid variable name; it should contain only latin alphanumeric characters, underscores and not start with a digit.

Also, why are we validating the variable name in two places?

"Invalid variable name; it should contain only latin alphanumeric characters, underscores and not start with a digit.",
line_number,
)

env_spec_entry["name"] = name
env_spec_entry["type"] = "text"
Expand Down Expand Up @@ -230,5 +251,5 @@ def main():


if __name__ == "__main__":
spec_str = "# This line will be ignored\nADMIN_EMAIL: email # This email will be notified for occurring errors"
spec_str = "DATABASE_URL: testing"
print(main())
17 changes: 13 additions & 4 deletions env_spec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
def test_invalid_input_non_latin():
with pytest.raises(env_spec.EnvSpecSyntaxError) as excinfo:
env_spec.parse("dαtabase_url")
assert "SYNTAX ERROR: Invalid variable name." in str(excinfo.value)
assert (
"Invalid variable name; it should contain only latin alphanumeric characters, underscores and not start with a digit."
in str(excinfo.value)
)


def test_invalid_input_start_with_digit():
with pytest.raises(env_spec.EnvSpecSyntaxError) as excinfo:
env_spec.parse("1DATABASE_URL: url")
assert "SYNTAX ERROR: Invalid variable name." in str(excinfo.value)
assert (
"Invalid variable name; it should contain only latin alphanumeric characters, underscores and not start with a digit."
in str(excinfo.value)
)


def test_non_existed_type():
with pytest.raises(env_spec.EnvSpecSyntaxError) as excinfo:
env_spec.parse("DATABASE_URL: testing")
assert "SYNTAX ERROR: Invalid type." in str(excinfo.value)
assert "Invalid variable type; it should be one of: " in str(excinfo.value)


def test_valid_input_types():
Expand Down Expand Up @@ -56,7 +62,10 @@ def test_wrong_default_values():
env_spec.parse(
"DEBUG: [0,1]=1\nENVIRONMENT: [production,staging,development]=develoent"
)
assert "SYNTAX ERROR: Invalid default value." in str(excinfo.value)
assert (
"Invalid default value; it is not included in the provided restricted choices."
in str(excinfo.value)
)


def test_default_values():
Expand Down
8 changes: 8 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
lines = [
"DEBUG: [0,1]= 1",
"ENVIRONMENT: [production,staging,development]= development",
]
enumerated = enumerate(lines)

for line in enumerated:
print(line[0])