Skip to content

Commit bc27829

Browse files
joncottonshin-
authored andcommitted
Fix parsing for an environment file with newlines
Fixes the new, purposely broken test added in the previous commit. Signed-off-by: Jon Cotton <[email protected]>
1 parent fce9892 commit bc27829

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

docker/utils/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,11 @@ def parse_env_file(env_file):
10691069
if line[0] == '#':
10701070
continue
10711071

1072-
parse_line = line.strip().split('=', 1)
1072+
line = line.strip()
1073+
if not line:
1074+
continue
1075+
1076+
parse_line = line.split('=', 1)
10731077
if len(parse_line) == 2:
10741078
k, v = parse_line
10751079
environment[k] = v

tests/unit/utils_test.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,18 @@ def test_parse_env_file_with_equals_character(self):
467467
def test_parse_env_file_commented_line(self):
468468
env_file = self.generate_tempfile(
469469
file_content='USER=jdoe\n#PASS=secret')
470-
get_parse_env_file = parse_env_file((env_file))
470+
get_parse_env_file = parse_env_file(env_file)
471471
self.assertEqual(get_parse_env_file, {'USER': 'jdoe'})
472472
os.unlink(env_file)
473473

474+
def test_parse_env_file_newline(self):
475+
env_file = self.generate_tempfile(
476+
file_content='\nUSER=jdoe\n\n\nPASS=secret')
477+
get_parse_env_file = parse_env_file(env_file)
478+
self.assertEqual(get_parse_env_file,
479+
{'USER': 'jdoe', 'PASS': 'secret'})
480+
os.unlink(env_file)
481+
474482
def test_parse_env_file_invalid_line(self):
475483
env_file = self.generate_tempfile(
476484
file_content='USER jdoe')

0 commit comments

Comments
 (0)