Repository: valohai/valohai-cli

Issue:
CLI does not honor default environment variables in step
They're probably currently only populated into the GUI.

Comments:

Patch:
diff --git a/valohai_cli/commands/execution/run/dynamic_run_command.py b/valohai_cli/commands/execution/run/dynamic_run_command.py
index 38cf466..02eae5f 100644
--- a/valohai_cli/commands/execution/run/dynamic_run_command.py
+++ b/valohai_cli/commands/execution/run/dynamic_run_command.py
@@ -80,6 +80,9 @@ class RunCommand(click.Command):
             self.params.append(self.convert_param_to_option(parameter))
         for input in step.inputs.values():
             self.params.append(self.convert_input_to_option(input))
+        for name, value in step.environment_variables.items():
+            if name not in self.environment_variables:
+                self.environment_variables[name] = value.default
 
     def convert_param_to_option(self, parameter):
         """
diff --git a/valohai_cli/utils/__init__.py b/valohai_cli/utils/__init__.py
index 68053a8..4ba3f90 100644
--- a/valohai_cli/utils/__init__.py
+++ b/valohai_cli/utils/__init__.py
@@ -184,10 +184,6 @@ def parse_environment_variable_strings(envvar_strings):
     environment_variables = {}
     for string in envvar_strings:
         key, _, value = string.partition('=')
-        if not value:
-            raise ValueError('Environment variable specification {string} must be in the format KEY=VALUE'.format(
-                string=string,
-            ))
         key = key.strip()
         if not key:
             continue


Test patch:
diff --git a/tests/commands/execution/test_run.py b/tests/commands/execution/test_run.py
index 89b66c1..fd9e5ea 100644
--- a/tests/commands/execution/test_run.py
+++ b/tests/commands/execution/test_run.py
@@ -98,7 +98,7 @@ def test_run_requires_step(runner, logged_in_and_linked):
 @pytest.mark.parametrize('pass_param', (False, True))
 @pytest.mark.parametrize('pass_input', (False, True))
 @pytest.mark.parametrize('pass_env', (False, True))
-@pytest.mark.parametrize('pass_env_var', (False, True))
+@pytest.mark.parametrize('pass_env_var', (None, 'custom', 'override-default'))
 @pytest.mark.parametrize('adhoc', (False, True), ids=('regular', 'adhoc'))
 def test_run(runner, logged_in_and_linked, monkeypatch, pass_param, pass_input, pass_env, pass_env_var, adhoc):
     project_id = PROJECT_DATA['id']
@@ -130,10 +130,16 @@ def test_run(runner, logged_in_and_linked, monkeypatch, pass_param, pass_input,
         args.extend(['-v', 'greeting=hello'])
         args.extend(['--var', 'enable=1'])
         args.extend(['-vdebug=yes'])
+        if pass_env_var == 'override-default':
+            args.extend(['--var', 'testenvvar='])
+            expected_testenvvar = ''
+        else:
+            expected_testenvvar = 'test'  # default from YAML
         values['environment_variables'] = {
             'greeting': 'hello',
             'enable': '1',
             'debug': 'yes',
+            'testenvvar': expected_testenvvar,
         }
     with RunAPIMock(project_id, commit_id, values):
         output = runner.invoke(run, args, catch_exceptions=False).output
diff --git a/tests/fixture_data.py b/tests/fixture_data.py
index b8f4fa6..d013318 100644
--- a/tests/fixture_data.py
+++ b/tests/fixture_data.py
@@ -121,6 +121,9 @@ CONFIG_YAML = """
         description: Number of steps to run the trainer
         type: integer
         default: 300
+    environment-variables:
+      - name: testenvvar
+        default: 'test'
 
 """