runapi 0.1.0__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: runapi
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: A Next.js-inspired file-based routing framework built on FastAPI
5
5
  Project-URL: Homepage, https://github.com/Amanbig/runapi
6
6
  Project-URL: Repository, https://github.com/Amanbig/runapi
@@ -742,7 +742,13 @@ Please include:
742
742
 
743
743
  ## Changelog
744
744
 
745
- ### v0.1.0 (Latest)
745
+ ### v0.1.1 (Latest)
746
+ - **Bug Fix**: Fixed `runapi dev` command failing to import main module
747
+ - **Enhancement**: Improved CLI error handling and validation
748
+ - **Enhancement**: Better Python path management for uvicorn integration
749
+ - **Enhancement**: Added pre-validation of main.py before server startup
750
+
751
+ ### v0.1.1
746
752
  - Initial release
747
753
  - File-based routing system
748
754
  - JWT authentication
@@ -669,7 +669,13 @@ Please include:
669
669
 
670
670
  ## Changelog
671
671
 
672
- ### v0.1.0 (Latest)
672
+ ### v0.1.1 (Latest)
673
+ - **Bug Fix**: Fixed `runapi dev` command failing to import main module
674
+ - **Enhancement**: Improved CLI error handling and validation
675
+ - **Enhancement**: Better Python path management for uvicorn integration
676
+ - **Enhancement**: Added pre-validation of main.py before server startup
677
+
678
+ ### v0.1.1
673
679
  - Initial release
674
680
  - File-based routing system
675
681
  - JWT authentication
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "runapi"
3
- version = "0.1.0"
3
+ version = "0.1.1"
4
4
  description = "A Next.js-inspired file-based routing framework built on FastAPI"
5
5
  readme = "README.md"
6
6
  authors = [{ name = "Amanpreet Singh", email = "amanpreetsinghjhiwant7@gmail.com" }]
@@ -2,7 +2,7 @@
2
2
  RunApi - A Next.js-inspired file-based routing framework built on FastAPI
3
3
  """
4
4
 
5
- __version__ = "0.1.0"
5
+ __version__ = "0.1.1"
6
6
  __author__ = "Amanpreet Singh"
7
7
  __email__ = "amanpreetsinghjhiwant7@gmail.com"
8
8
 
@@ -67,6 +67,29 @@ def dev(
67
67
  console.print()
68
68
 
69
69
  try:
70
+ # Ensure we're in the correct working directory
71
+ import os
72
+ import sys
73
+ current_dir = os.getcwd()
74
+
75
+ # Add current directory to Python path if not already there
76
+ if current_dir not in sys.path:
77
+ sys.path.insert(0, current_dir)
78
+
79
+ # Verify main.py can be imported before starting server
80
+ try:
81
+ import importlib.util
82
+ spec = importlib.util.spec_from_file_location("main", "main.py")
83
+ if spec is None:
84
+ raise ImportError("Cannot load main.py")
85
+ main_module = importlib.util.module_from_spec(spec)
86
+ spec.loader.exec_module(main_module)
87
+ console.print("✅ [green]main.py loaded successfully")
88
+ except Exception as e:
89
+ console.print(f"[red]❌ Error importing main.py: {e}")
90
+ console.print("[yellow]💡 Make sure main.py exists and runapi is installed in this environment")
91
+ raise typer.Exit(code=1)
92
+
70
93
  # Run uvicorn with the FastAPI app
71
94
  uvicorn.run(
72
95
  "main:app",
@@ -74,9 +97,13 @@ def dev(
74
97
  port=config.port,
75
98
  reload=config.reload,
76
99
  log_level=config.log_level.lower(),
100
+ reload_dirs=[current_dir] if config.reload else None,
77
101
  )
78
102
  except KeyboardInterrupt:
79
103
  console.print("\n[yellow]👋 Server stopped")
104
+ except Exception as e:
105
+ console.print(f"[red]❌ Server error: {e}")
106
+ raise typer.Exit(code=1)
80
107
 
81
108
 
82
109
  @app.command()
@@ -0,0 +1,381 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Comprehensive test script for RunApi CLI functionality
4
+ Tests the complete workflow from installation to running the dev server
5
+ """
6
+
7
+ import os
8
+ import sys
9
+ import subprocess
10
+ import tempfile
11
+ import shutil
12
+ import time
13
+ import signal
14
+ from pathlib import Path
15
+ import threading
16
+
17
+
18
+ class RunApiTester:
19
+ def __init__(self):
20
+ self.test_dir = None
21
+ self.original_cwd = os.getcwd()
22
+ self.success_count = 0
23
+ self.total_tests = 0
24
+
25
+ def log(self, message, status="INFO"):
26
+ """Log test messages with status"""
27
+ status_symbols = {
28
+ "INFO": "ℹ️",
29
+ "SUCCESS": "✅",
30
+ "ERROR": "❌",
31
+ "WARNING": "⚠️"
32
+ }
33
+ symbol = status_symbols.get(status, "•")
34
+ print(f"{symbol} {message}")
35
+
36
+ def run_command(self, cmd, timeout=30, expect_success=True):
37
+ """Run a command and return result"""
38
+ try:
39
+ result = subprocess.run(
40
+ cmd,
41
+ shell=True,
42
+ capture_output=True,
43
+ text=True,
44
+ timeout=timeout,
45
+ cwd=self.test_dir or self.original_cwd
46
+ )
47
+
48
+ if expect_success and result.returncode != 0:
49
+ self.log(f"Command failed: {cmd}", "ERROR")
50
+ self.log(f"STDOUT: {result.stdout}", "ERROR")
51
+ self.log(f"STDERR: {result.stderr}", "ERROR")
52
+ return False, result
53
+
54
+ return True, result
55
+ except subprocess.TimeoutExpired:
56
+ self.log(f"Command timed out: {cmd}", "WARNING")
57
+ return False, None
58
+ except Exception as e:
59
+ self.log(f"Command exception: {cmd} - {e}", "ERROR")
60
+ return False, None
61
+
62
+ def test_cli_available(self):
63
+ """Test 1: Check if runapi CLI is available"""
64
+ self.total_tests += 1
65
+ self.log("Testing CLI availability...")
66
+
67
+ success, result = self.run_command("runapi --help")
68
+ if success and "RunApi" in result.stdout:
69
+ self.log("CLI is available and working", "SUCCESS")
70
+ self.success_count += 1
71
+ return True
72
+ else:
73
+ self.log("CLI is not available or not working", "ERROR")
74
+ return False
75
+
76
+ def test_project_creation(self):
77
+ """Test 2: Create a new project"""
78
+ self.total_tests += 1
79
+ self.log("Testing project creation...")
80
+
81
+ # Create temporary directory
82
+ self.test_dir = tempfile.mkdtemp(prefix="runapi_test_")
83
+ os.chdir(self.test_dir)
84
+
85
+ success, result = self.run_command("runapi init testproject")
86
+ if success and Path("testproject").exists():
87
+ self.log("Project created successfully", "SUCCESS")
88
+ self.success_count += 1
89
+
90
+ # Change to project directory
91
+ self.test_dir = str(Path(self.test_dir) / "testproject")
92
+ os.chdir(self.test_dir)
93
+
94
+ return True
95
+ else:
96
+ self.log("Project creation failed", "ERROR")
97
+ return False
98
+
99
+ def test_project_structure(self):
100
+ """Test 3: Verify project structure"""
101
+ self.total_tests += 1
102
+ self.log("Testing project structure...")
103
+
104
+ required_files = [
105
+ "main.py",
106
+ ".env",
107
+ "README.md",
108
+ "routes/index.py",
109
+ "routes/api/hello.py"
110
+ ]
111
+
112
+ missing_files = []
113
+ for file_path in required_files:
114
+ if not Path(file_path).exists():
115
+ missing_files.append(file_path)
116
+
117
+ if not missing_files:
118
+ self.log("All required files present", "SUCCESS")
119
+ self.success_count += 1
120
+ return True
121
+ else:
122
+ self.log(f"Missing files: {missing_files}", "ERROR")
123
+ return False
124
+
125
+ def test_main_import(self):
126
+ """Test 4: Test if main.py can be imported"""
127
+ self.total_tests += 1
128
+ self.log("Testing main.py import...")
129
+
130
+ success, result = self.run_command('python -c "import main; print(\'SUCCESS\')"')
131
+ if success and "SUCCESS" in result.stdout:
132
+ self.log("main.py imports successfully", "SUCCESS")
133
+ self.success_count += 1
134
+ return True
135
+ else:
136
+ self.log("main.py import failed", "ERROR")
137
+ if result:
138
+ self.log(f"Error: {result.stderr}", "ERROR")
139
+ return False
140
+
141
+ def test_runapi_import(self):
142
+ """Test 5: Test if runapi package can be imported"""
143
+ self.total_tests += 1
144
+ self.log("Testing runapi package import...")
145
+
146
+ success, result = self.run_command('python -c "import runapi; print(\'SUCCESS\')"')
147
+ if success and "SUCCESS" in result.stdout:
148
+ self.log("runapi package imports successfully", "SUCCESS")
149
+ self.success_count += 1
150
+ return True
151
+ else:
152
+ self.log("runapi package import failed", "ERROR")
153
+ if result:
154
+ self.log(f"Error: {result.stderr}", "ERROR")
155
+ return False
156
+
157
+ def test_app_creation(self):
158
+ """Test 6: Test app creation"""
159
+ self.total_tests += 1
160
+ self.log("Testing app creation...")
161
+
162
+ test_script = '''
163
+ import sys
164
+ sys.path.insert(0, ".")
165
+ try:
166
+ from runapi import create_runapi_app
167
+ app = create_runapi_app(title="Test")
168
+ fastapi_app = app.get_app()
169
+ print("SUCCESS: App created")
170
+ print(f"App type: {type(fastapi_app)}")
171
+ except Exception as e:
172
+ print(f"ERROR: {e}")
173
+ sys.exit(1)
174
+ '''
175
+
176
+ success, result = self.run_command(f'python -c "{test_script}"')
177
+ if success and "SUCCESS: App created" in result.stdout:
178
+ self.log("App creation successful", "SUCCESS")
179
+ self.success_count += 1
180
+ return True
181
+ else:
182
+ self.log("App creation failed", "ERROR")
183
+ if result:
184
+ self.log(f"Error: {result.stderr}", "ERROR")
185
+ return False
186
+
187
+ def test_uvicorn_direct(self):
188
+ """Test 7: Test uvicorn directly"""
189
+ self.total_tests += 1
190
+ self.log("Testing uvicorn direct import...")
191
+
192
+ # Test if uvicorn can import the main:app
193
+ test_script = '''
194
+ import sys
195
+ import importlib.util
196
+ sys.path.insert(0, ".")
197
+
198
+ try:
199
+ # Test importlib approach (what uvicorn uses)
200
+ spec = importlib.util.spec_from_file_location("main", "main.py")
201
+ main_module = importlib.util.module_from_spec(spec)
202
+ spec.loader.exec_module(main_module)
203
+
204
+ if hasattr(main_module, "app"):
205
+ print("SUCCESS: main:app accessible")
206
+ else:
207
+ print("ERROR: main.app not found")
208
+ sys.exit(1)
209
+
210
+ except Exception as e:
211
+ print(f"ERROR: {e}")
212
+ sys.exit(1)
213
+ '''
214
+
215
+ success, result = self.run_command(f'python -c "{test_script}"')
216
+ if success and "SUCCESS: main:app accessible" in result.stdout:
217
+ self.log("uvicorn can access main:app", "SUCCESS")
218
+ self.success_count += 1
219
+ return True
220
+ else:
221
+ self.log("uvicorn cannot access main:app", "ERROR")
222
+ if result:
223
+ self.log(f"Error: {result.stderr}", "ERROR")
224
+ return False
225
+
226
+ def test_server_startup(self):
227
+ """Test 8: Test if server can start (without running indefinitely)"""
228
+ self.total_tests += 1
229
+ self.log("Testing server startup (quick test)...")
230
+
231
+ # Create a test script that starts the server and immediately stops it
232
+ test_script = '''
233
+ import sys
234
+ import os
235
+ import threading
236
+ import time
237
+ sys.path.insert(0, ".")
238
+
239
+ def test_server():
240
+ try:
241
+ import uvicorn
242
+ import main
243
+
244
+ # Test if we can create a server instance
245
+ config = uvicorn.Config("main:app", host="127.0.0.1", port=8999)
246
+ server = uvicorn.Server(config)
247
+ print("SUCCESS: Server can be created")
248
+ return True
249
+ except Exception as e:
250
+ print(f"ERROR: {e}")
251
+ return False
252
+
253
+ if test_server():
254
+ sys.exit(0)
255
+ else:
256
+ sys.exit(1)
257
+ '''
258
+
259
+ success, result = self.run_command(f'python -c "{test_script}"')
260
+ if success and "SUCCESS: Server can be created" in result.stdout:
261
+ self.log("Server startup test passed", "SUCCESS")
262
+ self.success_count += 1
263
+ return True
264
+ else:
265
+ self.log("Server startup test failed", "ERROR")
266
+ if result:
267
+ self.log(f"Error: {result.stderr}", "ERROR")
268
+ return False
269
+
270
+ def test_cli_dev_dry_run(self):
271
+ """Test 9: Test CLI dev command validation (without actual server start)"""
272
+ self.total_tests += 1
273
+ self.log("Testing CLI dev command validation...")
274
+
275
+ # We'll test the CLI's pre-validation logic
276
+ test_script = '''
277
+ import sys
278
+ import os
279
+ sys.path.insert(0, ".")
280
+
281
+ # Simulate what the CLI does before starting uvicorn
282
+ try:
283
+ # Check if main.py exists
284
+ if not os.path.exists("main.py"):
285
+ print("ERROR: main.py not found")
286
+ sys.exit(1)
287
+
288
+ # Check if main can be imported
289
+ import importlib.util
290
+ spec = importlib.util.spec_from_file_location("main", "main.py")
291
+ if spec is None:
292
+ print("ERROR: Cannot load main.py")
293
+ sys.exit(1)
294
+
295
+ main_module = importlib.util.module_from_spec(spec)
296
+ spec.loader.exec_module(main_module)
297
+
298
+ if not hasattr(main_module, "app"):
299
+ print("ERROR: main.py does not have app attribute")
300
+ sys.exit(1)
301
+
302
+ print("SUCCESS: CLI validation passed")
303
+
304
+ except Exception as e:
305
+ print(f"ERROR: {e}")
306
+ sys.exit(1)
307
+ '''
308
+
309
+ success, result = self.run_command(f'python -c "{test_script}"')
310
+ if success and "SUCCESS: CLI validation passed" in result.stdout:
311
+ self.log("CLI dev command validation passed", "SUCCESS")
312
+ self.success_count += 1
313
+ return True
314
+ else:
315
+ self.log("CLI dev command validation failed", "ERROR")
316
+ if result:
317
+ self.log(f"Error: {result.stderr}", "ERROR")
318
+ return False
319
+
320
+ def cleanup(self):
321
+ """Clean up test directory"""
322
+ try:
323
+ os.chdir(self.original_cwd)
324
+ if self.test_dir and Path(self.test_dir).exists():
325
+ # Go up to temp directory and remove the whole test dir
326
+ test_root = Path(self.test_dir).parents[0] if "testproject" in self.test_dir else Path(self.test_dir)
327
+ shutil.rmtree(test_root, ignore_errors=True)
328
+ self.log("Test directory cleaned up", "INFO")
329
+ except Exception as e:
330
+ self.log(f"Cleanup warning: {e}", "WARNING")
331
+
332
+ def run_all_tests(self):
333
+ """Run all tests"""
334
+ self.log("🚀 Starting RunApi Installation Tests", "INFO")
335
+ self.log(f"Python: {sys.executable}", "INFO")
336
+ self.log(f"Working directory: {os.getcwd()}", "INFO")
337
+ print("-" * 60)
338
+
339
+ try:
340
+ # Run tests in sequence
341
+ tests = [
342
+ self.test_cli_available,
343
+ self.test_project_creation,
344
+ self.test_project_structure,
345
+ self.test_main_import,
346
+ self.test_runapi_import,
347
+ self.test_app_creation,
348
+ self.test_uvicorn_direct,
349
+ self.test_server_startup,
350
+ self.test_cli_dev_dry_run,
351
+ ]
352
+
353
+ for i, test in enumerate(tests, 1):
354
+ self.log(f"Running test {i}/{len(tests)}: {test.__name__}", "INFO")
355
+ try:
356
+ test()
357
+ except Exception as e:
358
+ self.log(f"Test {test.__name__} threw exception: {e}", "ERROR")
359
+ print("-" * 40)
360
+
361
+ finally:
362
+ self.cleanup()
363
+
364
+ # Results
365
+ print("=" * 60)
366
+ self.log("🏁 TEST RESULTS", "INFO")
367
+ self.log(f"Passed: {self.success_count}/{self.total_tests}", "SUCCESS" if self.success_count == self.total_tests else "WARNING")
368
+
369
+ if self.success_count == self.total_tests:
370
+ self.log("🎉 All tests passed! RunApi is working correctly.", "SUCCESS")
371
+ return True
372
+ else:
373
+ failed = self.total_tests - self.success_count
374
+ self.log(f"❌ {failed} test(s) failed. RunApi may have issues.", "ERROR")
375
+ return False
376
+
377
+
378
+ if __name__ == "__main__":
379
+ tester = RunApiTester()
380
+ success = tester.run_all_tests()
381
+ sys.exit(0 if success else 1)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes