From b79015d1ee87cad65b0dc5af20dbced1d28ee76b Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Wed, 31 Mar 2021 17:39:36 +0900 Subject: [PATCH 1/4] add flavor option to record session --- launchable/commands/record/session.py | 21 ++++++++++++++++++--- launchable/commands/subset.py | 13 +++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/launchable/commands/record/session.py b/launchable/commands/record/session.py index 9f8696845..82d6401f7 100644 --- a/launchable/commands/record/session.py +++ b/launchable/commands/record/session.py @@ -1,5 +1,6 @@ import click import os +import json from ...utils.http_client import LaunchableClient from ...utils.token import parse_token @@ -26,7 +27,15 @@ default=True, metavar='SESSION_FILE' ) -def session(build_name: str, save_session_file: bool, print_session: bool = True): +@click.option( + "--flavor", + "flavor", + help='flavors', + nargs=2, + type=(str, str), + multiple=True, +) +def session(build_name: str, save_session_file: bool, print_session: bool = True, flavor=[]): """ print_session is for barckward compatibility. If you run this `record session` standalone, the command should print the session ID because v1.1 users expect the beheivior. That is why the flag is default True. @@ -38,12 +47,18 @@ def session(build_name: str, save_session_file: bool, print_session: bool = True "Content-Type": "application/json", } - client = LaunchableClient(token) + flavor_dict = {} + for f in flavor: + flavor_dict[f[0]] = f[1] + client = LaunchableClient(token) try: session_path = "/intake/organizations/{}/workspaces/{}/builds/{}/test_sessions".format( org, workspace, build_name) - res = client.request("post", session_path, headers=headers) + res = client.request("post", session_path, + headers=headers, data=json.dumps({ + "flavors": flavor_dict, + })) res.raise_for_status() session_id = res.json()['id'] diff --git a/launchable/commands/subset.py b/launchable/commands/subset.py index 36c32557e..042b535bb 100644 --- a/launchable/commands/subset.py +++ b/launchable/commands/subset.py @@ -56,8 +56,16 @@ help='output the rest of subset', type=str, ) +@click.option( + "--flavor", + "flavor", + help='flavors', + nargs=2, + type=(str, str), + multiple=True, +) @click.pass_context -def subset(context, target, session_id, base_path: str, build_name: str, rest: str, duration): +def subset(context, target, session_id, base_path: str, build_name: str, rest: str, duration, flavor=[]): token, org, workspace = parse_token() if session_id and build_name: @@ -68,7 +76,8 @@ def subset(context, target, session_id, base_path: str, build_name: str, rest: s if build_name: session_id = read_session(build_name) if not session_id: - context.invoke(session, build_name=build_name, save_session_file=True, print_session=False) + context.invoke(session, build_name=build_name, + save_session_file=True, print_session=False, flavor=flavor) session_id = read_session(build_name) else: raise click.UsageError( From 387c659300afb6b8c9840efb0e01895ac9b8ffd9 Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Thu, 1 Apr 2021 13:00:00 +0900 Subject: [PATCH 2/4] add flavor test --- tests/commands/record/test_session.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/commands/record/test_session.py diff --git a/tests/commands/record/test_session.py b/tests/commands/record/test_session.py new file mode 100644 index 000000000..ca4fb5e7d --- /dev/null +++ b/tests/commands/record/test_session.py @@ -0,0 +1,26 @@ +from tests.cli_test_case import CliTestCase +import responses # type: ignore +import json + + +class SessionTest(CliTestCase): + + @responses.activate + def ttest_run_session_without_flavor(self): + result = self.cli("record", "session", "--build", self.build_name) + self.assertEqual(result.exit_code, 0) + + payload = json.loads(responses.calls[0].request.body) + self.assert_json_orderless_equal({"flavors": {}}, payload) + + @responses.activate + def test_run_session_with_flavor(self): + result = self.cli("record", "session", "--build", self.build_name, + "--flavor", "key", "value", "--flavor", "k", "v") + self.assertEqual(result.exit_code, 0) + + payload = json.loads(responses.calls[0].request.body) + self.assert_json_orderless_equal({"flavors": { + "key": "value", + "k": "v", + }}, payload) From fa9fcb165375bf58220cb49afafcaf222adbcb13 Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Fri, 2 Apr 2021 13:49:02 +0900 Subject: [PATCH 3/4] fix typo --- tests/commands/record/test_session.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/commands/record/test_session.py b/tests/commands/record/test_session.py index ca4fb5e7d..520e9ee29 100644 --- a/tests/commands/record/test_session.py +++ b/tests/commands/record/test_session.py @@ -6,7 +6,7 @@ class SessionTest(CliTestCase): @responses.activate - def ttest_run_session_without_flavor(self): + def test_run_session_without_flavor(self): result = self.cli("record", "session", "--build", self.build_name) self.assertEqual(result.exit_code, 0) From 98357eaa9c8d27644dfd09f0c879330e5aa3d1b0 Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Fri, 2 Apr 2021 13:54:42 +0900 Subject: [PATCH 4/4] add todo comment --- launchable/commands/record/session.py | 1 + 1 file changed, 1 insertion(+) diff --git a/launchable/commands/record/session.py b/launchable/commands/record/session.py index 82d6401f7..6dc4d4f4f 100644 --- a/launchable/commands/record/session.py +++ b/launchable/commands/record/session.py @@ -47,6 +47,7 @@ def session(build_name: str, save_session_file: bool, print_session: bool = True "Content-Type": "application/json", } + # TODO: check duplicate keys flavor_dict = {} for f in flavor: flavor_dict[f[0]] = f[1]