Skip to content

Commit

Permalink
Fix error when listing workspaces without an active workspace (#16130)
Browse files Browse the repository at this point in the history
  • Loading branch information
desertaxle authored Nov 27, 2024
1 parent 3da0f05 commit 9526d4f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
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",
],
)

0 comments on commit 9526d4f

Please sign in to comment.