Skip to content

Commit a12962d

Browse files
committed
Add API key checks for Anthropic and OpenAI providers
This update adds environment variable checks for ANTHROPIC_API_KEY and OPENAI_API_KEY when the respective provider is selected. If the required API key is not set, a clear error message is shown and the process exits, improving user guidance and preventing runtime errors.
1 parent a701f93 commit a12962d

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

shiny/_main_generate_test.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def generate_test_file(
1818
):
1919
"""Generate AI-powered test file for a Shiny app."""
2020

21-
# Get app file path
2221
if app_file is None:
2322

2423
def path_exists(x: str) -> bool | str:
@@ -37,18 +36,15 @@ def path_exists(x: str) -> bool | str:
3736
else:
3837
app_file_val = app_file
3938

40-
# User quit early
4139
if app_file_val is None:
4240
sys.exit(1)
4341

4442
app_path = Path(app_file_val)
4543

46-
# Make sure app file exists
4744
if not app_path.exists():
4845
click.echo(f"❌ Error: App file does not exist: {app_path}")
4946
sys.exit(1)
5047

51-
# Get output file path if not provided
5248
if output_file is None:
5349
suggested_output = app_path.parent / f"test_{app_path.stem}.py"
5450

@@ -72,13 +68,11 @@ def output_path_valid(x: str) -> bool | str:
7268
else:
7369
output_file_val = output_file
7470

75-
# User quit early
7671
if output_file_val is None:
7772
sys.exit(1)
7873

7974
output_path = Path(output_file_val)
8075

81-
# Validate output file
8276
if output_path.exists():
8377
click.echo(f"❌ Error: Test file already exists: {output_path}")
8478
sys.exit(1)
@@ -87,21 +81,37 @@ def output_path_valid(x: str) -> bool | str:
8781
click.echo("❌ Error: Test file must start with 'test_'")
8882
sys.exit(1)
8983

90-
# Import and use the test generator
9184
try:
92-
# Import the test generator from the new testing module structure
9385
from .testing import ShinyTestGenerator
9486
except ImportError as e:
9587
click.echo(f"❌ Error: Could not import ShinyTestGenerator: {e}")
9688
click.echo("Make sure the shiny testing dependencies are installed.")
9789
sys.exit(1)
9890

91+
import os
92+
93+
if provider == "anthropic":
94+
if not os.getenv("ANTHROPIC_API_KEY"):
95+
click.echo("❌ Error: ANTHROPIC_API_KEY environment variable is not set.")
96+
click.echo("Please set your Anthropic API key:")
97+
click.echo(" export ANTHROPIC_API_KEY='your-api-key-here'")
98+
click.echo()
99+
click.echo("Get your API key from: https://console.anthropic.com/")
100+
sys.exit(1)
101+
elif provider == "openai":
102+
if not os.getenv("OPENAI_API_KEY"):
103+
click.echo("❌ Error: OPENAI_API_KEY environment variable is not set.")
104+
click.echo("Please set your OpenAI API key:")
105+
click.echo(" export OPENAI_API_KEY='your-api-key-here'")
106+
click.echo()
107+
click.echo("Get your API key from: https://platform.openai.com/api-keys")
108+
sys.exit(1)
109+
99110
click.echo(f"🤖 Generating test using {provider} provider...")
100111
if model:
101112
click.echo(f"📝 Using model: {model}")
102113

103114
try:
104-
# Create the generator
105115
generator = ShinyTestGenerator(provider=provider) # type: ignore
106116

107117
# Generate the test

0 commit comments

Comments
 (0)