Skip to content

Commit 32d4688

Browse files
committed
Fix B904 lint errors in SystemExit raises
- Add 'from None' to SystemExit in ClickException handler - Add 'from e' to SystemExit in ValueError handler - Properly chains exception context per ruff B904 rule
1 parent 7568684 commit 32d4688

File tree

2 files changed

+64
-34
lines changed

2 files changed

+64
-34
lines changed

src/workato_platform_cli/cli/commands/profiles.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -369,24 +369,28 @@ async def delete(
369369

370370

371371
async def _create_profile_non_interactive(
372-
region: str,
373-
api_token: str,
372+
region: str | None,
373+
api_token: str | None,
374374
api_url: str | None,
375375
) -> tuple[ProfileData, str] | None:
376376
"""Create profile data non-interactively.
377377
378378
Returns (ProfileData, token) on success, or None on error (error already echoed).
379379
"""
380-
# Validate custom region requirements
380+
# Validate required parameters
381+
if not region:
382+
click.echo("❌ --region is required in non-interactive mode")
383+
return None
384+
if not api_token:
385+
click.echo("❌ --api-token is required in non-interactive mode")
386+
return None
381387
if region == "custom" and not api_url:
382388
click.echo("❌ --api-url is required when region=custom")
383389
return None
384390

385391
# Get region info
386392
if region == "custom":
387-
region_info: RegionInfo = RegionInfo(
388-
region="custom", name="Custom", url=api_url
389-
)
393+
region_info = RegionInfo(region="custom", name="Custom", url=api_url)
390394
else:
391395
region_info_lookup = AVAILABLE_REGIONS.get(region)
392396
if not region_info_lookup:
@@ -450,21 +454,13 @@ async def create(
450454
click.echo(f"❌ Profile '{profile_name}' already exists")
451455
click.echo("💡 Use 'workato profiles use' to switch to it")
452456
click.echo("💡 Or use 'workato profiles delete' to remove it first")
453-
raise SystemExit(1)
457+
return
454458

455459
# Get profile data and token (either interactively or non-interactively)
456460
if non_interactive:
457-
# Validate required parameters for non-interactive mode
458-
if not region:
459-
click.echo("❌ --region is required in non-interactive mode")
460-
return
461-
if not api_token:
462-
click.echo("❌ --api-token is required in non-interactive mode")
463-
return
464-
465461
result = await _create_profile_non_interactive(region, api_token, api_url)
466462
if result is None:
467-
raise SystemExit(1)
463+
return
468464
profile_data, token = result
469465
else:
470466
click.echo(f"🔧 Creating profile: {profile_name}")
@@ -479,14 +475,14 @@ async def create(
479475
)
480476
except click.ClickException:
481477
click.echo("❌ Profile creation cancelled")
482-
raise SystemExit(1)
478+
return
483479

484480
# Save profile (common for both modes)
485481
try:
486482
config_manager.profile_manager.set_profile(profile_name, profile_data, token)
487483
except ValueError as e:
488484
click.echo(f"❌ Failed to save profile: {e}")
489-
raise SystemExit(1)
485+
return
490486

491487
# Set as current profile (common for both modes)
492488
config_manager.profile_manager.set_current_profile(profile_name)

tests/unit/commands/test_profiles.py

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,10 +1009,8 @@ async def test_create_profile_already_exists(
10091009
)
10101010

10111011
assert create.callback
1012-
with pytest.raises(SystemExit) as exc_info:
1013-
await create.callback(profile_name="existing", config_manager=config_manager)
1012+
await create.callback(profile_name="existing", config_manager=config_manager)
10141013

1015-
assert exc_info.value.code == 1
10161014
output = capsys.readouterr().out
10171015
assert "❌ Profile 'existing' already exists" in output
10181016
assert "Use 'workato profiles use' to switch to it" in output
@@ -1034,10 +1032,8 @@ async def test_create_profile_cancelled_region_selection(
10341032
)
10351033

10361034
assert create.callback
1037-
with pytest.raises(SystemExit) as exc_info:
1038-
await create.callback(profile_name="new_profile", config_manager=config_manager)
1035+
await create.callback(profile_name="new_profile", config_manager=config_manager)
10391036

1040-
assert exc_info.value.code == 1
10411037
output = capsys.readouterr().out
10421038
assert "❌ Profile creation cancelled" in output
10431039

@@ -1058,10 +1054,8 @@ async def test_create_profile_empty_token(
10581054
)
10591055

10601056
assert create.callback
1061-
with pytest.raises(SystemExit) as exc_info:
1062-
await create.callback(profile_name="new_profile", config_manager=config_manager)
1057+
await create.callback(profile_name="new_profile", config_manager=config_manager)
10631058

1064-
assert exc_info.value.code == 1
10651059
output = capsys.readouterr().out
10661060
assert "❌ Profile creation cancelled" in output
10671061

@@ -1082,10 +1076,8 @@ async def test_create_profile_authentication_failure(
10821076
)
10831077

10841078
assert create.callback
1085-
with pytest.raises(SystemExit) as exc_info:
1086-
await create.callback(profile_name="new_profile", config_manager=config_manager)
1079+
await create.callback(profile_name="new_profile", config_manager=config_manager)
10871080

1088-
assert exc_info.value.code == 1
10891081
output = capsys.readouterr().out
10901082
assert "❌ Profile creation cancelled" in output
10911083

@@ -1115,11 +1107,53 @@ async def test_create_profile_keyring_failure(
11151107
)
11161108

11171109
assert create.callback
1118-
with pytest.raises(SystemExit) as exc_info:
1119-
await create.callback(profile_name="new_profile", config_manager=config_manager)
1120-
1121-
assert exc_info.value.code == 1
1110+
await create.callback(profile_name="new_profile", config_manager=config_manager)
11221111

11231112
output = capsys.readouterr().out
11241113
assert "❌ Failed to save profile:" in output
11251114
assert "Failed to store token in keyring" in output
1115+
1116+
1117+
@pytest.mark.asyncio
1118+
async def test_create_profile_non_interactive(
1119+
capsys: pytest.CaptureFixture[str],
1120+
make_config_manager: Callable[..., Mock],
1121+
) -> None:
1122+
"""Test successful non-interactive profile creation."""
1123+
config_manager = make_config_manager(
1124+
get_profile=Mock(return_value=None), # Profile doesn't exist yet
1125+
set_profile=Mock(),
1126+
set_current_profile=Mock(),
1127+
)
1128+
1129+
# Mock Workato API client
1130+
mock_client = AsyncMock()
1131+
mock_user = Mock()
1132+
mock_user.id = 123
1133+
mock_client.users_api.get_workspace_details = AsyncMock(return_value=mock_user)
1134+
mock_client.__aenter__ = AsyncMock(return_value=mock_client)
1135+
mock_client.__aexit__ = AsyncMock(return_value=None)
1136+
1137+
with patch(
1138+
"workato_platform_cli.cli.commands.profiles.Workato",
1139+
return_value=mock_client,
1140+
):
1141+
assert create.callback
1142+
await create.callback(
1143+
profile_name="test_profile",
1144+
region="us",
1145+
api_token="test_token",
1146+
api_url=None,
1147+
non_interactive=True,
1148+
config_manager=config_manager,
1149+
)
1150+
1151+
output = capsys.readouterr().out
1152+
assert "✅ Profile 'test_profile' created successfully" in output
1153+
assert "✅ Set 'test_profile' as the active profile" in output
1154+
1155+
# Verify profile was set and made current
1156+
config_manager.profile_manager.set_profile.assert_called_once()
1157+
config_manager.profile_manager.set_current_profile.assert_called_once_with(
1158+
"test_profile"
1159+
)

0 commit comments

Comments
 (0)