Skip to content

Commit ea38d1f

Browse files
committed
Stop gap CLI v2
- Remove implicit test session creation behavior and all the options used for them - That meant fail fast code became mostly no-op - Removed reading/writing .launchable
1 parent e636393 commit ea38d1f

25 files changed

+106
-956
lines changed

launchable/commands/helper.py

Lines changed: 0 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,7 @@
1-
import datetime
21
from time import time
3-
from typing import Optional, Sequence, Tuple
4-
5-
import click
6-
7-
from launchable.utils.no_build import NO_BUILD_BUILD_NAME
8-
from launchable.utils.tracking import TrackingClient
9-
10-
from ..app import Application
11-
from ..utils.launchable_client import LaunchableClient
12-
from ..utils.session import read_build, read_session, validate_session_format
13-
14-
15-
def require_session(
16-
session: Optional[str],
17-
) -> Optional[str]:
18-
"""Ascertain the contextual test session to operate a CLI command for. If one doesn't exit, fail.
19-
20-
1. If the user explicitly provides the session id via the `--session` option
21-
2. If the user gives no options, the current session ID is read from the session file tied to $PWD.
22-
See https://github.com/cloudbees-oss/smart-tests-cli/pull/342
23-
"""
24-
if session:
25-
validate_session_format(session)
26-
return session
27-
28-
session = read_session(require_build())
29-
if session:
30-
return session
31-
32-
raise click.UsageError(
33-
click.style(
34-
"No saved test session found.\n"
35-
"If you already created a test session on a different machine, use the --session option. "
36-
"See https://docs.launchableinc.com/sending-data-to-launchable/managing-complex-test-session-layouts",
37-
fg="yellow"))
38-
39-
40-
def require_build() -> str:
41-
"""
42-
Like read_build() but fail if a build doesn't exist
43-
"""
44-
b = read_build()
45-
if not b:
46-
raise click.UsageError(
47-
click.style(
48-
"No saved build name found.\n"
49-
"To fix this, run `launchable record build`.\n"
50-
"If you already ran this command on a different machine, use the --session option. "
51-
"See https://www.launchableinc.com/docs/sending-data-to-launchable/using-the-launchable-cli/"
52-
"recording-test-results-with-the-launchable-cli/managing-complex-test-session-layouts/",
53-
fg="yellow"))
54-
return b
55-
56-
57-
def find_or_create_session(
58-
context: click.core.Context,
59-
session: Optional[str],
60-
build_name: Optional[str],
61-
tracking_client: TrackingClient,
62-
flavor: Sequence[Tuple[str, str]] = (),
63-
is_observation: bool = False,
64-
links: Sequence[Tuple[str, str]] = (),
65-
is_no_build: bool = False,
66-
lineage: Optional[str] = None,
67-
test_suite: Optional[str] = None,
68-
timestamp: Optional[datetime.datetime] = None,
69-
) -> Optional[str]:
70-
"""Determine the test session ID to be used.
71-
72-
1. If the user explicitly provides the session id via the `--session` option
73-
2. If the user gives no options, the current session ID is read from the session file tied to $PWD,
74-
or one is created from the current build name. See https://github.com/cloudbees-oss/smart-tests-cli/pull/342
75-
3. The `--build` option is legacy compatible behaviour, in which case a session gets created and tied
76-
to the build. This usage still requires a locally recorded build name that must match the specified name.
77-
Kohsuke is not sure what the historical motivation for this behaviour is.
78-
79-
Args:
80-
session: The --session option value
81-
build_name: The --build option value
82-
flavor: The --flavor option values
83-
is_observation: The --observation value
84-
links: The --link option values
85-
is_no_build: The --no-build option value
86-
lineage: lineage option value
87-
test_suite: --test-suite option value
88-
"""
89-
from .record.session import session as session_command
90-
91-
if session:
92-
validate_session_format(session)
93-
_check_observation_mode_status(session, is_observation, tracking_client=tracking_client, app=context.obj)
94-
return session
95-
96-
if is_no_build:
97-
context.invoke(
98-
session_command,
99-
build_name=NO_BUILD_BUILD_NAME,
100-
save_session_file=True,
101-
print_session=False,
102-
flavor=flavor,
103-
is_observation=is_observation,
104-
links=links,
105-
is_no_build=is_no_build,
106-
lineage=lineage,
107-
test_suite=test_suite,
108-
)
109-
saved_build_name = read_build()
110-
return read_session(str(saved_build_name))
111-
112-
saved_build_name = require_build()
113-
114-
if build_name and saved_build_name != build_name:
115-
raise click.UsageError(
116-
click.style(
117-
"The build name you provided ({}) is different from the last build name recorded on this machine ({}).\n"
118-
"Make sure to run `launchable record build --name {}` before you run this command.\n"
119-
"If you already recorded this build on a different machine, use the --session option instead of --build. "
120-
"See https://www.launchableinc.com/docs/sending-data-to-launchable/using-the-launchable-cli/"
121-
"recording-test-results-with-the-launchable-cli/managing-complex-test-session-layouts/".format(
122-
build_name, saved_build_name, build_name), fg="yellow", ))
123-
124-
session_id = read_session(saved_build_name)
125-
if session_id:
126-
_check_observation_mode_status(session_id, is_observation, tracking_client=tracking_client, app=context.obj)
127-
return session_id
128-
129-
context.invoke(
130-
session_command,
131-
build_name=saved_build_name,
132-
save_session_file=True,
133-
print_session=False,
134-
flavor=flavor,
135-
is_observation=is_observation,
136-
links=links,
137-
is_no_build=is_no_build,
138-
lineage=lineage,
139-
test_suite=test_suite,
140-
timestamp=timestamp,
141-
)
142-
return read_session(saved_build_name)
1432

1443

1454
def time_ns():
1465
# time.time_ns() method is new in Python version 3.7
1476
# As a workaround, we convert time.time() to nanoseconds.
1487
return int(time() * 1e9)
149-
150-
151-
def _check_observation_mode_status(session: str, is_observation: bool,
152-
tracking_client: TrackingClient, app: Optional[Application] = None):
153-
if not is_observation:
154-
return
155-
156-
client = LaunchableClient(tracking_client=tracking_client, app=app)
157-
res = client.request("get", session)
158-
159-
# only check when the status code is 200 not to stop the command
160-
if res.status_code == 200:
161-
is_observation_in_recorded_session = res.json().get("isObservation", False)
162-
if is_observation and not is_observation_in_recorded_session:
163-
click.echo(
164-
click.style(
165-
"WARNING: --observation flag was ignored. Observation mode can only be enabled for a test session "
166-
"during its initial creation. "
167-
"Add `--observation` option to the `launchable record session` command instead.",
168-
fg='yellow'),
169-
err=True)

launchable/commands/inspect/tests.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
from ...utils.authentication import ensure_org_workspace
1111
from ...utils.launchable_client import LaunchableClient
12-
from ...utils.session import parse_session
13-
from ..helper import require_session
1412

1513

1614
class TestResult(object):
@@ -133,6 +131,7 @@ def display(self):
133131
'--test-session-id',
134132
'test_session_id',
135133
help='test session id',
134+
required=True,
136135
)
137136
@click.option(
138137
'--json',
@@ -142,18 +141,6 @@ def display(self):
142141
)
143142
@click.pass_context
144143
def tests(context: click.core.Context, test_session_id: int, is_json_format: bool):
145-
if (test_session_id is None):
146-
try:
147-
session = require_session(None)
148-
_, test_session_id = parse_session(session)
149-
except Exception:
150-
click.echo(
151-
click.style(
152-
"test session id requires.\n"
153-
"Use the --test-session-id option or execute after `launchable record tests` command.",
154-
fg="yellow"))
155-
return
156-
157144
client = LaunchableClient(app=context.obj)
158145
try:
159146
res = client.request(

launchable/commands/record/attachment.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
from typing import Optional
2-
31
import click
42

53
from ...utils.launchable_client import LaunchableClient
6-
from ..helper import require_session
4+
from ...utils.session import validate_session_format
75

86

97
@click.command()
@@ -12,17 +10,18 @@
1210
'session',
1311
help='In the format builds/<build-name>/test_sessions/<test-session-id>',
1412
type=str,
13+
required=True,
1514
)
1615
@click.argument('attachments', nargs=-1) # type=click.Path(exists=True)
1716
@click.pass_context
1817
def attachment(
1918
context: click.core.Context,
2019
attachments,
21-
session: Optional[str] = None
20+
session: str
2221
):
2322
client = LaunchableClient(app=context.obj)
2423
try:
25-
session = require_session(session)
24+
validate_session_format(session)
2625

2726
for a in attachments:
2827
click.echo("Sending {}".format(a))

launchable/commands/record/build.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from ...utils.commands import Command
1717
from ...utils.fail_fast_mode import set_fail_fast_mode, warn_and_exit_if_fail_fast_mode
1818
from ...utils.launchable_client import LaunchableClient
19-
from ...utils.session import clean_session_files, write_build
2019
from .commit import commit
2120

2221
JENKINS_GIT_BRANCH_KEY = "GIT_BRANCH"
@@ -126,8 +125,6 @@ def build(
126125
if not no_commit_collection and len(commits) != 0:
127126
sys.exit("--no-commit-collection must be specified when --commit is used")
128127

129-
clean_session_files(days_ago=14)
130-
131128
# Information we want to collect for each Git repository
132129
# The key data structure throughout the implementation of this command
133130
class Workspace:
@@ -341,9 +338,6 @@ def compute_links():
341338
res = client.request("post", "builds", payload=payload)
342339
res.raise_for_status()
343340

344-
# at this point we've successfully send the data, so it's OK to record this build
345-
write_build(build_name)
346-
347341
return res.json().get("id", None)
348342
except Exception as e:
349343
tracking_client.send_error_event(

launchable/commands/record/session.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
from ...utils.click import KEY_VALUE
1515
from ...utils.commands import Command
16-
from ...utils.fail_fast_mode import FailFastModeValidateParams, fail_fast_mode_validate, set_fail_fast_mode
16+
from ...utils.fail_fast_mode import set_fail_fast_mode
1717
from ...utils.launchable_client import LaunchableClient
1818
from ...utils.no_build import NO_BUILD_BUILD_NAME
19-
from ...utils.session import _session_file_path, read_build, write_session
2019

2120
LAUNCHABLE_SESSION_DIR_KEY = 'LAUNCHABLE_SESSION_DIR'
2221

@@ -41,13 +40,6 @@ def _validate_session_name(ctx, param, value):
4140
type=str,
4241
metavar='BUILD_NAME'
4342
)
44-
@click.option(
45-
'--save-file/--no-save-file',
46-
'save_session_file',
47-
help='save session to file',
48-
default=True,
49-
metavar='SESSION_FILE'
50-
)
5143
@click.option(
5244
"--flavor",
5345
"flavor",
@@ -114,7 +106,6 @@ def _validate_session_name(ctx, param, value):
114106
def session(
115107
ctx: click.core.Context,
116108
build_name: str,
117-
save_session_file: bool,
118109
print_session: bool = True,
119110
flavor: Sequence[Tuple[str, str]] = [],
120111
is_observation: bool = False,
@@ -138,22 +129,10 @@ def session(
138129
client = LaunchableClient(app=ctx.obj, tracking_client=tracking_client)
139130
set_fail_fast_mode(client.is_fail_fast_mode())
140131

141-
fail_fast_mode_validate(FailFastModeValidateParams(
142-
command=Command.RECORD_SESSION,
143-
build=build_name,
144-
is_no_build=is_no_build,
145-
test_suite=test_suite,
146-
))
147-
148132
if not is_no_build and not build_name:
149133
raise click.UsageError("Error: Missing option '--build'")
150134

151135
if is_no_build:
152-
build = read_build()
153-
if build and build != "":
154-
raise click.UsageError(
155-
"The cli already created '{}'. If you want to use the '--no-build' option, please remove this file first.".format(_session_file_path())) # noqa: E501
156-
157136
build_name = NO_BUILD_BUILD_NAME
158137

159138
if session_name:
@@ -226,8 +205,6 @@ def session(
226205
build_name = res.json().get("buildNumber", "")
227206
sub_path = "builds/{}/test_sessions".format(build_name)
228207

229-
if save_session_file:
230-
write_session(build_name, "{}/{}".format(sub_path, session_id))
231208
if print_session:
232209
# what we print here gets captured and passed to `--session` in
233210
# later commands

0 commit comments

Comments
 (0)