Repository: gabrieldemarmiesse/python-on-whales (language: python)

Issue:
Incorrect stack env file parsing
If the value of the variable in the env file contains the symbol `=`, then the parsing of the line ends with an error
Affected version: 0.23.0

affects to the `stack` component only:
https://github.com/gabrieldemarmiesse/python-on-whales/blob/dc4e4feb6c032bf2164c426e46c31d85df1ea99c/python_on_whales/components/stack/cli_wrapper.py#L83

How to reproduce:

```
# envfile
MYVAR="value"
MYARGS="--tls=true"
```

```
Python 3.9.6 (default, Jul 19 2021, 16:40:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from python_on_whales import docker
>>> docker.stack.deploy(name='mystack', env_files=['envfile'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/src/example/lib/python3.9/site-packages/python_on_whales/components/stack/cli_wrapper.py", line 83, in deploy
    env = read_env_files([Path(x) for x in env_files])
  File "/home/user/src/example/lib/python3.9/site-packages/python_on_whales/utils.py", line 246, in read_env_files
    result_dict.update(read_env_file(file))
  File "/home/user/src/example/lib/python3.9/site-packages/python_on_whales/utils.py", line 238, in read_env_file
    key, value = line.split("=")
ValueError: too many values to unpack (expected 2)
```

How to fix:

https://github.com/gabrieldemarmiesse/python-on-whales/blob/dc4e4feb6c032bf2164c426e46c31d85df1ea99c/python_on_whales/utils.py#L239

split line into key=value one time only:

`key, value = line.split("=", 1)`


Discussion / hints:

Fix patch:
diff --git a/python_on_whales/utils.py b/python_on_whales/utils.py
index 6fa0abec..ba737a72 100644
--- a/python_on_whales/utils.py
+++ b/python_on_whales/utils.py
@@ -236,7 +236,7 @@ def read_env_file(env_file: Path) -> Dict[str, str]:
         if not line:
             continue
         line = line.strip()
-        key, value = line.split("=")
+        key, value = line.split("=", 1)
         result_dict[key] = value
     return result_dict
 


Test patch:
diff --git a/tests/python_on_whales/components/test_stack.py b/tests/python_on_whales/components/test_stack.py
index c109888c..fd0377cb 100644
--- a/tests/python_on_whales/components/test_stack.py
+++ b/tests/python_on_whales/components/test_stack.py
@@ -63,7 +63,7 @@ def test_stack_variables():
 @pytest.mark.usefixtures("swarm_mode")
 def test_stack_env_files(tmp_path: Path):
     env_file = tmp_path / "some.env"
-    env_file.write_text("SOME_VARIABLE=hello-world # some var \n # some comment")
+    env_file.write_text('SOME_VARIABLE="--tls=true" # some var \n # some comment')
     third_stack = docker.stack.deploy(
         "third_stack",
         [PROJECT_ROOT / "tests/python_on_whales/components/test-stack-file.yml"],
@@ -71,7 +71,7 @@ def test_stack_env_files(tmp_path: Path):
     )
 
     agent_service = docker.service.inspect("third_stack_agent")
-    expected = "SOME_OTHER_VARIABLE=hello-world"
+    expected = 'SOME_OTHER_VARIABLE="--tls=true"'
     assert expected in agent_service.spec.task_template.container_spec.env
 
     assert docker.stack.list() == [third_stack]