Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when listing workspaces without an active workspace #16130

Merged
merged 1 commit into from
Nov 27, 2024
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
2 changes: 1 addition & 1 deletion src/prefect/cli/cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ async def ls():
)

for workspace_handle in sorted(workspace.handle for workspace in workspaces):
if workspace_handle == current_workspace.handle:
if current_workspace and workspace_handle == current_workspace.handle:
table.add_row(f"[green]* {workspace_handle}[/green]")
else:
table.add_row(f" {workspace_handle}")
Expand Down
85 changes: 85 additions & 0 deletions tests/cli/cloud/test_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,3 +1125,88 @@ def test_set_workspace_with_less_than_10_workspaces(respx_mock):
PREFECT_API_URL: bar_workspace.api_url(),
PREFECT_API_KEY: "fake-key",
}


class TestCloudWorkspaceLs:
@pytest.fixture
def workspaces(self, respx_mock):
foo_workspace = gen_test_workspace(
account_handle="test1", workspace_handle="foo"
)
bar_workspace = gen_test_workspace(
account_handle="test2", workspace_handle="bar"
)

respx_mock.get(PREFECT_CLOUD_API_URL.value() + "/me/workspaces").mock(
return_value=httpx.Response(
status.HTTP_200_OK,
json=[
foo_workspace.model_dump(mode="json"),
bar_workspace.model_dump(mode="json"),
],
)
)

cloud_profile = "cloud-foo"
save_profiles(
ProfilesCollection(
[
Profile(
name=cloud_profile,
settings={
PREFECT_API_URL: foo_workspace.api_url(),
PREFECT_API_KEY: "fake-key",
},
)
],
active=None,
)
)

with use_profile(cloud_profile):
yield foo_workspace, bar_workspace

def test_ls(self, respx_mock, monkeypatch, workspaces):
foo_workspace, bar_workspace = workspaces
invoke_and_assert(
["cloud", "workspace", "ls"],
expected_code=0,
expected_output_contains=[
"* test1/foo",
"test2/bar",
],
)

def test_ls_without_active_workspace(self, workspaces, monkeypatch):
"""
Regression test for https://github.com/PrefectHQ/prefect/issues/16098
"""
wonky_profile = "wonky-profile"
save_profiles(
ProfilesCollection(
[
Profile(
name=wonky_profile,
settings={
PREFECT_API_URL: "http://something-else.com/api",
PREFECT_API_KEY: "fake-key",
},
)
],
active=None,
)
)

with use_profile(wonky_profile):
invoke_and_assert(
["cloud", "workspace", "ls"],
expected_code=0,
expected_output_contains=[
"test1/foo",
"test2/bar",
],
expected_output_does_not_contain=[
"* test1/foo",
"* test2/bar",
],
)