Skip to content
Merged
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
18 changes: 14 additions & 4 deletions gradient/cli/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get_option_name(options_strings):
class ReadValueFromConfigFile(click.Parameter):
def handle_parse_result(self, ctx, opts, args):
config_file = ctx.params.get(OPTIONS_FILE_PARAMETER_NAME)
if config_file:
if self.should_read_value_from_file(opts, args, config_file):
with open(config_file) as f:
config_data = yaml.load(f, Loader=yaml.FullLoader)
option_name = get_option_name(self.opts)
Expand All @@ -89,8 +89,15 @@ def handle_parse_result(self, ctx, opts, args):

opts[self.name] = value

return super(ReadValueFromConfigFile, self).handle_parse_result(
rv = super(ReadValueFromConfigFile, self).handle_parse_result(
ctx, opts, args)
return rv

def should_read_value_from_file(self, opts, args, config_file):
"""
:rtype: bool
"""
raise NotImplementedError


class ColorExtrasInCommandHelpMixin(object):
Expand Down Expand Up @@ -127,11 +134,13 @@ def _color_str(self, s):


class GradientArgument(ColorExtrasInCommandHelpMixin, ReadValueFromConfigFile, click.Argument):
pass
def should_read_value_from_file(self, opts, args, config_file):
return opts.get(self.name) in (None, ()) and config_file


class GradientOption(ColorExtrasInCommandHelpMixin, ReadValueFromConfigFile, click.Option):
pass
def should_read_value_from_file(self, opts, args, config_file):
return self.name not in opts and config_file


api_key_option = click.option(
Expand Down Expand Up @@ -207,6 +216,7 @@ def options_file(f):
click.option(
"--" + OPTIONS_FILE_OPTION_NAME,
OPTIONS_FILE_PARAMETER_NAME,
is_eager=True,
help="Path to YAML file with predefined options",
type=click.Path(exists=True, resolve_path=True)
),
Expand Down
34 changes: 34 additions & 0 deletions tests/functional/test_experiments.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import os
import shutil
import tempfile
Expand Down Expand Up @@ -98,6 +99,11 @@ class TestExperimentsCreateSingleNode(object):
"experiments", "create", "singlenode",
"--optionsFile", # path added in test,
]
FULL_OPTIONS_COMMAND_WITH_OPTIONS_FILE_AND_SOME_VALUES_OVERWRITTEN_IN_LINE = [
"experiments", "create", "singlenode",
"--name", "some_other_name",
"--optionsFile", # path added in test,
]
BASIC_OPTIONS_REQUEST = {
"projectHandle": u"testHandle",
"container": u"testContainer",
Expand Down Expand Up @@ -173,6 +179,29 @@ def test_should_read_options_from_config_file(
data=None)
assert result.exit_code == 0, result.exc_info

@mock.patch("gradient.api_sdk.clients.http_client.requests.post")
def test_should_read_options_from_config_file_and_overwrite_options_with_values_provided_in_terminal(
self, post_patched, create_single_node_experiment_config_path):
post_patched.return_value = MockResponse(self.RESPONSE_JSON_200)
request_json = copy.deepcopy(self.FULL_OPTIONS_REQUEST)
request_json["name"] = "some_other_name"
request_json["projectHandle"] = "some_other_project_id"

command = self.FULL_OPTIONS_COMMAND_WITH_OPTIONS_FILE_AND_SOME_VALUES_OVERWRITTEN_IN_LINE[:]
command = command[:] + [create_single_node_experiment_config_path, "--projectId", "some_other_project_id"]

runner = CliRunner()
result = runner.invoke(cli.cli, command)

assert self.EXPECTED_STDOUT in result.output, result.exc_info
post_patched.assert_called_once_with(self.URL_V2,
headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
json=request_json,
params=None,
files=None,
data=None)
assert result.exit_code == 0, result.exc_info

@mock.patch("gradient.api_sdk.clients.http_client.requests.post")
def test_should_send_proper_data_and_print_message_when_create_experiment_was_run_with_full_options(self,
post_patched):
Expand Down Expand Up @@ -953,6 +982,11 @@ class TestExperimentsCreateAndStartSingleNode(TestExperimentsCreateSingleNode):
"experiments", "run", "singlenode",
"--optionsFile", # path added in test,
]
FULL_OPTIONS_COMMAND_WITH_OPTIONS_FILE_AND_SOME_VALUES_OVERWRITTEN_IN_LINE = [
"experiments", "run", "singlenode",
"--name", "some_other_name",
"--optionsFile", # path added in test,
]
EXPECTED_STDOUT = "New experiment created and started with ID: sadkfhlskdjh\n"


Expand Down