Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Test script to validate the comprehensive uvicorn logging fix for TTS service. | |
| This script tests all layers of protection without starting the full service. | |
| """ | |
| import sys | |
| import os | |
| import subprocess | |
| import tempfile | |
| def test_import_safety(): | |
| """Test that imports work without logging conflicts""" | |
| print("π§ͺ Testing import safety...") | |
| test_code = ''' | |
| import sys | |
| print("[TEST] Starting import test") | |
| # This should trigger our SafeStream protection | |
| import os | |
| import warnings | |
| from typing import List, Dict, Any, Optional | |
| # Environment protection should be active | |
| print(f"[TEST] PYTHONWARNINGS: {os.environ.get('PYTHONWARNINGS', 'not set')}") | |
| print(f"[TEST] GRADIO_ANALYTICS_ENABLED: {os.environ.get('GRADIO_ANALYTICS_ENABLED', 'not set')}") | |
| # Logging should be completely disabled | |
| import logging | |
| print(f"[TEST] Logging disabled: {logging.root.disabled}") | |
| # Safe imports that previously caused conflicts | |
| try: | |
| import gradio as gr | |
| print("[TEST] β Gradio import successful") | |
| except Exception as e: | |
| print(f"[TEST] β Gradio import failed: {e}") | |
| try: | |
| import torch | |
| print("[TEST] β PyTorch import successful") | |
| except Exception as e: | |
| print(f"[TEST] β PyTorch import failed: {e}") | |
| try: | |
| import transformers | |
| print("[TEST] β Transformers import successful") | |
| except Exception as e: | |
| print(f"[TEST] β Transformers import failed: {e}") | |
| print("[TEST] Import test completed") | |
| ''' | |
| # Write test code to temporary file | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: | |
| f.write(test_code) | |
| temp_file = f.name | |
| try: | |
| # Run the test code using the TTS app imports | |
| result = subprocess.run([ | |
| sys.executable, '-c', | |
| f'exec(open("{os.path.abspath("app.py")}").read()[:2000]); exec(open("{temp_file}").read())' | |
| ], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__))) | |
| print("STDOUT:") | |
| print(result.stdout) | |
| if result.stderr: | |
| print("STDERR:") | |
| print(result.stderr) | |
| if result.returncode == 0: | |
| print("β Import safety test PASSED") | |
| return True | |
| else: | |
| print("β Import safety test FAILED") | |
| return False | |
| finally: | |
| os.unlink(temp_file) | |
| def test_safe_log_function(): | |
| """Test the safe_log function""" | |
| print("\nπ§ͺ Testing safe_log function...") | |
| test_code = ''' | |
| # Import the safe_log function from app.py | |
| import sys | |
| import os | |
| exec(open("app.py").read()[:3000]) # Import first part including safe_log | |
| # Test safe_log function | |
| try: | |
| safe_log("info", "Test info message") | |
| safe_log("warning", "Test warning message") | |
| safe_log("error", "Test error message") | |
| print("[TEST] β safe_log function working correctly") | |
| except Exception as e: | |
| print(f"[TEST] β safe_log function failed: {e}") | |
| ''' | |
| result = subprocess.run([ | |
| sys.executable, '-c', test_code | |
| ], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__))) | |
| print("STDOUT:") | |
| print(result.stdout) | |
| if result.stderr: | |
| print("STDERR:") | |
| print(result.stderr) | |
| if result.returncode == 0 and "[TTS-INFO]" in result.stdout: | |
| print("β safe_log function test PASSED") | |
| return True | |
| else: | |
| print("β safe_log function test FAILED") | |
| return False | |
| def test_syntax_validation(): | |
| """Test Python syntax validation""" | |
| print("\nπ§ͺ Testing syntax validation...") | |
| result = subprocess.run([ | |
| sys.executable, '-m', 'py_compile', 'app.py' | |
| ], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__))) | |
| if result.returncode == 0: | |
| print("β Syntax validation PASSED") | |
| return True | |
| else: | |
| print("β Syntax validation FAILED") | |
| print("STDERR:", result.stderr) | |
| return False | |
| def test_stream_protection(): | |
| """Test SafeStream protection""" | |
| print("\nπ§ͺ Testing SafeStream protection...") | |
| test_code = ''' | |
| import sys | |
| import os | |
| # Test SafeStream class from app.py | |
| exec(open("app.py").read()[:1000]) # Get SafeStream class | |
| # Test SafeStream functionality | |
| try: | |
| stream = SafeStream('stdout') | |
| stream.write("Test write operation") | |
| stream.flush() | |
| print("[TEST] β SafeStream write/flush successful") | |
| # Test error conditions | |
| try: | |
| stream.fileno() | |
| print("[TEST] β SafeStream should raise OSError for fileno()") | |
| except OSError: | |
| print("[TEST] β SafeStream correctly raises OSError for fileno()") | |
| print(f"[TEST] SafeStream isatty(): {stream.isatty()}") | |
| except Exception as e: | |
| print(f"[TEST] β SafeStream test failed: {e}") | |
| ''' | |
| result = subprocess.run([ | |
| sys.executable, '-c', test_code | |
| ], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__))) | |
| print("STDOUT:") | |
| print(result.stdout) | |
| if result.stderr: | |
| print("STDERR:") | |
| print(result.stderr) | |
| if result.returncode == 0 and "SafeStream write/flush successful" in result.stdout: | |
| print("β SafeStream protection test PASSED") | |
| return True | |
| else: | |
| print("β SafeStream protection test FAILED") | |
| return False | |
| def main(): | |
| """Run all logging fix tests""" | |
| print("π TTS Service - Comprehensive Logging Fix Validation") | |
| print("=" * 60) | |
| tests = [ | |
| ("Syntax Validation", test_syntax_validation), | |
| ("SafeStream Protection", test_stream_protection), | |
| ("safe_log Function", test_safe_log_function), | |
| ("Import Safety", test_import_safety), | |
| ] | |
| results = [] | |
| for test_name, test_func in tests: | |
| print(f"\nπ Running {test_name}...") | |
| try: | |
| result = test_func() | |
| results.append((test_name, result)) | |
| except Exception as e: | |
| print(f"β {test_name} failed with exception: {e}") | |
| results.append((test_name, False)) | |
| print("\n" + "=" * 60) | |
| print("π― TEST SUMMARY") | |
| print("=" * 60) | |
| passed = 0 | |
| total = len(results) | |
| for test_name, result in results: | |
| status = "β PASSED" if result else "β FAILED" | |
| print(f"{test_name:.<40} {status}") | |
| if result: | |
| passed += 1 | |
| print("-" * 60) | |
| print(f"Total: {passed}/{total} tests passed") | |
| if passed == total: | |
| print("\nπ ALL TESTS PASSED - TTS logging fix is working correctly!") | |
| return True | |
| else: | |
| print(f"\nβ οΈ {total - passed} tests failed - review output above") | |
| return False | |
| if __name__ == "__main__": | |
| success = main() | |
| sys.exit(0 if success else 1) |