Skip to content

Commit 49033de

Browse files
authored
Merge pull request #246 from tcdent/envfile
Make .env file parser more robust
2 parents c948832 + f8c8cb2 commit 49033de

3 files changed

Lines changed: 20 additions & 9 deletions

File tree

agentstack/generation/files.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Optional, Union
2+
import re
23
import string
34
import os, sys
45
import string
@@ -39,6 +40,9 @@ class EnvFile:
3940
```
4041
"""
4142

43+
# split the key-value pair on the first '=' character
44+
# allow spaces around the '=' character
45+
RE_PAIR = re.compile(r"^\s*([^\s=]+)\s*=\s*(.*)$")
4246
variables: dict[str, str]
4347

4448
def __init__(self, filename: str = ENV_FILENAME):
@@ -69,8 +73,13 @@ def parse_line(line) -> tuple[str, str]:
6973
Pairs are split on the first '=' character, and stripped of whitespace & quotes.
7074
Only the last occurrence of a variable is stored.
7175
"""
72-
key, value = line.split('=')
73-
return key.strip(), value.strip(string.whitespace + '"')
76+
match = self.RE_PAIR.match(line)
77+
78+
if not match:
79+
raise ValueError(f"Invalid line in .env file: {line}")
80+
81+
key, value = match.groups()
82+
return key, value.strip(' "')
7483

7584
if os.path.exists(conf.PATH / self._filename):
7685
with open(conf.PATH / self._filename, 'r') as f:

tests/fixtures/.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
ENV_VAR1=value1
33
ENV_VAR2=value_ignored
44
ENV_VAR2=value2
5-
#ENV_VAR3=""
5+
ENV_VAR3 = "12a34b===="
6+
#ENV_VAR4=""

tests/test_generation_files.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ def test_read_env(self):
8989
shutil.copy(BASE_PATH / "fixtures/.env", self.project_dir / ".env")
9090

9191
env = EnvFile()
92-
assert env.variables == {"ENV_VAR1": "value1", "ENV_VAR2": "value2"}
92+
assert env.variables == {"ENV_VAR1": "value1", "ENV_VAR2": "value2", "ENV_VAR3": "12a34b===="}
9393
assert env["ENV_VAR1"] == "value1"
9494
assert env["ENV_VAR2"] == "value2"
95+
assert env["ENV_VAR3"] == "12a34b===="
9596
with self.assertRaises(KeyError) as _:
9697
env["ENV_VAR100"]
9798

@@ -105,7 +106,7 @@ def test_write_env(self):
105106
tmp_data = open(self.project_dir / ".env").read()
106107
assert (
107108
tmp_data
108-
== """\nENV_VAR1=value1\nENV_VAR2=value_ignored\nENV_VAR2=value2\n#ENV_VAR3=""\nENV_VAR100=value2"""
109+
== """\nENV_VAR1=value1\nENV_VAR2=value_ignored\nENV_VAR2=value2\nENV_VAR3 = \"12a34b====\"\n#ENV_VAR4=""\nENV_VAR100=value2"""
109110
)
110111

111112
def test_write_env_numeric_that_can_be_boolean(self):
@@ -116,20 +117,20 @@ def test_write_env_numeric_that_can_be_boolean(self):
116117
env.append_if_new("ENV_VAR101", 1)
117118

118119
env = EnvFile() # re-read the file
119-
assert env.variables == {"ENV_VAR1": "value1", "ENV_VAR2": "value2", "ENV_VAR100": "0", "ENV_VAR101": "1"}
120+
assert env.variables == {"ENV_VAR1": "value1", "ENV_VAR2": "value2", "ENV_VAR3": "12a34b====", "ENV_VAR100": "0", "ENV_VAR101": "1"}
120121

121122
def test_write_env_commented(self):
122123
"""We should be able to write a commented-out value."""
123124
shutil.copy(BASE_PATH / "fixtures/.env", self.project_dir / ".env")
124125

125126
with EnvFile() as env:
126-
env.append_if_new("ENV_VAR3", "value3")
127+
env.append_if_new("ENV_VAR4", "value3")
127128

128129
env = EnvFile() # re-read the file
129-
assert env.variables == {"ENV_VAR1": "value1", "ENV_VAR2": "value2", "ENV_VAR3": "value3"}
130+
assert env.variables == {"ENV_VAR1": "value1", "ENV_VAR2": "value2", "ENV_VAR3": "12a34b====", "ENV_VAR4": "value3"}
130131

131132
tmp_file = open(self.project_dir / ".env").read()
132133
assert (
133134
tmp_file
134-
== """\nENV_VAR1=value1\nENV_VAR2=value_ignored\nENV_VAR2=value2\n#ENV_VAR3=""\nENV_VAR3=value3"""
135+
== """\nENV_VAR1=value1\nENV_VAR2=value_ignored\nENV_VAR2=value2\nENV_VAR3 = \"12a34b====\"\n#ENV_VAR4=""\nENV_VAR4=value3"""
135136
)

0 commit comments

Comments
 (0)